Skip to content

Commit

Permalink
Merge pull request #9 from chardon55/8-add-chrono-support-as-an-optio…
Browse files Browse the repository at this point in the history
…nal-feature

Add chrono timestamp feature
  • Loading branch information
chardoncs authored Feb 9, 2024
2 parents ab2a23b + 604d745 commit 8a72693
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ homepage = "https://github.com/chardon55/minotp"

[dependencies]
hmac = "0.12"
chrono = { version = "0.4", optional = true }

[dev-dependencies]
chrono = "^0.4.33"
data-encoding = "2.5.0"
sha1 = { version = "0.10", default-features = false }
sha2 = { version = "0.10", default-features = false }
sha3 = { version = "0.10", default-features = false }

[features]
default = []
chrono = ["dep:chrono"]

18 changes: 8 additions & 10 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(not(feature = "chrono"))]
use std::time::{SystemTime, UNIX_EPOCH};

const ENS_BIT: usize = 8;
Expand Down Expand Up @@ -29,20 +30,17 @@ pub(crate) fn calc_totp_counter(interval: u32, timestamp: u64) -> (u64, u32) {
}

#[inline]
#[cfg(feature = "chrono")]
pub(crate) fn time_now() -> u64 {
chrono::Utc::now().timestamp() as u64
}

#[inline]
#[cfg(not(feature = "chrono"))]
pub(crate) fn time_now() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("[minotp::Totp] FATAL: Your system time is probably incorrectly set.")
.as_secs()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_time_now() {
// Compare our own function with the chrono's
assert_eq!(time_now(), chrono::Utc::now().timestamp() as u64)
}
}

0 comments on commit 8a72693

Please sign in to comment.