Skip to content

Fixes for time zone conversions #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 5, 2025
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
2 changes: 1 addition & 1 deletion src/gleam/time/timestamp.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ fn to_calendar_from_offset(
timestamp: Timestamp,
offset: Int,
) -> #(Int, Int, Int, Int, Int, Int) {
let total = timestamp.seconds - { offset * 60 }
let total = timestamp.seconds + { offset * 60 }
let seconds = modulo(total, 60)
let total_minutes = floored_div(total, 60.0)
let minutes = modulo(total, 60 * 60) / 60
Expand Down
2 changes: 1 addition & 1 deletion src/gleam_time_ffi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export function system_time() {
}

export function local_time_offset_seconds() {
return new Date().getTimezoneOffset() * 60;
return new Date().getTimezoneOffset() * -60;
}
25 changes: 22 additions & 3 deletions test/gleam/time/timestamp_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ pub fn to_rfc3339_6_test() {
}

pub fn to_rfc3339_7_test() {
timestamp.from_unix_seconds(60 * 60 + 60 * 5)
timestamp.from_unix_seconds(-60 * 60 - 60 * 5)
|> timestamp.to_rfc3339(duration.seconds(65 * 60))
|> should.equal("1970-01-01T00:00:00+01:05")
}

pub fn to_rfc3339_8_test() {
timestamp.from_unix_seconds(0)
|> timestamp.to_rfc3339(duration.seconds(-120 * 60))
|> should.equal("1970-01-01T02:00:00-02:00")
|> timestamp.to_rfc3339(duration.seconds(120 * 60))
|> should.equal("1970-01-01T02:00:00+02:00")
}

pub fn to_rfc3339_9_test() {
Expand Down Expand Up @@ -356,6 +356,25 @@ pub fn rfc3339_string_timestamp_rfc3339_string_roundtrip_property_test() {
original_timestamp == roundtrip_timestamp
}

// Eastern US Timezone round trip tests
pub fn rfc3339_string_timestamp_rfc3339_string_roundtrip_to_est_test() {
let assert Ok(date_time) =
timestamp.parse_rfc3339("2025-02-04T13:00:00+00:00")

date_time
|> timestamp.to_rfc3339(duration.seconds(-18_000))
|> should.equal("2025-02-04T08:00:00-05:00")
}

pub fn rfc3339_string_timestamp_rfc3339_string_roundtrip_from_est_test() {
let assert Ok(date_time) =
timestamp.parse_rfc3339("2025-02-04T13:00:00-05:00")

date_time
|> timestamp.to_rfc3339(calendar.utc_offset)
|> should.equal("2025-02-04T18:00:00Z")
}

// Check against OCaml Ptime reference implementation.
//
// These test cases include leap seconds.
Expand Down