Browse Source

add --rfc3339-to-unix command to parse a datetime in rfc3339 and display it as unix timestamp

tags/v1.2.0
Jonathan Strong 4 years ago
parent
commit
82714c36e7
2 changed files with 27 additions and 7 deletions
  1. +1
    -1
      Cargo.toml
  2. +26
    -6
      src/main.rs

+ 1
- 1
Cargo.toml View File

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




+ 26
- 6
src/main.rs View File

@@ -1,6 +1,6 @@
#![allow(unused)] #![allow(unused)]
use std::str::FromStr; use std::str::FromStr;
use chrono::{DateTime, Utc, NaiveDateTime};
use chrono::{DateTime, Utc, NaiveDateTime, TimeZone};


const ONE_SECOND: u64 = 1_000_000_000_u64; const ONE_SECOND: u64 = 1_000_000_000_u64;


@@ -68,18 +68,26 @@ fn main() {
.help("terminate displayed time with null character instead of newline") .help("terminate displayed time with null character instead of newline")
.takes_value(false) .takes_value(false)
.required(false)) .required(false))
.arg(clap::Arg::with_name("unix-to-utc")
.long("unix-to-utc")
.help("convert integer unix timestamp (nanoseconds precision) to iso datetime")
.arg(clap::Arg::with_name("unix-to-rfc3339")
.long("unix-to-rfc3339")
.alias("unix-to-utc")
.help("convert integer unix timestamp (nanoseconds precision) to datetime in rfc3339 format")
.takes_value(true) .takes_value(true)
.required(false) .required(false)
.conflicts_with_all(&["unix", "seconds"])) .conflicts_with_all(&["unix", "seconds"]))
.arg(clap::Arg::with_name("rfc3339-to-unix")
.long("rfc3339-to-unix")
.alias("utc-to-unix")
.help("parse rfc3339 timestamp and display it as a unix timestamp")
.takes_value(true)
.required(false)
.conflicts_with_all(&["unix", "unix-to-rfc3339", "rfc2822", "timespec"]))
.get_matches(); .get_matches();


let (sec, nsec): (i64, i32) = let (sec, nsec): (i64, i32) =


if let Some(ts_str) = args.value_of("unix-to-utc") { // either parse --unix-to-utc 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) {
Ok(nanos) => nanos_to_timespec(nanos), Ok(nanos) => nanos_to_timespec(nanos),


@@ -88,6 +96,18 @@ fn main() {
std::process::exit(1); std::process::exit(1);
} }
} }

} else if let Some(rfc_str) = args.value_of("rfc3339-to-unix") { // or --rfc3339-to-unix
//match DateTime::<Utc>::parse_from_rfc3339(rfc_str) {
match rfc_str.parse::<DateTime<Utc>>() {
Ok(utc) => nanos_to_timespec(nanos(utc)),

Err(e) => {
eprintln!("failed to parse timestamp (expected integer): {}", e);
std::process::exit(1);
}
}

} else { // or else get current time } else { // or else get current time
get_time() get_time()
}; };
@@ -100,7 +120,7 @@ fn main() {
print!("{},{}{}", sec, nsec, endline); print!("{},{}{}", sec, nsec, endline);




} else if args.is_present("unix") { // display unix
} else if args.is_present("unix") || args.is_present("rfc3339-to-unix") { // display unix


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


Loading…
Cancel
Save