Source file fmt_datetime.ml
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
(** Format validators: date-time, date, time (RFC 3339). *)
let is_leap_year y = (y mod 4 = 0 && y mod 100 <> 0) || y mod 400 = 0
let days_in_month y m =
match m with
| 1 | 3 | 5 | 7 | 8 | 10 | 12 -> 31
| 4 | 6 | 9 | 11 -> 30
| 2 -> if is_leap_year y then 29 else 28
| _ -> 0
let is_digit c = c >= '0' && c <= '9'
let parse_two_digits s i =
if i + 1 >= String.length s then None
else if is_digit s.[i] && is_digit s.[i + 1] then
Some
(((Char.code s.[i] - Char.code '0') * 10)
+ Char.code s.[i + 1]
- Char.code '0')
else None
let parse_four_digits s i =
if i + 3 >= String.length s then None
else if
is_digit s.[i]
&& is_digit s.[i + 1]
&& is_digit s.[i + 2]
&& is_digit s.[i + 3]
then
Some
(((Char.code s.[i] - Char.code '0') * 1000)
+ ((Char.code s.[i + 1] - Char.code '0') * 100)
+ ((Char.code s.[i + 2] - Char.code '0') * 10)
+ Char.code s.[i + 3]
- Char.code '0')
else None
let parse_date_parts s =
if String.length s <> 10 || s.[4] <> '-' || s.[7] <> '-' then None
else
match
(parse_four_digits s 0, parse_two_digits s 5, parse_two_digits s 8)
with
| Some y, Some m, Some d ->
if m >= 1 && m <= 12 && d >= 1 && d <= days_in_month y m then
Some (y, m, d)
else None
| _ -> None
let parse_secfrac s i =
let len = String.length s in
if i >= len || s.[i] <> '.' then Some i
else if i + 1 >= len || not (is_digit s.[i + 1]) then None
else
let j = ref (i + 2) in
while !j < len && is_digit s.[!j] do
incr j
done;
Some !j
let check_date s =
match parse_date_parts s with
| Some _ -> true
| None -> false
type time_parts = {
hour : int;
minute : int;
second : int;
offset_sign : int;
offset_hour : int;
offset_minute : int;
}
let parse_time_parts s =
let len = String.length s in
if len < 9 || s.[2] <> ':' || s.[5] <> ':' then None
else
match
(parse_two_digits s 0, parse_two_digits s 3, parse_two_digits s 6)
with
| Some hour, Some minute, Some second
when hour <= 23 && minute <= 59 && second <= 60 -> (
match parse_secfrac s 8 with
| None -> None
| Some i ->
if i < len && (s.[i] = 'Z' || s.[i] = 'z') && i = len - 1 then
Some
{
hour;
minute;
second;
offset_sign = 1;
offset_hour = 0;
offset_minute = 0;
}
else if
i + 5 < len && (s.[i] = '+' || s.[i] = '-') && s.[i + 3] = ':'
then
match
(parse_two_digits s (i + 1), parse_two_digits s (i + 4))
with
| Some offset_hour, Some offset_minute
when i + 6 = len && offset_hour <= 23 && offset_minute <= 59 ->
Some
{
hour;
minute;
second;
offset_sign = (if s.[i] = '+' then 1 else -1);
offset_hour;
offset_minute;
}
| _ -> None
else None)
| _ -> None
let utc_minute_and_day_delta time =
let local_minutes = (time.hour * 60) + time.minute in
let offset_minutes =
time.offset_sign * ((time.offset_hour * 60) + time.offset_minute)
in
let utc_minutes = local_minutes - offset_minutes in
if utc_minutes < 0 then (utc_minutes + 1440, -1)
else if utc_minutes >= 1440 then (utc_minutes - 1440, 1)
else (utc_minutes, 0)
let leap_second_time_ok time =
if time.second < 60 then true
else
let utc_minutes, _day_delta = utc_minute_and_day_delta time in
utc_minutes = 1439
let previous_date y m d =
if d > 1 then (y, m, d - 1)
else if m > 1 then
let previous_month = m - 1 in
(y, previous_month, days_in_month y previous_month)
else (y - 1, 12, 31)
let next_date y m d =
if d < days_in_month y m then (y, m, d + 1)
else if m < 12 then (y, m + 1, 1)
else (y + 1, 1, 1)
let shift_date_by_one_day y m d = function
| -1 -> previous_date y m d
| 0 -> (y, m, d)
| 1 -> next_date y m d
| _ -> invalid_arg "date-time offset day delta must be -1, 0, or 1"
let is_known_positive_leap_second_utc_date = function
| 1972, 6, 30
| 1972, 12, 31
| 1973, 12, 31
| 1974, 12, 31
| 1975, 12, 31
| 1976, 12, 31
| 1977, 12, 31
| 1978, 12, 31
| 1979, 12, 31
| 1981, 6, 30
| 1982, 6, 30
| 1983, 6, 30
| 1985, 6, 30
| 1987, 12, 31
| 1989, 12, 31
| 1990, 12, 31
| 1992, 6, 30
| 1993, 6, 30
| 1994, 6, 30
| 1995, 12, 31
| 1997, 6, 30
| 1998, 12, 31
| 2005, 12, 31
| 2008, 12, 31
| 2012, 6, 30
| 2015, 6, 30
| 2016, 12, 31 ->
true
| _ -> false
let leap_second_datetime_ok (y, m, d) time =
if time.second < 60 then true
else
let utc_minutes, day_delta = utc_minute_and_day_delta time in
utc_minutes = 1439
&& is_known_positive_leap_second_utc_date
(shift_date_by_one_day y m d day_delta)
let check_time s =
match parse_time_parts s with
| Some time -> leap_second_time_ok time
| None -> false
let check_datetime s =
let len = String.length s in
if len < 20 then false
else
let t_pos = 10 in
if s.[t_pos] <> 'T' && s.[t_pos] <> 't' then false
else
let date_part = String.sub s 0 10 in
let time_part = String.sub s 11 (len - 11) in
match (parse_date_parts date_part, parse_time_parts time_part) with
| Some date, Some time -> leap_second_datetime_ok date time
| _ -> false
let parse_number s i =
let len = String.length s in
let j = ref i in
while !j < len && is_digit s.[!j] do
incr j
done;
if !j = i then None else Some !j
let parse_component s i unit_char =
match parse_number s i with
| Some j when j < String.length s && s.[j] = unit_char -> Some (j + 1)
| _ -> None
let check_duration s =
let len = String.length s in
if len < 3 || s.[0] <> 'P' then false
else if not (String.for_all (fun c -> Char.code c < 128) s) then false
else
match parse_component s 1 'W' with
| Some j -> j = len
| None ->
let i = ref 1 in
let saw_date = ref false in
let y =
match parse_component s !i 'Y' with
| Some j ->
i := j;
saw_date := true;
true
| None -> false
in
let m =
match parse_component s !i 'M' with
| Some j ->
i := j;
saw_date := true;
true
| None -> false
in
let d =
match parse_component s !i 'D' with
| Some j ->
i := j;
saw_date := true;
true
| None -> false
in
let date_ok = not (y && d && not m) in
if not date_ok then false
else if !i = len then !saw_date
else if s.[!i] <> 'T' then false
else begin
incr i;
let saw_time = ref false in
let h =
match parse_component s !i 'H' with
| Some j ->
i := j;
saw_time := true;
true
| None -> false
in
let tm =
match parse_component s !i 'M' with
| Some j ->
i := j;
saw_time := true;
true
| None -> false
in
let sec =
match parse_component s !i 'S' with
| Some j ->
i := j;
saw_time := true;
true
| None -> false
in
let time_ok = not (h && sec && not tm) in
!i = len && !saw_time && time_ok && (!saw_date || !saw_time)
end