Skip to content

Commit

Permalink
Merge pull request #9 from kamphaus/8-improved-file-parsing
Browse files Browse the repository at this point in the history
Improved timestamp parsing from file
  • Loading branch information
christophe-kamphaus-jemmic authored Jun 4, 2023
2 parents 6dd8e99 + 4717d0a commit 4dd9845
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
# before the plugin, as the cache uses the current rustc version as its cache key
- run: rustup toolchain install stable --profile minimal
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: "true"

- name: Build
run: cargo build --verbose --locked
Expand Down
34 changes: 17 additions & 17 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "upt"
version = "1.1.0"
version = "1.1.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
38 changes: 33 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ fn persist_time(dt: DateTime<Utc>) -> BoxResult<()> {

fn read_time() -> BoxResult<DateTime<Utc>> {
let persisted = fs::read_to_string(get_file_path()?)?;
let parsed = DateTime::parse_from_rfc3339(persisted.as_str())?;
parse_time(persisted)
}

fn parse_time(date_str: String) -> BoxResult<DateTime<Utc>> {
let parsed = DateTime::parse_from_rfc3339(date_str.trim())?;
Ok(parsed.with_timezone(&Utc))
}

Expand Down Expand Up @@ -219,8 +223,32 @@ fn main() {
}
}

#[test]
fn verify_cli() {
use clap::CommandFactory;
Cli::command().debug_assert()
#[cfg(test)]
mod tests {
use super::*;
use chrono::{DateTime, NaiveDateTime, Utc};

#[test]
fn verify_cli() {
use clap::CommandFactory;
Cli::command().debug_assert()
}

#[test]
fn test_date_parsing() {
let nt = NaiveDateTime::from_timestamp_opt(1685871491, 0);
let dt: DateTime<Utc> = DateTime::from_utc(nt.unwrap(), Utc);
assert_eq!(
parse_time("2023-06-04T09:38:11.000000000+00:00".to_string()).unwrap(),
dt
);
assert_eq!(
parse_time("2023-06-04T09:38:11.000000000+00:00\n".to_string()).unwrap(),
dt
);
assert_eq!(
parse_time("2030604T09:xy:11.000000000+00:00".to_string()).is_err(),
true
)
}
}

0 comments on commit 4dd9845

Please sign in to comment.