Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "unix-ts"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
authors = [
"Luke Sneeringer <[email protected]>"
Expand All @@ -21,10 +21,11 @@ exclude = [

[dependencies]
chrono = { version = "0.4", optional = true }
unix-ts-macros = { path = "macros", version = "0.2" }
unix-ts-macros = { path = "macros", version = "0.3" }

[dev-dependencies]
chrono-tz = "0.5"
chrono-tz = "0.6"
assert2 = "0.3"

[build-dependencies]
readme-rustdocifier = "0.1"
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ use unix_ts::Timestamp;
let t = Timestamp::from(1335020400);
```

For milliseconds, microseconds, or nanoseconds, there are specific `from`
methods available:

```rs
use unix_ts::Timestamp;

let t = Timestamp::from_nanos(1335020400_000_000_000i64);
```

Finally, the `new` method accepts `seconds` and `nanos`. This is generally less
convenient than the macro, though, because you have to convert fractional
seconds to nanos by yourself.
Expand Down
2 changes: 1 addition & 1 deletion macros-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "unix-ts-macros-test"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
publish = false

Expand Down
4 changes: 2 additions & 2 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "unix-ts-macros"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
keywords = ["date", "time", "unix-timestamp", "timestamp"]
categories = ["date-and-time"]
Expand All @@ -20,7 +20,7 @@ exclude = [
proc-macro = true

[dev-dependencies]
unix-ts = { path = "..", version = "0.2.0" }
unix-ts = { path = "..", version = "0.3.0" }

[build-dependencies]
readme-rustdocifier = "0.1"
77 changes: 73 additions & 4 deletions src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ impl Timestamp {
Timestamp { seconds: seconds, nanos: nanos }
}

/// Create a timestamp from the given number of nanoseconds.
pub fn from_nanos(nanos: impl Into<i128>) -> Timestamp {
let nanos = nanos.into();
let seconds: i64 = (nanos / 1_000_000_000)
.try_into()
.expect("Timestamp value out of range.");
let nanos = if seconds >= 0 {
(nanos % 1_000_000_000) as u32
}
else {
(1_000_000_000 - (nanos % 1_000_000_000).abs()) as u32
};
Timestamp { seconds: seconds, nanos: nanos }
}

/// Create a timestamp from the given number of microseconds.
pub fn from_micros(micros: impl Into<i128>) -> Timestamp {
Timestamp::from_nanos(micros.into() * 1_000)
}

/// Create a timestamp from the given number of milliseconds.
pub fn from_millis(millis: impl Into<i128>) -> Timestamp {
Timestamp::from_nanos(millis.into() * 1_000_000)
}

/// Return the seconds since the Unix epoch.
/// Sub-second values are discarded.
///
Expand Down Expand Up @@ -119,16 +144,60 @@ impl Sub for Timestamp {
#[cfg(test)]
mod tests {
use super::*;
use assert2::check;

#[test]
fn test_cmp() {
assert!(Timestamp::from(1335020400) < Timestamp::from(1335024000));
assert!(Timestamp::from(1335020400) == Timestamp::from(1335020400));
assert!(
check!(Timestamp::from(1335020400) < Timestamp::from(1335024000));
check!(Timestamp::from(1335020400) == Timestamp::from(1335020400));
check!(
Timestamp::new(1335020400, 500_000_000)
< Timestamp::new(1335020400, 750_000_000)
);
assert!(Timestamp::new(1, 999_999_999) < Timestamp::from(2));
check!(Timestamp::new(1, 999_999_999) < Timestamp::from(2));
}

#[test]
fn test_from_nanos() {
check!(
Timestamp::from_nanos(1335020400_000_000_000i64)
== Timestamp::new(1335020400, 0)
);
check!(
Timestamp::from_nanos(1335020400_500_000_000i64)
== Timestamp::new(1335020400, 500_000_000)
);
check!(
Timestamp::from_nanos(-1_750_000_000) == Timestamp::new(-1, 250_000_000)
);
}

#[test]
fn test_from_micros() {
check!(
Timestamp::from_micros(1335020400_000_000i64)
== Timestamp::new(1335020400, 0)
);
check!(
Timestamp::from_micros(1335020400_500_000i64)
== Timestamp::new(1335020400, 500_000_000)
);
check!(
Timestamp::from_micros(-1_750_000) == Timestamp::new(-1, 250_000_000)
);
}

#[test]
fn test_from_millis() {
check!(
Timestamp::from_millis(1335020400_000i64)
== Timestamp::new(1335020400, 0)
);
check!(
Timestamp::from_millis(1335020400_500i64)
== Timestamp::new(1335020400, 500_000_000)
);
check!(Timestamp::from_millis(-1_750) == Timestamp::new(-1, 250_000_000));
}

#[test]
Expand Down