Skip to content

Commit

Permalink
fix negative timestamp support
Browse files Browse the repository at this point in the history
  • Loading branch information
PgBiel committed Apr 15, 2024
1 parent 0b19881 commit 929ece6
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/birl.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,12 @@ pub fn ffi_to_parts(
// fallback for Nix
let timestamp = timestamp + offset
// microseconds => days
let days = timestamp / { 1000 * 1000 * 3600 * 24 }
// (with adjustment to handle negative days)
let microseconds_per_day = 1000 * 1000 * 3600 * 24
let days = case timestamp >= 0 {
True -> timestamp / microseconds_per_day
False -> {timestamp - {microseconds_per_day - 1}} / microseconds_per_day
}

// From http://howardhinnant.github.io/date_algorithms.html (`civil_from_days`)
let #(year, month, date) = {
Expand Down Expand Up @@ -1575,7 +1580,7 @@ pub fn ffi_to_parts(
#(year, month, date)
}

let remaining_microseconds = timestamp - { days * 1000 * 1000 * 3600 * 24 }
let remaining_microseconds = timestamp - days * microseconds_per_day
let remaining_milliseconds = remaining_microseconds / 1000
let hours = remaining_milliseconds / { 1000 * 3600 }
let minutes = { remaining_milliseconds - hours * 1000 * 3600 } / { 1000 * 60 }
Expand Down

0 comments on commit 929ece6

Please sign in to comment.