1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-07 00:17:47 +00:00

touch: turn macros into functions

This commit is contained in:
Terts Diepraam 2021-03-13 12:47:20 +01:00
parent dfc7a95054
commit 86422a70d2

View file

@ -36,23 +36,15 @@ pub mod options {
static ARG_FILES: &str = "files"; static ARG_FILES: &str = "files";
// Since touch's date/timestamp parsing doesn't account for timezone, the fn to_local(mut tm: time::Tm) -> time::Tm {
// returned value from time::strptime() is UTC. We get system's timezone to
// localize the time.
macro_rules! to_local(
($exp:expr) => ({
let mut tm = $exp;
tm.tm_utcoff = time::now().tm_utcoff; tm.tm_utcoff = time::now().tm_utcoff;
tm tm
}) }
);
macro_rules! local_tm_to_filetime( fn local_tm_to_filetime(tm: time::Tm) -> FileTime {
($exp:expr) => ({ let ts = tm.to_timespec();
let ts = $exp.to_timespec();
FileTime::from_unix_time(ts.sec as i64, ts.nsec as u32) FileTime::from_unix_time(ts.sec as i64, ts.nsec as u32)
}) }
);
fn get_usage() -> String { fn get_usage() -> String {
format!("{0} [OPTION]... [USER]", executable!()) format!("{0} [OPTION]... [USER]", executable!())
@ -161,7 +153,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
}; };
(timestamp, timestamp) (timestamp, timestamp)
} else { } else {
let now = local_tm_to_filetime!(time::now()); let now = local_tm_to_filetime(time::now());
(now, now) (now, now)
}; };
@ -249,7 +241,7 @@ fn parse_date(str: &str) -> FileTime {
// not about to implement GNU parse_datetime. // not about to implement GNU parse_datetime.
// http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=lib/parse-datetime.y // http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=lib/parse-datetime.y
match time::strptime(str, "%c") { match time::strptime(str, "%c") {
Ok(tm) => local_tm_to_filetime!(to_local!(tm)), Ok(tm) => local_tm_to_filetime(to_local(tm)),
Err(e) => panic!("Unable to parse date\n{}", e), Err(e) => panic!("Unable to parse date\n{}", e),
} }
} }
@ -267,7 +259,7 @@ fn parse_timestamp(s: &str) -> FileTime {
}; };
match time::strptime(&ts, format) { match time::strptime(&ts, format) {
Ok(tm) => local_tm_to_filetime!(to_local!(tm)), Ok(tm) => local_tm_to_filetime(to_local(tm)),
Err(e) => panic!("Unable to parse timestamp\n{}", e), Err(e) => panic!("Unable to parse timestamp\n{}", e),
} }
} }