Skip to content

Commit

Permalink
Merge pull request #28 from yerTools/main
Browse files Browse the repository at this point in the history
Add functions to get and create Time based on Unix timestamps with a millisecond or microsecond precision.
  • Loading branch information
massivefermion committed May 23, 2024
2 parents 0dd9e9b + 5634728 commit f633776
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/birl.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,30 @@ pub fn from_unix(value: Int) -> Time {
Time(value * 1_000_000, 0, option.None, option.None)
}

/// unix milli timestamps are the number of milliseconds that have elapsed since 00:00:00 UTC on January 1st, 1970
pub fn to_unix_milli(value: Time) -> Int {
case value {
Time(t, _, _, _) -> t / 1000
}
}

/// unix milli timestamps are the number of milliseconds that have elapsed since 00:00:00 UTC on January 1st, 1970
pub fn from_unix_milli(value: Int) -> Time {
Time(value * 1000, 0, option.None, option.None)
}

/// unix micro timestamps are the number of microseconds that have elapsed since 00:00:00 UTC on January 1st, 1970
pub fn to_unix_micro(value: Time) -> Int {
case value {
Time(t, _, _, _) -> t
}
}

/// unix micro timestamps are the number of microseconds that have elapsed since 00:00:00 UTC on January 1st, 1970
pub fn from_unix_micro(value: Int) -> Time {
Time(value, 0, option.None, option.None)
}

pub fn compare(a: Time, b: Time) -> order.Order {
let Time(wall_time: wta, offset: _, timezone: _, monotonic_time: mta) = a
let Time(wall_time: wtb, offset: _, timezone: _, monotonic_time: mtb) = b
Expand Down

0 comments on commit f633776

Please sign in to comment.