Skip to content

Commit

Permalink
add runnable examples for dateparser (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
waltzofpearls authored May 15, 2021
1 parent bbced1d commit db391c7
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 17 deletions.
7 changes: 5 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion belt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "belt"
version = "0.1.0"
version = "0.1.1"
authors = ["Rollie Ma <[email protected]>"]
edition = "2018"
publish = false
Expand Down
5 changes: 4 additions & 1 deletion dateparser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dateparser"
version = "0.1.0"
version = "0.1.1"
authors = ["Rollie Ma <[email protected]>"]
description = "Parse dates in string formats that are commonly used"
readme = "README.md"
Expand All @@ -14,3 +14,6 @@ edition = "2018"
anyhow = "1.0.40"
chrono = "0.4"
regex = "1.5.4"

[dev-dependencies]
chrono-tz = "0.5.3"
4 changes: 2 additions & 2 deletions dateparser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Add to your `Cargo.toml`:

```toml
[dependencies]
dateparser = "0.1.0"
dateparser = "0.1.1"
```

And then use `dateparser` in your code:
Expand Down Expand Up @@ -54,7 +54,7 @@ Convert returned `DateTime<Utc>` to pacific time zone datetime with `chrono-tz`:
```toml
[dependencies]
chrono-tz = "0.5.3"
dateparser = "0.1.0"
dateparser = "0.1.1"
```

```rust
Expand Down
9 changes: 9 additions & 0 deletions dateparser/examples/convert_to_pacific.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use chrono_tz::US::Pacific;
use dateparser::DateTimeUtc;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
let parsed = "2021-05-14 18:51 PDT".parse::<DateTimeUtc>()?.0;
println!("{:#?}", parsed.with_timezone(&Pacific));
Ok(())
}
8 changes: 8 additions & 0 deletions dateparser/examples/parse_function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use dateparser::parse;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
let parsed = parse("6:15pm")?;
println!("{:#?}", parsed);
Ok(())
}
8 changes: 8 additions & 0 deletions dateparser/examples/str_parse_method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use dateparser::DateTimeUtc;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
let parsed = "2021-05-14 18:51 PDT".parse::<DateTimeUtc>()?.0;
println!("{:#?}", parsed);
Ok(())
}
40 changes: 29 additions & 11 deletions dateparser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,39 +372,57 @@ mod tests {
);

let tz = FixedOffset::west(8 * 3600);
let now = Local::now();

// ymd
assert_eq!(
parse("2021-02-21").unwrap().trunc_subsecs(0),
Local::ymd(&Local, 2021, 2, 21)
.and_hms_nano(now.hour(), now.minute(), now.second(), now.nanosecond())
.and_hms_nano(
Local::now().hour(),
Local::now().minute(),
Local::now().second(),
Local::now().nanosecond()
)
.trunc_subsecs(0)
.with_timezone(&Utc)
);
// ymd_z
assert_eq!(
parse("2021-02-21 PST").unwrap().trunc_subsecs(0),
tz.ymd(2021, 2, 21)
.and_hms_nano(now.hour(), now.minute(), now.second(), now.nanosecond())
.and_hms_nano(
Local::now().hour(),
Local::now().minute(),
Local::now().second(),
Local::now().nanosecond()
)
.trunc_subsecs(0)
.with_timezone(&Utc)
);
// hms_imp
assert_eq!(
parse("4:00pm").unwrap(),
Local::ymd(&Local, now.year(), now.month(), now.day())
.and_time(NaiveTime::from_hms(16, 0, 0))
.unwrap()
.with_timezone(&Utc)
Local::ymd(
&Local,
Local::now().year(),
Local::now().month(),
Local::now().day()
)
.and_time(NaiveTime::from_hms(16, 0, 0))
.unwrap()
.with_timezone(&Utc)
);
// hms_imp_z
assert_eq!(
parse("6:00 AM PST").unwrap(),
tz.ymd(now.year(), now.month(), now.day())
.and_time(NaiveTime::from_hms(6, 0, 0))
.unwrap()
.with_timezone(&Utc)
tz.ymd(
Local::now().year(),
Local::now().month(),
Local::now().day()
)
.and_time(NaiveTime::from_hms(6, 0, 0))
.unwrap()
.with_timezone(&Utc)
);
// bey_hms_z
assert_eq!(
Expand Down

0 comments on commit db391c7

Please sign in to comment.