|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #![allow(unused)]
-
- use chrono::{DateTime, Utc, NaiveDateTime};
-
- fn nanos(utc: DateTime<Utc>) -> u64 {
- (utc.timestamp() as u64) * 1_000_000_000_u64 + (utc.timestamp_subsec_nanos() as u64)
- }
-
- fn get_time() -> (i64, i32) {
- let mut tv = libc::timespec { tv_sec: 0, tv_nsec: 0 };
- unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, &mut tv); }
- (tv.tv_sec as i64, tv.tv_nsec as i32)
- }
-
- fn timespec_to_utc(sec: i64, nsec: i32) -> DateTime<Utc> {
- let naive = NaiveDateTime::from_timestamp(sec, nsec as u32);
- DateTime::from_utc(naive, Utc)
- }
-
- fn timespec_to_nanos(sec: i64, nsec: i32) -> u64 {
- (sec as u64) * 1_000_000_000_u64 + (nsec as u64)
- }
-
- fn main() {
- let args: clap::ArgMatches = clap::App::new("utcnow")
- .author("Jonathan Strong <jonathan.strong@gmail.com>")
- .version(clap::crate_version!())
- .about("\ncurrent time in utc with non-cryptic interface.\n\n\
- for your own safety and well-being, local time functionality is not provided.\n\n\
- default output format is rfc3339 with trailing 'Z', e.g. '1999-12-31T23:59:59.999999999Z'")
- .help_message("help")
- .version_message("version")
- .arg(clap::Arg::with_name("unix")
- .help("display elapsed nanoseconds since 1970-01-01T00:00:00Z")
- .long("unix")
- .short("u")
- .required(false)
- .takes_value(false))
- .arg(clap::Arg::with_name("rfc2822")
- .help("display in rfc2822 format")
- .long("rfc2822")
- .short("r")
- .required(false)
- .takes_value(false))
- .arg(clap::Arg::with_name("seconds")
- .help("display unix timestamp in seconds, instead of default nanoseconds")
- .long("seconds")
- .short("s")
- .requires("unix")
- .required(false)
- .takes_value(false))
- .arg(clap::Arg::with_name("timespec")
- .help("display as <sec>,<nsec>")
- .long("timespec")
- .short("t")
- .required(false)
- .takes_value(false))
- .get_matches();
-
- let (sec, nsec) = get_time();
-
- if args.is_present("timespec") {
- println!("{},{}", sec, nsec);
- } else if args.is_present("unix") {
- if args.is_present("seconds") {
- println!("{}", sec);
- } else {
- println!("{}", timespec_to_nanos(sec, nsec));
- }
- } else if args.is_present("rfc2822") {
-
- println!("{}", timespec_to_utc(sec, nsec).to_rfc2822());
-
- } else {
- // Debug view is iso format / rfc3339 that we want
- // Display has spaces - no go
- // to_rfc3339 uses +00:00 instead of Z, which I don't like
- println!("{:?}", timespec_to_utc(sec, nsec));
- }
- }
|