1
0
mirror of https://github.com/bspeice/dtparse synced 2024-11-12 17:08:09 -05:00

Initial structure

This commit is contained in:
Bradlee Speice 2018-05-13 16:18:45 -04:00
commit 9ab5a3d5e3
4 changed files with 59 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/target
**/*.rs.bk
Cargo.lock

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "dtparse"
version = "0.1.0"
authors = ["Bradlee Speice <bspeice@kcg.com>"]
[dependencies]
chrono = "0.4"
chrono-tz = "0.4"

18
src/lib.rs Normal file
View File

@ -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<DateTime<Utc>, 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))
}

29
tests/parse.rs Normal file
View File

@ -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);
}