1(* test/date.sml 2 PS 1995-03-20, 1995-05-12, 1996-07-05, 1998-04-07, 1999-08-04 3*) 4 5use "auxil.sml"; 6 7local 8 open Time Date 9 fun later h = 10 toString(fromTimeLocal(now() + fromReal (3600.0 * real h))) ^ "\n"; 11 fun nowdate () = Date.fromTimeLocal(now()); 12 fun mkdate(y,mo,d,h,mi,s) = 13 date {year=y, month=mo, day=d, hour=h, minute=mi, second=s, 14 offset = NONE} 15 fun cmp(dt1, dt2) = compare(mkdate dt1, mkdate dt2) 16 17 fun fromto dt = 18 toString (valOf (fromString (toString dt))) = toString dt 19 20 fun tofrom s = 21 toString (valOf (fromString s)) = s 22 23 (* The millenium at UTC *) 24 val y2k = 25 date {year=2000, month=Jan, day=1, hour=0, minute=0, second=0, 26 offset = SOME Time.zeroTime} 27 28 (* The millenium at UTC+0100 i.e. East of UTC *) 29 val y2kE1 = 30 date {year=2000, month=Jan, day=1, hour=0, minute=0, second=0, 31 offset = SOME (Time.fromSeconds 82800) } 32 33 (* The millenium at UTC-0100 i.e. West of UTC *) 34 val y2kW1 = 35 date {year=2000, month=Jan, day=1, hour=0, minute=0, second=0, 36 offset = SOME (Time.fromSeconds 3600) } 37in 38 39val _ = 40 (print "This is (local time) now: "; print (later 0); 41 print "This is UTC now: "; 42 print (toString (fromTimeUniv(now()))); print "\n"; 43 print "This is an hour from now: "; print (later 1); 44 print "This is a day from now: "; print (later 24); 45 print "This is a week from now: "; print (later 168); 46 print "This is 120 days from now: "; print (later (24 * 120)); 47 print "This is 160 days from now: "; print (later (24 * 160)); 48 print "This is 200 days from now: "; print (later (24 * 200)); 49 print "This is 240 days from now: "; print (later (24 * 240)); 50 print "This is the epoch (UTC): "; 51 print (toString(fromTimeUniv zeroTime) ^ "\n"); 52 print "The UTC millenium (UTC time): "; 53 print (toString y2k ^ "\n"); 54 print "The UTC millenium (UTC time): "; 55 print (toString (fromTimeUniv (toTime y2k)) ^ "\n"); 56 print "The UTC millenium minus 5 sec: "; 57 print (toString (date {year=2000, month=Jan, day=1, hour=0, 58 minute=0, second= ~5, offset = SOME Time.zeroTime}) 59 ^ "\n"); 60 print "The UTC millenium (local time): "; 61 print (toString (fromTimeLocal (toTime y2k)) ^ "\n"); 62 print "The local millenium (UTC time): "; 63 print (toString (fromTimeUniv (toTime (mkdate(2000, Jan, 1, 0, 0, 0)))) 64 ^ "\n"); 65 print "The UTC+01 millenium (UTC): "; 66 print (toString (fromTimeUniv (toTime y2kE1)) ^ "\n"); 67 print "The UTC-01 millenium (UTC): "; 68 print (toString (fromTimeUniv (toTime y2kW1)) ^ "\n"); 69 print "This is today's number: "; 70 print (fmt "%j" (nowdate()) ^ " (internally: "); 71 print (Int.toString (yearDay (nowdate())) ^ ")\n"); 72 print "This is today's weekday: "; 73 print (fmt "%A" (nowdate()) ^ "\n"); 74 print "This is the name of this month: "; 75 print (fmt "%B" (nowdate()) ^ "\n"); 76 print "Today's ISO date: "; 77 print (fmt "%Y-%m-%d" (nowdate ()) ^ "\n")) 78 79 80val test1 = 81check'(fn _ => 82 cmp((1993,Jul,25,16,12,18), (1994,Jun,25,16,12,18)) = LESS 83 andalso cmp((1995,May,25,16,12,18), (1994,Jun,25,16,12,18)) = GREATER 84 andalso cmp((1994,May,26,16,12,18), (1994,Jun,25,16,12,18)) = LESS 85 andalso cmp((1994,Jul,24,16,12,18), (1994,Jun,25,16,12,18)) = GREATER 86 andalso cmp((1994,Jun,24,17,12,18), (1994,Jun,25,16,12,18)) = LESS 87 andalso cmp((1994,Jun,26,15,12,18), (1994,Jun,25,16,12,18)) = GREATER 88 andalso cmp((1994,Jun,25,15,13,18), (1994,Jun,25,16,12,18)) = LESS 89 andalso cmp((1994,Jun,25,17,11,18), (1994,Jun,25,16,12,18)) = GREATER 90 andalso cmp((1994,Jun,25,16,11,19), (1994,Jun,25,16,12,18)) = LESS 91 andalso cmp((1994,Jun,25,16,13,17), (1994,Jun,25,16,12,18)) = GREATER 92 andalso cmp((1994,Jun,25,16,12,17), (1994,Jun,25,16,12,18)) = LESS 93 andalso cmp((1994,Jun,25,16,12,19), (1994,Jun,25,16,12,18)) = GREATER 94 andalso cmp((1994,Jun,25,16,12,18), (1994,Jun,25,16,12,18)) = EQUAL); 95 96val test2 = 97 check'(fn _ => 98 fmt "%A" (mkdate(1995,May,22,4,0,1)) = "Monday"); 99 100val test3 = 101 check'(fn _ => 102 List.all fromto 103 [mkdate(1995,Aug,22,4,0,1), 104 mkdate(1996,Apr, 5, 0, 7, 21), 105 mkdate(1996,Mar, 5, 6, 13, 58)]); 106 107val test4 = 108 check'(fn _ => 109 List.all tofrom 110 ["Fri Jul 5 14:25:16 1996", 111 "Mon Feb 5 04:25:16 1996", 112 "Sat Jan 6 04:25:16 1996"]) 113 114val test5 = 115 check'(fn _ => 116 weekDay(mkdate(1962, Jun, 25, 1, 2, 3)) = Mon 117 andalso weekDay(mkdate(1998, Mar, 6, 1, 2, 3)) = Fri 118 andalso weekDay(mkdate(1998, Apr, 6, 1, 2, 3)) = Mon 119 andalso weekDay(mkdate(1900, Feb, 28, 1, 2, 3)) = Wed 120 andalso weekDay(mkdate(1900, Mar, 1, 1, 2, 3)) = Thu 121 andalso weekDay(mkdate(1850, Feb, 28, 1, 2, 3)) = Thu 122 andalso weekDay(mkdate(1850, Mar, 1, 1, 2, 3)) = Fri 123 andalso weekDay(mkdate(1860, Feb, 28, 1, 2, 3)) = Tue 124 andalso weekDay(mkdate(1860, Feb, 29, 1, 2, 3)) = Wed 125 andalso weekDay(mkdate(1860, Mar, 1, 1, 2, 3)) = Thu 126 andalso weekDay(mkdate(2000, Feb, 28, 1, 2, 3)) = Mon 127 andalso weekDay(mkdate(2000, Feb, 29, 1, 2, 3)) = Tue 128 andalso weekDay(mkdate(2000, Mar, 1, 1, 2, 3)) = Wed) 129 130val test6 = 131 check'(fn _ => 132 yearDay(mkdate(1962, Jan, 1, 1, 2, 3)) = 0 133 andalso yearDay(mkdate(1998, Mar, 6, 1, 2, 3)) = 64 134 andalso yearDay(mkdate(1900, Feb, 28, 1, 2, 3)) = 58 135 andalso yearDay(mkdate(1900, Mar, 1, 1, 2, 3)) = 59 136 andalso yearDay(mkdate(1900, Dec, 31, 1, 2, 3)) = 364 137 andalso yearDay(mkdate(1850, Feb, 28, 1, 2, 3)) = 58 138 andalso yearDay(mkdate(1850, Mar, 1, 1, 2, 3)) = 59 139 andalso yearDay(mkdate(1850, Dec, 31, 1, 2, 3)) = 364 140 andalso yearDay(mkdate(1860, Feb, 28, 1, 2, 3)) = 58 141 andalso yearDay(mkdate(1860, Feb, 29, 1, 2, 3)) = 59 142 andalso yearDay(mkdate(1860, Mar, 1, 1, 2, 3)) = 60 143 andalso yearDay(mkdate(1860, Dec, 31, 1, 2, 3)) = 365 144 andalso yearDay(mkdate(2000, Feb, 28, 1, 2, 3)) = 58 145 andalso yearDay(mkdate(2000, Feb, 29, 1, 2, 3)) = 59 146 andalso yearDay(mkdate(2000, Mar, 1, 1, 2, 3)) = 60 147 andalso yearDay(mkdate(2000, Dec, 31, 1, 2, 3)) = 365 148 andalso yearDay(mkdate(1959, Feb, 28, 1, 2, 3)) = 58 149 andalso yearDay(mkdate(1959, Mar, 1, 1, 2, 3)) = 59 150 andalso yearDay(mkdate(1959, Dec, 31, 1, 2, 3)) = 364 151 andalso yearDay(mkdate(1960, Feb, 28, 1, 2, 3)) = 58 152 andalso yearDay(mkdate(1960, Feb, 29, 1, 2, 3)) = 59 153 andalso yearDay(mkdate(1960, Mar, 1, 1, 2, 3)) = 60 154 andalso yearDay(mkdate(1960, Dec, 31, 1, 2, 3)) = 365) 155 156fun addh h = 157 let val dt = mkdate(1998, Apr, 6, h, 0, 0) 158 in (month dt, day dt, hour dt) end 159 160val test7 = 161 check'(fn _ => 162 addh 0 = (Apr, 6, 0) 163 andalso addh 23 = (Apr, 6, 23) 164 andalso addh 24 = (Apr, 7, 0) 165 andalso addh 36 = (Apr, 7, 12) 166 andalso addh 600 = (May, 1, 0) 167 andalso addh 610 = (May, 1, 10) 168 andalso addh 625 = (May, 2, 1)) 169 170val test8 = 171 check'(fn _ => 172 hour (mkdate(1998, Mar, 28, 12, 0, 0)) = 12 173 andalso hour (mkdate(1998, Mar, 28, 36, 0, 0)) = 12) 174 175val test9 = 176 check'(fn _ => 177 SOME (Time.fromSeconds 82800) = offset y2kE1 178 andalso SOME (Time.fromSeconds 3600) = offset y2kW1) 179 180end 181