Mercurial > urweb
comparison lib/ur/datetime.ur @ 1988:abb6981a2c4c
Merge with small clean-ups
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Tue, 18 Feb 2014 07:07:01 -0500 |
parents | 50322ba22972 |
children |
comparison
equal
deleted
inserted
replaced
1970:6bea98c7f736 | 1988:abb6981a2c4c |
---|---|
1 datatype day_of_week = Sunday | Monday | Tuesday | Wednesday | Thursday | | |
2 Friday | Saturday | |
3 | |
4 val show_day_of_week = mkShow (fn dow => case dow of | |
5 Sunday => "Sunday" | |
6 | Monday => "Monday" | |
7 | Tuesday => "Tuesday" | |
8 | Wednesday => "Wednesday" | |
9 | Thursday => "Thursday" | |
10 | Friday => "Friday" | |
11 | Saturday => "Saturday") | |
12 | |
13 fun dayOfWeekToInt dow = case dow of | |
14 Sunday => 0 | |
15 | Monday => 1 | |
16 | Tuesday => 2 | |
17 | Wednesday => 3 | |
18 | Thursday => 4 | |
19 | Friday => 5 | |
20 | Saturday => 6 | |
21 | |
22 fun intToDayOfWeek i = case i of | |
23 0 => Sunday | |
24 | 1 => Monday | |
25 | 2 => Tuesday | |
26 | 3 => Wednesday | |
27 | 4 => Thursday | |
28 | 5 => Friday | |
29 | 6 => Saturday | |
30 | n => error <xml>Invalid day of week {[n]}</xml> | |
31 | |
32 val eq_day_of_week = mkEq (fn a b => dayOfWeekToInt a = dayOfWeekToInt b) | |
33 | |
34 | |
35 datatype month = January | February | March | April | May | June | July | | |
36 August | September | October | November | December | |
37 | |
38 val show_month = mkShow (fn m => case m of | |
39 January => "January" | |
40 | February => "February" | |
41 | March => "March" | |
42 | April => "April" | |
43 | May => "May" | |
44 | June => "June" | |
45 | July => "July" | |
46 | August => "August" | |
47 | September => "September" | |
48 | October => "October" | |
49 | November => "November" | |
50 | December => "December") | |
51 | |
52 type t = { | |
53 Year : int, | |
54 Month : month, | |
55 Day : int, | |
56 Hour : int, | |
57 Minute : int, | |
58 Second : int | |
59 } | |
60 | |
61 fun monthToInt m = case m of | |
62 January => 0 | |
63 | February => 1 | |
64 | March => 2 | |
65 | April => 3 | |
66 | May => 4 | |
67 | June => 5 | |
68 | July => 6 | |
69 | August => 7 | |
70 | September => 8 | |
71 | October => 9 | |
72 | November => 10 | |
73 | December => 11 | |
74 | |
75 fun intToMonth i = case i of | |
76 0 => January | |
77 | 1 => February | |
78 | 2 => March | |
79 | 3 => April | |
80 | 4 => May | |
81 | 5 => June | |
82 | 6 => July | |
83 | 7 => August | |
84 | 8 => September | |
85 | 9 => October | |
86 | 10 => November | |
87 | 11 => December | |
88 | n => error <xml>Invalid month number {[n]}</xml> | |
89 | |
90 val eq_month = mkEq (fn a b => monthToInt a = monthToInt b) | |
91 | |
92 | |
93 fun toTime dt : time = fromDatetime dt.Year (monthToInt dt.Month) dt.Day | |
94 dt.Hour dt.Minute dt.Second | |
95 | |
96 fun fromTime t : t = { | |
97 Year = datetimeYear t, | |
98 Month = intToMonth (datetimeMonth t), | |
99 Day = datetimeDay t, | |
100 Hour = datetimeHour t, | |
101 Minute = datetimeMinute t, | |
102 Second = datetimeSecond t | |
103 } | |
104 | |
105 val ord_datetime = mkOrd { Lt = fn a b => toTime a < toTime b, | |
106 Le = fn a b => toTime a <= toTime b } | |
107 | |
108 fun format fmt dt : string = timef fmt (toTime dt) | |
109 | |
110 fun dayOfWeek dt : day_of_week = intToDayOfWeek (datetimeDayOfWeek (toTime dt)) | |
111 | |
112 val now : transaction t = | |
113 n <- now; | |
114 return (fromTime n) | |
115 | |
116 (* Normalize a datetime. This will convert, e.g., January 32nd into February | |
117 1st. *) | |
118 | |
119 fun normalize dt = fromTime (toTime dt) | |
120 fun addToField [nm :: Name] [rest ::: {Type}] [[nm] ~ rest] | |
121 (delta : int) (r : $([nm = int] ++ rest)) | |
122 : $([nm = int] ++ rest) = | |
123 (r -- nm) ++ {nm = r.nm + delta} | |
124 | |
125 | |
126 (* Functions for adding to a datetime. There is no addMonths or addYears since | |
127 it's not clear what should be done; what's 1 month after January 31, or 1 | |
128 year after February 29th? | |
129 | |
130 These can't all be defined in terms of addSeconds because of leap seconds. *) | |
131 | |
132 fun addSeconds n dt = normalize (addToField [#Second] n dt) | |
133 fun addMinutes n dt = normalize (addToField [#Minute] n dt) | |
134 fun addHours n dt = normalize (addToField [#Hour] n dt) | |
135 fun addDays n dt = normalize (addToField [#Day] n dt) |