[MLton-commit] r4420

Matthew Fluet MLton@mlton.org
Tue, 25 Apr 2006 14:02:36 -0700


Refactor Date and Time
----------------------------------------------------------------------

U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/build/sources.mlb
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/system/date.sml
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/system/pre-os.sml
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/system/time.sml

----------------------------------------------------------------------

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/build/sources.mlb
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/build/sources.mlb	2006-04-25 20:25:43 UTC (rev 4419)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/build/sources.mlb	2006-04-25 21:02:35 UTC (rev 4420)
@@ -196,13 +196,12 @@
    ../util/cleaner.sig
    ../util/cleaner.sml
 
+   ../system/pre-os.sml
+   ../system/time.sig
+   ../system/time.sml
+   ../system/date.sig
+   ../system/date.sml
 (*
-      ../../system/pre-os.sml
-      ../../system/time.sig
-      ../../system/time.sml
-      ../../system/date.sig
-      ../../system/date.sml
-
       ../../io/io.sig
       ../../io/io.sml
       ../../io/prim-io.sig

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/system/date.sml
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/system/date.sml	2006-04-25 20:25:43 UTC (rev 4419)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/system/date.sml	2006-04-25 21:02:35 UTC (rev 4420)
@@ -1,4 +1,5 @@
 (* Modified from the ML Kit 4.1.4; basislib/Date.sml
+ * by mfluet@acm.org on 2006-4-25
  * by mfluet@acm.org on 2005-8-10 based on
  *  modifications from the ML Kit Version 3; basislib/Date.sml
  *  by sweeks@research.nj.nec.com on 1999-1-3 and
@@ -59,18 +60,17 @@
 
     (* 86400 = 24*60*6 is the number of seconds per day *)
 
-    type tmoz = {tm_hour   : int,
-                 tm_isdst  : int,       (* 0 = no, 1 = yes, ~1 = don't know *)
-                 tm_mday   : int,
-                 tm_min    : int,
-                 tm_mon    : int,
-                 tm_sec    : int, 
-                 tm_wday   : int,
-                 tm_yday   : int,
-                 tm_year   : int}
-
+    type tmoz = {tm_hour  : C_Int.t,
+                 tm_isdst : C_Int.t,       (* 0 = no, 1 = yes, ~1 = don't know *)
+                 tm_mday  : C_Int.t,
+                 tm_min   : C_Int.t,
+                 tm_mon   : C_Int.t,
+                 tm_sec   : C_Int.t, 
+                 tm_wday  : C_Int.t,
+                 tm_yday  : C_Int.t,
+                 tm_year  : C_Int.t}
     local
-       fun make (f: int ref -> int) (n: int): tmoz =
+       fun make (f: C_Time.t ref -> C_Int.t C_Errno.t) (n: C_Time.t) : tmoz =
           (ignore (f (ref n))
            ; {tm_hour = Tm.getHour (),
               tm_isdst = Tm.getIsDst (),
@@ -86,8 +86,8 @@
        val getgmtime_ = make Prim.gmTime
     end
 
-    fun setTmBuf {tm_hour, tm_isdst, tm_mday, tm_min, tm_mon, tm_sec, tm_wday,
-                 tm_yday, tm_year} =
+    fun setTmBuf ({tm_hour, tm_isdst, tm_mday, tm_min, tm_mon, 
+                   tm_sec, tm_wday, tm_yday, tm_year}: tmoz) : unit =
        (Tm.setHour tm_hour
         ; Tm.setIsDst tm_isdst
         ; Tm.setMDay tm_mday
@@ -98,10 +98,10 @@
         ; Tm.setYDay tm_yday
         ; Tm.setYear tm_year)
         
-    fun mktime_ (t: tmoz): int = (setTmBuf t; Prim.mkTime ())
+    fun mktime_ (t: tmoz): C_Time.t = (setTmBuf t; Prim.mkTime ())
 
     (* The offset to add to local time to get UTC: positive West of UTC *)
-    val localoffset: int = Real.round (Prim.localOffset ())
+    val localoffset: int = C_Double.round (Prim.localOffset ())
 
     val toweekday: int -> weekday =
        fn 0 => Sun | 1 => Mon | 2 => Tue | 3 => Wed
@@ -123,21 +123,21 @@
         | May => 4 | Jun => 5 | Jul => 6  | Aug => 7
         | Sep => 8 | Oct => 9 | Nov => 10 | Dec => 11
         
-    fun tmozToDate ({tm_hour, tm_isdst, tm_mday, tm_min, tm_mon, tm_sec,
-                    tm_wday, tm_yday, tm_year}: tmoz) offset = 
-       T {day = tm_mday,
-          hour = tm_hour,
+    fun tmozToDate ({tm_hour, tm_isdst, tm_mday, tm_min, tm_mon, 
+                     tm_sec, tm_wday, tm_yday, tm_year}: tmoz) offset = 
+       T {day = C_Int.toInt tm_mday,
+          hour = C_Int.toInt tm_hour,
           isDst = (case tm_isdst of
                       0 => SOME false 
                     | 1 => SOME true
                     | _ => NONE),
-          minute = tm_min, 
-          month = tomonth tm_mon, 
+          minute = C_Int.toInt tm_min, 
+          month = tomonth (C_Int.toInt tm_mon), 
           offset = offset,
-          second = tm_sec,
-          weekDay = toweekday tm_wday,
-          year = tm_year + 1900,
-          yearDay = tm_yday}
+          second = C_Int.toInt tm_sec,
+          weekDay = toweekday (C_Int.toInt tm_wday),
+          yearDay = C_Int.toInt tm_yday,
+          year = (C_Int.toInt tm_year) + 1900}
 
     fun leapyear (y: int) =
        y mod 4 = 0 andalso y mod 100 <> 0 orelse y mod 400 = 0   
@@ -170,18 +170,18 @@
                              weekDay, yearDay, isDst, ...}): tmoz =
         if not (okDate dt)
            then raise Date
-        else {tm_hour = hour,
-              tm_mday = day,
-              tm_min = minute, 
-              tm_mon = frommonth month,
-              tm_sec = second, 
-              tm_year = year -? 1900, 
+        else {tm_hour = C_Int.fromInt hour,
               tm_isdst = (case isDst of
                              SOME false => 0
                            | SOME true => 1
                            | NONE=> ~1),
-              tm_wday = fromwday weekDay,
-              tm_yday = yearDay}
+              tm_mday = C_Int.fromInt day,
+              tm_min = C_Int.fromInt minute, 
+              tm_mon = C_Int.fromInt (frommonth month),
+              tm_sec = C_Int.fromInt second, 
+              tm_wday = C_Int.fromInt (fromwday weekDay),
+              tm_yday = C_Int.fromInt yearDay,
+              tm_year = C_Int.fromInt (year - 1900)}
 
     (* -------------------------------------------------- *)
     (* Translated from Emacs's calendar.el:               *)
@@ -279,10 +279,10 @@
         end
 
     fun fromTimeLocal t = 
-        tmozToDate (getlocaltime_ (Time.toSeconds t)) NONE
+        tmozToDate (getlocaltime_ (C_Time.fromInt (Time.toSeconds t))) NONE
 
     fun fromTimeUniv t = 
-        tmozToDate (getgmtime_ (Time.toSeconds t)) (SOME 0)
+        tmozToDate (getgmtime_ (C_Time.fromInt (Time.toSeconds t))) (SOME 0)
 
     (* The following implements conversion from a local date to 
      * a Time.time.  It IGNORES wday and yday.
@@ -294,7 +294,7 @@
               case offset of
                  NONE      => 0
                | SOME secs => localoffset + secs
-            val clock = mktime_ (dateToTmoz date) - secoffset
+            val clock = C_Time.toInt (mktime_ (dateToTmoz date)) - secoffset
         in
             if clock < 0 then raise Date
             else Time.fromSeconds clock
@@ -307,7 +307,7 @@
           let
              val a = Array.tabulate (Char.maxOrd + 1, fn _ => false)
              val validChars = "aAbBcdHIjmMpSUwWxXyYZ%"
-          in Util.naturalForeach
+          in Natural.foreach
              (size validChars, fn i =>
               Array.update (a, Char.ord (String.sub (validChars, i)), true));
              fn c => Array.sub (a, Char.ord c)
@@ -317,14 +317,14 @@
           let
              val _ = setTmBuf (dateToTmoz d)
              val bufLen = 50 (* more than enough for a single format char *)
-             val buf = Primitive.Array.array bufLen
+             val buf = Array.arrayUninit bufLen
              fun strftime fmtChar =
                 let
                    val len =
                       Prim.strfTime
-                      (buf, Word.fromInt bufLen,
+                      (buf, C_Size.fromInt bufLen,
                        NullString.fromString (concat ["%", str fmtChar, "\000"]))
-                   val len = Word.toInt len
+                   val len = C_Size.toInt len
                 in if len = 0
                       then raise Fail "Date.fmt"
                    else ArraySlice.vector (ArraySlice.slice (buf, 0, SOME len))

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/system/pre-os.sml
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/system/pre-os.sml	2006-04-25 20:25:43 UTC (rev 4419)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/system/pre-os.sml	2006-04-25 21:02:35 UTC (rev 4420)
@@ -17,12 +17,12 @@
                          val fromFD: C_Fd.t -> iodesc
                          val toFD: iodesc -> C_Fd.t
                       end = 
-                      struct
-                         type iodesc = C_Fd.t
+         struct
+            type iodesc = C_Fd.t
 
-                         val fromFD = fn z => z
-                         val toFD = fn z => z
-                      end
+            val fromFD = fn z => z
+            val toFD = fn z => z
+         end
    end
 
 structure PreOS = OS

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/system/time.sml
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/system/time.sml	2006-04-25 20:25:43 UTC (rev 4419)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/system/time.sml	2006-04-25 21:02:35 UTC (rev 4420)
@@ -23,12 +23,13 @@
 val zeroTime = T 0
 
 fun fromReal r =
-   T (Real.toLargeInt IEEEReal.TO_NEAREST 
-      (Real.* (r, Real.fromLargeInt ticksPerSecond)))
+   T (LargeReal.toLargeInt IEEEReal.TO_NEAREST 
+      (LargeReal.* (r, LargeReal.fromLargeInt ticksPerSecond)))
    handle Overflow => raise Time
 
 fun toReal (T i) =
-   Real./ (Real.fromLargeInt i, Real.fromLargeInt ticksPerSecond)
+   LargeReal./ (LargeReal.fromLargeInt i, 
+                LargeReal.fromLargeInt ticksPerSecond)
 
 local
    fun make ticksPer =
@@ -87,7 +88,7 @@
 end
 
 val fmt: int -> time -> string =
-   fn n => (Real.fmt (StringCvt.FIX (SOME n))) o toReal
+   fn n => (LargeReal.fmt (StringCvt.FIX (SOME n))) o toReal
 
 val toString = fmt 3