@@ -0,0 +1,2 @@ | |||||
/target | |||||
*.swp |
@@ -0,0 +1,168 @@ | |||||
# This file is automatically @generated by Cargo. | |||||
# It is not intended for manual editing. | |||||
[[package]] | |||||
name = "ansi_term" | |||||
version = "0.11.0" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" | |||||
dependencies = [ | |||||
"winapi", | |||||
] | |||||
[[package]] | |||||
name = "atty" | |||||
version = "0.2.14" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" | |||||
dependencies = [ | |||||
"hermit-abi", | |||||
"libc", | |||||
"winapi", | |||||
] | |||||
[[package]] | |||||
name = "autocfg" | |||||
version = "1.0.0" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" | |||||
[[package]] | |||||
name = "bitflags" | |||||
version = "1.2.1" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" | |||||
[[package]] | |||||
name = "chrono" | |||||
version = "0.4.11" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2" | |||||
dependencies = [ | |||||
"num-integer", | |||||
"num-traits", | |||||
"time", | |||||
] | |||||
[[package]] | |||||
name = "clap" | |||||
version = "2.33.0" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" | |||||
dependencies = [ | |||||
"ansi_term", | |||||
"atty", | |||||
"bitflags", | |||||
"strsim", | |||||
"textwrap", | |||||
"unicode-width", | |||||
"vec_map", | |||||
] | |||||
[[package]] | |||||
name = "hermit-abi" | |||||
version = "0.1.8" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "1010591b26bbfe835e9faeabeb11866061cc7dcebffd56ad7d0942d0e61aefd8" | |||||
dependencies = [ | |||||
"libc", | |||||
] | |||||
[[package]] | |||||
name = "libc" | |||||
version = "0.2.68" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0" | |||||
[[package]] | |||||
name = "num-integer" | |||||
version = "0.1.42" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba" | |||||
dependencies = [ | |||||
"autocfg", | |||||
"num-traits", | |||||
] | |||||
[[package]] | |||||
name = "num-traits" | |||||
version = "0.2.11" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" | |||||
dependencies = [ | |||||
"autocfg", | |||||
] | |||||
[[package]] | |||||
name = "redox_syscall" | |||||
version = "0.1.56" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" | |||||
[[package]] | |||||
name = "strsim" | |||||
version = "0.8.0" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" | |||||
[[package]] | |||||
name = "textwrap" | |||||
version = "0.11.0" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" | |||||
dependencies = [ | |||||
"unicode-width", | |||||
] | |||||
[[package]] | |||||
name = "time" | |||||
version = "0.1.42" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" | |||||
dependencies = [ | |||||
"libc", | |||||
"redox_syscall", | |||||
"winapi", | |||||
] | |||||
[[package]] | |||||
name = "unicode-width" | |||||
version = "0.1.7" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" | |||||
[[package]] | |||||
name = "utcnow" | |||||
version = "1.0.0" | |||||
dependencies = [ | |||||
"chrono", | |||||
"clap", | |||||
"libc", | |||||
] | |||||
[[package]] | |||||
name = "vec_map" | |||||
version = "0.8.1" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" | |||||
[[package]] | |||||
name = "winapi" | |||||
version = "0.3.8" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" | |||||
dependencies = [ | |||||
"winapi-i686-pc-windows-gnu", | |||||
"winapi-x86_64-pc-windows-gnu", | |||||
] | |||||
[[package]] | |||||
name = "winapi-i686-pc-windows-gnu" | |||||
version = "0.4.0" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" | |||||
[[package]] | |||||
name = "winapi-x86_64-pc-windows-gnu" | |||||
version = "0.4.0" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" |
@@ -0,0 +1,12 @@ | |||||
[package] | |||||
name = "utcnow" | |||||
version = "1.0.0" | |||||
authors = ["Jonathan Strong <jonathan.strong@gmail.com>"] | |||||
edition = "2018" | |||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |||||
[dependencies] | |||||
chrono = "0.4" | |||||
clap = "2" | |||||
libc = "0.2.68" |
@@ -0,0 +1,21 @@ | |||||
``` | |||||
utcnow 1.0.0 | |||||
Jonathan Strong <jonathan.strong@gmail.com> | |||||
current time in utc with non-cryptic interface. | |||||
for your own safety and well-being, local time functionality is not provided. | |||||
default output format is rfc3339 with trailing 'Z', e.g. '1999-12-31T23:59:59.999999999Z' | |||||
USAGE: | |||||
utcnow [FLAGS] | |||||
FLAGS: | |||||
-h, --help help | |||||
-r, --rfc2822 display in rfc2822 format | |||||
-s, --seconds display unix timestamp in seconds, instead of default nanoseconds | |||||
-t, --timespec display as <sec>,<nsec> | |||||
-u, --unix display elapsed nanoseconds since 1970-01-01T00:00:00Z | |||||
-V, --version version | |||||
``` |
@@ -0,0 +1,80 @@ | |||||
#![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)); | |||||
} | |||||
} |