From 9ab5a3d5e34b8b1c3bd79d3d3e95e0fda9595c43 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 13 May 2018 16:18:45 -0400 Subject: [PATCH] Initial structure --- .gitignore | 4 ++++ Cargo.toml | 8 ++++++++ src/lib.rs | 18 ++++++++++++++++++ tests/parse.rs | 29 +++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs create mode 100644 tests/parse.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a821aa9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ + +/target +**/*.rs.bk +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..124162f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "dtparse" +version = "0.1.0" +authors = ["Bradlee Speice "] + +[dependencies] +chrono = "0.4" +chrono-tz = "0.4" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..d9d9c5d --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,18 @@ +extern crate chrono; + +use chrono::NaiveDateTime; +use chrono::DateTime; +use chrono::Utc; +use chrono::ParseError; +use std::time::UNIX_EPOCH; +use std::time::SystemTime; + +pub fn parse(date: &str) -> Result, ParseError> { + + let current = SystemTime::now(); + let epoch = current.duration_since(UNIX_EPOCH).unwrap(); + + let naive = NaiveDateTime::from_timestamp(epoch.as_secs() as i64, epoch.subsec_nanos()); + + Ok(DateTime::from_utc(naive, Utc)) +} diff --git a/tests/parse.rs b/tests/parse.rs new file mode 100644 index 0000000..82531c4 --- /dev/null +++ b/tests/parse.rs @@ -0,0 +1,29 @@ +extern crate chrono; +extern crate dtparse; + +use chrono::DateTime; +use chrono::Utc; +use chrono::NaiveDate; +use chrono::NaiveTime; +use chrono::NaiveDateTime; + +use dtparse::parse; + +macro_rules! ymd_test { + ($date: expr, $year: expr, $month: expr, $day: expr) => { + let nd = NaiveDate::from_ymd($year, $month, $day); + let nt = NaiveTime::from_hms(0, 0, 0); + let dt = NaiveDateTime::new(nd, nt); + let utc_dt = DateTime::from_utc(dt, Utc); + + let parsed = parse($date); + + println!("{:?}", parsed); + assert!(parsed == Ok(utc_dt)); + }; +} + +#[test] +fn test_basic() { + ymd_test!("2014 January 19", 2014, 1, 19); +} \ No newline at end of file