1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
open Util
module Validable_option =
Preface.Option.Applicative.Traversable (Validate.Applicative)
type month =
| Jan
| Feb
| Mar
| Apr
| May
| Jun
| Jul
| Aug
| Sep
| Oct
| Nov
| Dec
type year = int
type day = int
type hour = int
type min = int
type sec = int
type t =
{ year : year
; month : month
; day : day
; time : (hour * min * sec) option
}
type day_of_week =
| Mon
| Tue
| Wed
| Thu
| Fri
| Sat
| Sun
let is_leap year =
if year mod 100 = 0 then year mod 400 = 0 else year mod 4 = 0
;;
let days_in_month year month =
match month with
| Jan | Mar | May | Jul | Aug | Oct | Dec -> 31
| Feb -> if is_leap year then 29 else 28
| _ -> 30
;;
let try_day year month day =
let days = days_in_month year month in
if day < 1 || day > days
then Try.error $ Error.Invalid_day day
else Try.ok day
;;
let try_year year =
if year < 0 then Try.error $ Error.Invalid_year year else Try.ok year
;;
let in_range min_bound_incl max_bound_excl x =
if x < min_bound_incl || x >= max_bound_excl
then Validate.error $ Error.Invalid_range (x, min_bound_incl, max_bound_excl)
else Validate.valid x
;;
let make_date year month day =
let open Try.Monad in
let aux =
let* y = try_year year in
let* d = try_day year month day in
return (y, month, d)
in
Validate.from_try aux
;;
let make_time (h, m, s) =
let open Validate.Applicative in
(fun h m s -> h, m, s)
<$> in_range 0 24 h
<*> in_range 0 60 m
<*> in_range 0 60 s
;;
let make ?time year month day =
let open Validate.Applicative in
(fun time (year, month, day) -> { year; month; day; time })
<$> (Validable_option.sequence $ Option.map make_time time)
<*> make_date year month day
;;
let month_equal x y =
match x, y with
| Jan, Jan
| Feb, Feb
| Mar, Mar
| Apr, Apr
| May, May
| Jun, Jun
| Jul, Jul
| Aug, Aug
| Sep, Sep
| Oct, Oct
| Nov, Nov
| Dec, Dec -> true
| _ -> false
;;
let month_to_int = function
| Jan -> 1
| Feb -> 2
| Mar -> 3
| Apr -> 4
| May -> 5
| Jun -> 6
| Jul -> 7
| Aug -> 8
| Sep -> 9
| Oct -> 10
| Nov -> 11
| Dec -> 12
;;
let pp_month ppf m =
Format.fprintf
ppf
"%s"
(match m with
| Jan -> "Jan"
| Feb -> "Feb"
| Mar -> "Mar"
| Apr -> "Apr"
| May -> "May"
| Jun -> "Jun"
| Jul -> "Jul"
| Aug -> "Aug"
| Sep -> "Sep"
| Oct -> "Oct"
| Nov -> "Nov"
| Dec -> "Dec")
;;
let month_to_string = Format.asprintf "%a" pp_month
let month_from_int x =
if x > 0 && x <= 12
then
Validate.valid
[| Jan; Feb; Mar; Apr; May; Jun; Jul; Aug; Sep; Oct; Nov; Dec |].(x - 1)
else Validate.error $ Error.Invalid_month x
;;
let equal a b =
Int.equal a.year b.year
&& month_equal a.month b.month
&& Int.equal a.day b.day
&& Option.equal
(fun (a, b, c) (x, y, z) ->
Int.equal a x && Int.equal b y && Int.equal c z)
a.time
b.time
;;
let compare a b =
let f (x, y, z) = (x * 10000) + (y * 100) + z in
let g x =
let a = f (x.year, month_to_int x.month, x.day)
and b = f $ Option.value ~default:(0, 0, 0) x.time in
(a * 1000000) + b
in
Int.compare (g a) (g b)
;;
let pp ppf t =
Format.fprintf
ppf
"%04d-%02d-%02d%s"
t.year
(month_to_int t.month)
t.day
(Option.fold
~none:""
~some:(fun (h, m, s) -> Format.asprintf " %02d:%02d:%02d" h m s)
t.time)
;;
let to_string = Format.asprintf "%a" pp
let from_string s =
try
Scanf.sscanf
(String.trim s)
"%04d-%02d-%02d %02d:%02d:%02d"
(fun y m d hour min sec ->
let open Validate.Monad in
month_from_int m >>= fun month -> make ~time:(hour, min, sec) y month d)
with
| _ ->
(try
Scanf.sscanf (String.trim s) "%04d-%02d-%02d" (fun y m d ->
let open Validate.Monad in
month_from_int m >>= fun month -> make y month d)
with
| _ -> Validate.error $ Error.Invalid_date s)
;;
let to_pair date = (date.year, date.month, date.day), date.time
let month_value = function
| Jan -> 0
| Feb -> 3
| Mar -> 3
| Apr -> 6
| May -> 1
| Jun -> 4
| Jul -> 6
| Aug -> 2
| Sep -> 5
| Oct -> 0
| Nov -> 3
| Dec -> 5
;;
let day_of_week date =
let d = date.day
and m = date.month
and y = date.year in
let yy = y mod 100 in
let cc = (y - yy) / 100 in
let c_code = [| 6; 4; 2; 0 |].(cc mod 4) in
let y_code = (yy + (yy / 4)) mod 7 in
let m_code =
let v = month_value m in
if is_leap y && (m = Jan || m = Feb) then v - 1 else v
in
let index = (c_code + y_code + m_code + d) mod 7 in
[| Sun; Mon; Tue; Wed; Thu; Fri; Sat |].(index)
;;
let pp_day_of_week ppf d =
Format.fprintf
ppf
"%s"
(match d with
| Mon -> "Mon"
| Tue -> "Tue"
| Wed -> "Wed"
| Thu -> "Thu"
| Fri -> "Fri"
| Sat -> "Sat"
| Sun -> "Sun")
;;
let day_of_week_to_string = Format.asprintf "%a" pp_day_of_week
let pp_time default ppf t =
let h, m, s = Option.value ~default t in
Format.fprintf ppf " %02d:%02d:%02d GMT" h m s
;;
let pp_rfc822 ?(default_time = 10, 0, 0) ppf t =
let dow = day_of_week t in
Format.fprintf
ppf
"%a, %02d %a %04d%a"
pp_day_of_week
dow
t.day
pp_month
t.month
t.year
(pp_time default_time)
t.time
;;