Skip to content

Commit

Permalink
Merge pull request #505 from MatthewFluet/date-localOffset-fix
Browse files Browse the repository at this point in the history
Fix `Date.localOffset` for time zones east of prime meridian
  • Loading branch information
MatthewFluet authored Jul 28, 2023
2 parents 63666ac + 86ca971 commit f291d2a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Here are the changes from version 20210117 to YYYYMMDD.

=== Details

* 2023-0-728
** Fix bug in `Date.localOffset` for time zones east of prime meridian.
Thanks to Arata Mizuki (minoki) for the bug report and suggested fix.

* 2023-05-29
** Fix bugs in `WORD.scan` when `0` is followed by `w` or `x` or
`wx` but not by more digits. Thanks to Arata Mizuki (minoki) for
Expand Down
6 changes: 3 additions & 3 deletions basis-library/system/date.sml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ structure Date :> DATE =
fun mktime_ (t: tmoz): C_Time.t = C_Errno.check (setTmBuf t; Prim.mkTime ())

(* The offset to add to local time to get UTC: positive West of UTC *)
val localoffset: int = C_Double.round (Prim.localOffset ())
fun localoffset () : int = C_Double.round (Prim.localOffset ())

val toweekday: int -> weekday =
fn 0 => Sun | 1 => Mon | 2 => Tue | 3 => Wed
Expand Down Expand Up @@ -293,14 +293,14 @@ structure Date :> DATE =
val secoffset =
case offset of
NONE => 0
| SOME secs => localoffset + secs
| SOME secs => localoffset () + secs
val clock = C_Time.toInt (mktime_ (dateToTmoz date)) - secoffset
in
if clock < 0 then raise Date
else Time.fromSeconds clock
end

fun localOffset () = Time.fromSeconds (localoffset mod 86400)
fun localOffset () = Time.fromSeconds (Int.rem (localoffset (), 86400))

local
val isFormatChar =
Expand Down
3 changes: 3 additions & 0 deletions regression/date-localOffset.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TZ = UTC+4 :: Date.localOffset () = 14400.000
TZ = UTC+0 :: Date.localOffset () = 0.000
TZ = UTC-4 :: Date.localOffset () = ~14400.000
12 changes: 12 additions & 0 deletions regression/date-localOffset.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fun doit tz =
let
val () = MLton.ProcEnv.setenv {name = "TZ", value = tz}
val lo = Date.localOffset ()
in
app print
["TZ = ", tz, " :: Date.localOffset () = ", Time.toString lo, "\n"]
end

val () = doit "UTC+4"
val () = doit "UTC+0"
val () = doit "UTC-4"

0 comments on commit f291d2a

Please sign in to comment.