Skip to content

Commit

Permalink
Lua: Use a different epoch to avoid negative Unix time
Browse files Browse the repository at this point in the history
  • Loading branch information
minoki committed Oct 7, 2024
1 parent b8be995 commit 2d3beb4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/lunarml/ml/basis/lua/time.sml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ signature TIME = sig
*)
end;
local
(* Avoid 1970-01-01 00:00:00 in local time, which might be negative *)
val epoch = LunarML.assumeDiscardable (fn () =>
let val t = Lua.newTable ()
in Lua.setField (t, "year", Lua.fromInt 1970)
in Lua.setField (t, "year", Lua.fromInt 2001)
; Lua.setField (t, "month", Lua.fromInt 1)
; Lua.setField (t, "day", Lua.fromInt 1)
; Lua.setField (t, "hour", Lua.fromInt 0)
Expand Down Expand Up @@ -91,7 +92,7 @@ fun toLuaTime (x : int) : Lua.value
val hour = hour' mod 24
val day' = hour' div 24
val t = Lua.newTable ()
in Lua.setField (t, "year", Lua.fromInt 1970)
in Lua.setField (t, "year", Lua.fromInt 2001)
; Lua.setField (t, "month", Lua.fromInt 1)
; Lua.setField (t, "day", Lua.fromInt (day' + 1))
; Lua.setField (t, "hour", Lua.fromInt hour)
Expand Down
5 changes: 3 additions & 2 deletions lib/lunarml/ml/basis/luajit/time.sml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ signature TIME = sig
*)
end;
local
(* Avoid 1970-01-01 00:00:00 in local time, which might be negative *)
val epoch = LunarML.assumeDiscardable (fn () =>
let val t = Lua.newTable ()
in Lua.setField (t, "year", Lua.fromInt 1970)
in Lua.setField (t, "year", Lua.fromInt 2001)
; Lua.setField (t, "month", Lua.fromInt 1)
; Lua.setField (t, "day", Lua.fromInt 1)
; Lua.setField (t, "hour", Lua.fromInt 0)
Expand Down Expand Up @@ -91,7 +92,7 @@ fun toLuaTime (x : Int54.int) : Lua.value
val hour = hour' mod 24
val day' = hour' div 24
val t = Lua.newTable ()
in Lua.setField (t, "year", Lua.fromInt 1970)
in Lua.setField (t, "year", Lua.fromInt 2001)
; Lua.setField (t, "month", Lua.fromInt 1)
; Lua.setField (t, "day", Lua.fromInt54 (day' + 1))
; Lua.setField (t, "hour", Lua.fromInt54 hour)
Expand Down

0 comments on commit 2d3beb4

Please sign in to comment.