Browse Source

adds parsing timestamps with alternate precisions (including new option --millis)

tags/v1.3.0
Jonathan Strong 3 years ago
parent
commit
a9adfb8acf
2 changed files with 26 additions and 6 deletions
  1. +1
    -1
      Cargo.toml
  2. +25
    -5
      src/main.rs

+ 1
- 1
Cargo.toml View File

@@ -1,6 +1,6 @@
[package] [package]
name = "utcnow" name = "utcnow"
version = "1.2.0"
version = "1.3.0"
authors = ["Jonathan Strong <jonathan.strong@gmail.com>"] authors = ["Jonathan Strong <jonathan.strong@gmail.com>"]
edition = "2018" edition = "2018"




+ 25
- 5
src/main.rs View File

@@ -52,8 +52,14 @@ fn main() {
.help("display unix timestamp in seconds, instead of default nanoseconds") .help("display unix timestamp in seconds, instead of default nanoseconds")
.long("seconds") .long("seconds")
.short("s") .short("s")
.requires("unix")
.conflicts_with_all(&["rfc2822", "timespec"])
.conflicts_with_all(&["rfc2822", "timespec", "millis"])
.required(false)
.takes_value(false))
.arg(clap::Arg::with_name("millis")
.help("display unix timestamp in milliseconds, instead of default nanoseconds")
.long("millis")
.short("m")
.conflicts_with_all(&["rfc2822", "timespec", "seconds"])
.required(false) .required(false)
.takes_value(false)) .takes_value(false))
.arg(clap::Arg::with_name("timespec") .arg(clap::Arg::with_name("timespec")
@@ -70,13 +76,17 @@ fn main() {
.required(false)) .required(false))
.arg(clap::Arg::with_name("unix-to-rfc3339") .arg(clap::Arg::with_name("unix-to-rfc3339")
.long("unix-to-rfc3339") .long("unix-to-rfc3339")
.short("R")
.alias("unix-to-utc") .alias("unix-to-utc")
.help("convert integer unix timestamp (nanoseconds precision) to datetime in rfc3339 format")
.help("convert integer unix timestamp (nanoseconds precision) to datetime \
in rfc3339 format. combine with --seconds or --millis to parse a
seconds timestamp with alternate precison")
.takes_value(true) .takes_value(true)
.required(false) .required(false)
.conflicts_with_all(&["unix", "seconds"]))
.conflicts_with_all(&["unix"]))
.arg(clap::Arg::with_name("rfc3339-to-unix") .arg(clap::Arg::with_name("rfc3339-to-unix")
.long("rfc3339-to-unix") .long("rfc3339-to-unix")
.short("U")
.alias("utc-to-unix") .alias("utc-to-unix")
.help("parse rfc3339 timestamp and display it as a unix timestamp") .help("parse rfc3339 timestamp and display it as a unix timestamp")
.takes_value(true) .takes_value(true)
@@ -89,6 +99,15 @@ fn main() {


if let Some(ts_str) = args.value_of("unix-to-rfc3339") { // either parse --unix-to-rfc3339 input if let Some(ts_str) = args.value_of("unix-to-rfc3339") { // either parse --unix-to-rfc3339 input
match u64::from_str(ts_str) { match u64::from_str(ts_str) {

// check alternate precisions --seconds or --millis

Ok(seconds) if args.is_present("seconds") => nanos_to_timespec(seconds * ONE_SECOND),
Ok(millis) if args.is_present("millis") => nanos_to_timespec(millis * 1_000_000),

// otherwise go with default nanos

Ok(nanos) => nanos_to_timespec(nanos), Ok(nanos) => nanos_to_timespec(nanos),


Err(e) => { Err(e) => {
@@ -124,7 +143,8 @@ fn main() {


if args.is_present("seconds") { // unix seconds if args.is_present("seconds") { // unix seconds
print!("{}{}", sec, endline); print!("{}{}", sec, endline);

} else if args.is_present("millis") { // unix millis
print!("{}{}", timespec_to_nanos(sec, nsec) / 1_000_000, endline);
} else { // unix nanos } else { // unix nanos
print!("{}{}", timespec_to_nanos(sec, nsec), endline); print!("{}{}", timespec_to_nanos(sec, nsec), endline);
} }


Loading…
Cancel
Save