Skip to content

Instantly share code, notes, and snippets.

@rightgo09
Created May 5, 2022 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightgo09/1f837f9f2da56f130e6c6e96e5f6aacf to your computer and use it in GitHub Desktop.
Save rightgo09/1f837f9f2da56f130e6c6e96e5f6aacf to your computer and use it in GitHub Desktop.
Parse unixtime in Rust
use std::env;
use chrono::prelude::DateTime;
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
println!("argument is unixtime");
return;
}
let unixtime = &args[1];
let pretty = pretty_format(unixtime);
println!("{}", pretty);
// let utc_datetime: DateTime<Utc> = Utc::now();
// let utc_date: Date<Utc> = Utc::today();
// println!("{}", utc_datetime);
// println!("{}", utc_date);
// let local_datetime: DateTime<Local> = Local::now();
// let local_date: Date<Local> = Local::today();
// println!("{}", local_datetime);
// println!("{}", local_date);
// let dt = DateTime::parse_from_str(
// "1983 Apr 13 12:09:14.274 +0000", "%Y %b %d %H:%M:%S%.3f %z");
// println!("{}", dt.unwrap());
// let dt = NaiveDateTime::parse_from_str("1651713139", "%s");
// println!("{}", dt.unwrap());
// let dt = DateTime::parse_from_str("1651713139 +0900", "%s %z");
// println!("{}", dt.unwrap());
}
fn pretty_format(unixtime: &String) -> String {
// println!("pretty_format: unixtime: {}", unixtime);
let unixtime = format!("{} +0900", unixtime);
match DateTime::parse_from_str(unixtime.as_str(), "%s %z") {
Ok(t) => t.to_string(),
Err(e) => panic!("error: {:?}", e),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment