mirror of
https://github.com/bspeice/dtparse
synced 2024-12-22 04:18:09 -05:00
Initial structure
This commit is contained in:
commit
9ab5a3d5e3
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
/target
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal 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
18
src/lib.rs
Normal 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
29
tests/parse.rs
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user