Home My Page Projects Code Snippets Project Openings SML/NJ Bugs
Summary Activity Tracker Lists

[#239] Date.toTime is incorrect (by a factor of 10E9)

Date:
2019-12-16 23:17
Priority:
3
State:
Closed
Submitted by:
Bug Submitter (webuser)
Assigned to:
John Reppy (jhr)
Machine Architecture:
None
Operating System:
Generic Unix
Component:
Basis Library
Resolution:
Fixed
Severity:
Major
OS Version:
SML/NJ Version:
110.96
Keywords:
URL:
Transcript (of reproduction):
val t = TIME {usec=1576534090258702} : Time.time val s = 1576534090 : IntInf.int val d = DATE {day=16,hour=23,isDst=SOME false,minute=8,month=Dec, offset=SOME (TIME {usec=~3600000000}),second=10,wday=Mon,yday=349, year=2019} : Date.date val t' = TIME {usec=1576534089999996400000000} : Time.time val s' = 1576534089999996400 : IntInf.int val d' = DATE {day=11,hour=1,isDst=SOME false,minute=58,month=Feb, offset=SOME (TIME {usec=~3600000000}),second=9,wday=Tue,yday=41, year=2127} : Date.date val factor = 999999999 : IntInf.int val difference = ~1576534088423462310 : IntInf.int
Source (for reproduction):
val t = Time.now (); val s = Time.toSeconds t; val d = Date.fromTimeLocal t; val t' = Date.toTime d; val s' = Time.toSeconds t'; val d' = Date.fromTimeLocal t'; val factor = IntInf.div (s', s); val difference = IntInf.- (s, s');
Summary:
Date.toTime is incorrect (by a factor of 10E9)

Detailed description
The values returned by the Date.toTime function are too large
by a factor of 10E9.

This is caused by a typo in the definition of function nsToTime
in Basis/Implementation/date.sml (introduced in revision 5400 [1]
and hence in release 110.90).

Obviously, Time.fromSeconds should be replaced by
Time.fromNanoseconds in nsToTime.

[1] <http://smlnj-gforge.cs.uchicago.edu/scm/viewvc.php/sml/trunk/system/Basis/Implementation/date.sml?root=smlnj&r1=5366&r2=5400>

Additional comments:
After applying the patch below and recompiling the system,
the result of executing the above source is as follows:

val t = TIME {usec=1576534078704822} : Time.time
val s = 1576534078 : IntInf.int
val d =
DATE
{day=16,hour=23,isDst=SOME false,minute=7,month=Dec,
offset=SOME (TIME {usec=~3600000000}),second=58,wday=Mon,yday=349,
year=2019} : Date.date
val t' = TIME {usec=1576530478000000} : Time.time
val s' = 1576530478 : IntInf.int
val d' =
DATE
{day=16,hour=22,isDst=SOME false,minute=7,month=Dec,
offset=SOME (TIME {usec=~3600000000}),second=58,wday=Mon,yday=349,
year=2019} : Date.date
val factor = 0 : IntInf.int
val difference = 3600 : IntInf.int


Fix:
--- base/system/Basis/Implementation/date.sml.orig 2019-06-12 21:16:48 UTC
+++ base/system/Basis/Implementation/date.sml
@@ -70,7 +70,7 @@ structure Date : DATE =
= wrap (CInterface.c_function "SMLNJ-Date" "strfTime")

(* conversions between integer numbers of seconds (used by runtime) and Time.time values *)
- fun nsToTime s = Time.fromSeconds (Word64.toLargeInt s)
+ fun nsToTime s = Time.fromNanoseconds (Word64.toLargeInt s)
fun timeToNs t = Word64.fromLargeInt (Time.toNanoseconds t)

val localTime = localTime' o timeToNs


Submitted via web form by Johannes 5 Joemann <joemann@beefree.free.de>

Comments:

Message  ↓
Date: 2019-12-21 18:58
Sender: John Reppy

Fixed for 110.97.

Attached Files:

Changes

Field Old Value Date By
status_idOpen2019-12-21 18:58jhr
close_dateNone2019-12-21 18:58jhr
assigned_tonone2019-12-21 18:58jhr
detailsThe values returned by the Date.toTime function are too large by a factor of 10E9. This is caused by a typo in the definition of function nsToTime in Basis/Implementation/date.sml (introduced in revision 5400 [1] and hence in release 110.90). Obviously, Time.fromSeconds should be replaced by Time.fromNanoseconds in nsToTime. [1] <http://smlnj-gforge.cs.uchicago.edu/scm/viewvc.php/sml/trunk/system/Basis/Implementation/date.sml?root=smlnj&r1=5366&r2=5400> Additional comments: After applying the patch below and recompiling the system, the result of executing the above source is as follows: val t = TIME {usec=1576534078704822} : Time.time val s = 1576534078 : IntInf.int val d = DATE {day=16,hour=23,isDst=SOME false,minute=7,month=Dec, offset=SOME (TIME {usec=~3600000000}),second=58,wday=Mon,yday=349, year=2019} : Date.date val t' = TIME {usec=1576530478000000} : Time.time val s' = 1576530478 : IntInf.int val d' = DATE {day=16,hour=22,isDst=SOME false,minute=7,month=Dec, offset=SOME (TIME {usec=~3600000000}),second=58,wday=Mon,yday=349, year=2019} : Date.date val factor = 0 : IntInf.int val difference = 3600 : IntInf.int Fix: --- base/system/Basis/Implementation/date.sml.orig 2019-06-12 21:16:48 UTC +++ base/system/Basis/Implementation/date.sml @@ -70,7 +70,7 @@ structure Date : DATE = = wrap (CInterface.c_function "SMLNJ-Date" "strfTime") (* conversions between integer numbers of seconds (used by runtime) and Time.time values *) - fun nsToTime s = Time.fromSeconds (Word64.toLargeInt s) + fun nsToTime s = Time.fromNanoseconds (Word64.toLargeInt s) fun timeToNs t = Word64.fromLargeInt (Time.toNanoseconds t) val localTime = localTime' o timeToNs Submitted via web form by Johannes 5 Joemann <joemann@beefree.free.de> 2019-12-21 18:58jhr
ResolutionNone2019-12-21 18:58jhr
Transcript (of reproduction)val t = TIME {usec=1576534090258702} : Time.time val s = 1576534090 : IntInf.int val d = DATE {day=16,hour=23,isDst=SOME false,minute=8,month=Dec, offset=SOME (TIME {usec=~3600000000}),second=10,wday=Mon,yday=349, year=2019} : Date.date val t' = TIME {usec=1576534089999996400000000} : Time.time val s' = 1576534089999996400 : IntInf.int val d' = DATE {day=11,hour=1,isDst=SOME false,minute=58,month=Feb, offset=SOME (TIME {usec=~3600000000}),second=9,wday=Tue,yday=41, year=2127} : Date.date val factor = 999999999 : IntInf.int val difference = ~1576534088423462310 : IntInf.int 2019-12-21 18:58jhr
Source (for reproduction)val t = Time.now (); val s = Time.toSeconds t; val d = Date.fromTimeLocal t; val t' = Date.toTime d; val s' = Time.toSeconds t'; val d' = Date.fromTimeLocal t'; val factor = IntInf.div (s', s); val difference = IntInf.- (s, s'); 2019-12-21 18:58jhr