Source file time_zone.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
include Timedesc_tzdb
type record = {
recorded_offsets : int array;
table : table;
}
type typ =
| Backed of string
| Offset_only of int
type t = {
typ : typ;
record : record;
}
type 'a local_result =
[ `None
| `Single of 'a
| `Ambiguous of 'a * 'a
]
let check_table ((starts, entries) : table) : bool =
let size = Bigarray.Array1.dim starts in
assert (size = Array.length entries);
let has_no_dup =
let seen = ref Int64_set.empty in
let has_no_dup = ref true in
let i = ref 0 in
while !has_no_dup && !i < size do
let start = starts.{!i} in
if Int64_set.mem start !seen then has_no_dup := false
else seen := Int64_set.add start !seen;
i := !i + 1
done;
!has_no_dup
in
let is_sorted =
let i = ref 0 in
let is_sorted = ref true in
while !is_sorted && !i < size do
(if !i > 0 then
let cur = starts.{!i} in
let prev = starts.{!i - 1} in
if cur < prev then is_sorted := false);
i := !i + 1
done;
!is_sorted
in
has_no_dup && is_sorted
let process_table ((starts, entries) : table) : record =
let size = Bigarray.Array1.dim starts in
assert (size = Array.length entries);
if size = 0 then failwith "Time zone record table is empty"
else
let starts, entries =
let first_start = starts.{0} in
let first_entry = entries.(0) in
if Constants.timestamp_min.s < first_start then (
let starts' =
Bigarray.Array1.create Bigarray.Int64 Bigarray.c_layout (size + 1)
in
let sub = Bigarray.Array1.sub starts' 1 size in
starts'.{0} <- Constants.timestamp_min.s;
Bigarray.Array1.blit starts sub;
(starts', Array.append [| first_entry |] entries))
else (
starts.{0} <- Constants.timestamp_min.s;
(starts, entries))
in
let recorded_offsets =
Array.fold_left
(fun acc entry -> Int_set.add entry.offset acc)
Int_set.empty entries
|> Int_set.to_list
|> CCArray.of_list
in
{ recorded_offsets; table = (starts, entries) }
let lookup_record name : record option =
M.find_opt name db
|> CCOpt.map (fun table ->
assert (check_table table);
process_table table)
let name t =
match t.typ with
| Backed name -> name
| Offset_only s ->
let dur = Span.(make_small ~s () |> For_human'.view) in
Printf.sprintf "UTC%c%02d:%02d"
(match dur.sign with `Pos -> '+' | `Neg -> '-')
dur.hours dur.minutes
let to_fixed_offset_from_utc t =
match t.typ with
| Backed _ -> None
| Offset_only s -> Some (Span.make_small ~s ())
let equal t1 t2 =
(match (t1.typ, t2.typ) with
| Backed name1, Backed name2 -> CCString.equal name1 name2
| Offset_only s1, Offset_only s2 -> CCInt.equal s1 s2
| _, _ -> false)
&& Bigarray.Array1.dim (fst t1.record.table)
= Bigarray.Array1.dim (fst t2.record.table)
&& Array.length (snd t1.record.table) = Array.length (snd t2.record.table)
&& CCArray.for_all2
(fun e1 e2 -> e1 = e2)
(snd t1.record.table) (snd t2.record.table)
let fixed_offset_name_parser =
let open MParser in
let open Parser_components in
string "UTC"
>> (char '+' >> return `Pos <|> (char '-' >> return `Neg))
>>= fun sign ->
attempt
(max_two_digit_nat_zero
>>= fun hour ->
char ':' >> max_two_digit_nat_zero >>= fun minute -> return (hour, minute))
<|> (max_two_digit_nat_zero >>= fun hour -> return (hour, 0))
>>= fun (hour, minute) ->
if hour < 24 && minute < 60 then
return (Span.For_human'.make_exn ~sign ~hours:hour ~minutes:minute ())
else fail "Invalid offset"
let fixed_offset_of_name (s : string) : Span.t option =
match
Parser_components.result_of_mparser_result
@@ MParser.(parse_string (fixed_offset_name_parser << eof)) s ()
with
| Ok dt -> Some dt
| Error _ -> None
let one_day = Span.For_human'.(make_exn ~days:1 ())
let make_offset_only (offset : Span.t) =
if Span.abs offset > one_day then None
else
let offset = CCInt64.to_int offset.s in
Some
{
typ = Offset_only offset;
record =
process_table
( Bigarray.Array1.of_array Bigarray.Int64 Bigarray.C_layout
[| Constants.timestamp_min.s |],
[| { is_dst = false; offset } |] );
}
let make_offset_only_exn offset =
match make_offset_only offset with
| None -> invalid_arg "make_offset_only_span_exn"
| Some x -> x
let utc : t =
{
typ = Backed "UTC";
record =
process_table
( Bigarray.Array1.of_array Bigarray.Int64 Bigarray.C_layout
[| Constants.timestamp_min.s |],
[| { is_dst = false; offset = 0 } |] );
}
let make name : t option =
if name = "UTC" then Some utc
else
match fixed_offset_of_name name with
| Some fixed -> make_offset_only fixed
| None -> (
match lookup_record name with
| Some record -> Some { typ = Backed name; record }
| None -> None)
let make_exn name : t =
match make name with Some x -> x | None -> invalid_arg "make_exn"
let bsearch_table timestamp ((starts, _) : table) =
Bigarray_utils.bsearch ~cmp:Int64.compare timestamp starts
let lookup_timestamp_utc (t : t) timestamp =
let table = t.record.table in
let entries = snd table in
match bsearch_table timestamp table with
| `At i -> Some entries.(i)
| `All_lower -> Some entries.(Array.length entries - 1)
| `All_bigger -> None
| `Just_after i -> Some entries.(i)
| `Empty -> None
let local_interval_of_table ((starts, entries) : table) (i : int) =
let size = Bigarray.Array1.dim starts in
let start_utc = starts.{i} in
let entry = entries.(i) in
let end_exc_utc =
if i = size - 1 then Constants.timestamp_max.s else starts.{i + 1}
in
( Int64.add start_utc (Int64.of_int entry.offset),
Int64.add end_exc_utc (Int64.of_int entry.offset) )
let interval_mem (t : int64) ((x, y) : int64 * int64) = x <= t && t < y
let lookup_timestamp_local (t : t) timestamp : entry local_result =
let table = t.record.table in
let starts, entries = table in
let size = Bigarray.Array1.dim starts in
let index =
match bsearch_table timestamp table with
| `At i -> Some i
| `All_lower -> Some (size - 1)
| `All_bigger -> Some 0
| `Just_after i -> Some i
| `Empty -> None
in
match index with
| None -> `None
| Some index -> (
let x1 =
if
index > 0
&& interval_mem timestamp (local_interval_of_table table (index - 1))
then Some entries.(index - 1)
else None
in
let x2 =
if interval_mem timestamp (local_interval_of_table table index) then
Some entries.(index)
else None
in
let x3 =
if
index < size - 1
&& interval_mem timestamp (local_interval_of_table table (index + 1))
then Some entries.(index + 1)
else None
in
match (x1, x2, x3) with
| None, None, None -> `None
| Some x, None, None | None, Some x, None | None, None, Some x ->
`Single x
| Some x, Some y, None | Some x, None, Some y | None, Some x, Some y ->
`Ambiguous (x, y)
| Some _, Some _, Some _ -> failwith "Unexpected case")
module Raw = struct
let to_transition_seq (t : t) : ((int64 * int64) * entry) Seq.t =
let table = t.record.table in
let starts, entries = table in
let size = Bigarray.Array1.dim starts in
let rec aux s =
match s () with
| Seq.Nil -> Seq.empty
| Seq.Cons ((k1, entry1), s) -> (
match s () with
| Seq.Nil ->
fun () ->
Seq.Cons
(((k1, Constants.timestamp_max.s), entry1), aux Seq.empty)
| Seq.Cons ((k2, entry2), rest) ->
fun () ->
Seq.Cons
( ((k1, k2), entry1),
aux (fun () -> Seq.Cons ((k2, entry2), rest)) ))
in
OSeq.(0 --^ size) |> OSeq.map (fun i -> (starts.{i}, entries.(i))) |> aux
let to_transitions (t : t) : ((int64 * int64) * entry) list =
CCList.of_seq @@ to_transition_seq t
let table_of_transitions (l : (int64 * entry) list) : table option =
let table =
l
|> List.split
|> fun (starts, entries) ->
let starts =
starts
|> Array.of_list
|> Bigarray.Array1.of_array Bigarray.Int64 Bigarray.C_layout
in
let entries = Array.of_list entries in
(starts, entries)
in
if check_table table then Some table else None
let of_table ~name table = { typ = Backed name; record = process_table table }
let of_transitions ~name (l : (int64 * entry) list) : t option =
table_of_transitions l
|> CCOpt.map (fun table ->
{ typ = Backed name; record = process_table table })
end
let offset_is_recorded offset (t : t) =
Array.mem (CCInt64.to_int Span.(offset.s)) t.record.recorded_offsets
module Sexp = struct
let of_sexp (x : CCSexp.t) : t option =
let open Of_sexp_utils in
try
match x with
| `List l -> (
match l with
| `Atom "tz" :: `Atom name :: transitions ->
transitions
|> List.map (fun x ->
match x with
| `List [ start; `List [ `Atom is_dst; offset ] ] ->
let start = int64_of_sexp start in
let is_dst =
match is_dst with
| "t" -> true
| "f" -> false
| _ -> invalid_data ""
in
let offset = int_of_sexp offset in
let entry = { is_dst; offset } in
(start, entry)
| _ -> invalid_data "")
|> Raw.of_transitions ~name
| _ -> invalid_data "")
| `Atom _ -> invalid_data ""
with _ -> None
let to_sexp (t : t) : CCSexp.t =
let open To_sexp_utils in
CCSexp.(
list
(atom "tz"
::
atom (name t)
::
List.map
(fun ((start, _), entry) ->
list
[
sexp_of_int64 start;
list
[
(if entry.is_dst then atom "t" else atom "f");
sexp_of_int entry.offset;
];
])
(Raw.to_transitions t)))
let of_string s =
let res =
try CCSexp.parse_string s
with _ -> Error "Failed to parse string into sexp"
in
match res with Error _ -> None | Ok x -> of_sexp x
end
module JSON = struct
let of_json json : t option =
let exception Invalid_data in
try
match json with
| `Assoc l ->
let name =
match List.assoc "name" l with
| `String s -> s
| _ -> raise Invalid_data
in
let table_rows =
match List.assoc "table" l with
| `List l -> l
| _ -> raise Invalid_data
in
table_rows
|> List.map (fun row ->
match row with
| `List [ `String s; `Assoc e ] ->
let start = Int64.of_string s in
let is_dst =
match List.assoc "is_dst" e with
| `Bool b -> b
| _ -> raise Invalid_data
in
let offset =
match List.assoc "offset" e with
| `Int x -> x
| _ -> raise Invalid_data
in
let entry = { is_dst; offset } in
(start, entry)
| _ -> raise Invalid_data)
|> Raw.of_transitions ~name
| _ -> raise Invalid_data
with _ -> None
let of_string s = try of_json @@ Yojson.Basic.from_string s with _ -> None
let to_json (t : t) : Yojson.Basic.t =
`Assoc
[
("name", `String (name t));
( "table",
`List
(Raw.to_transition_seq t
|> Seq.map (fun ((start, _), entry) ->
`List
[
`String (Int64.to_string start);
`Assoc
[
("is_dst", `Bool entry.is_dst);
("offset", `Int entry.offset);
];
])
|> CCList.of_seq) );
]
end
module Db = struct
type db = table M.t
let empty = M.empty
let add tz db = M.add (name tz) tz.record.table db
let find_opt name db =
M.find_opt name db |> CCOpt.map (fun table -> Raw.of_table ~name table)
let remove name db = M.remove name db
let add_seq db s : db = Seq.fold_left (fun db tz -> add tz db) db s
let of_seq s : db = add_seq empty s
let names db = List.map fst (M.bindings db)
module Raw' = Raw
module Raw = struct
let dump (db : db) : string = Marshal.to_string db []
let load s : db = Marshal.from_string s 0
end
module Sexp = struct
let of_sexp (x : CCSexp.t) : db option =
let open Of_sexp_utils in
try
match x with
| `Atom _ -> invalid_data ""
| `List l ->
Some
(l
|> CCList.to_seq
|> Seq.map (fun x ->
match Sexp.of_sexp x with
| None -> invalid_data ""
| Some x -> x)
|> of_seq)
with _ -> None
let to_sexp db =
M.bindings db
|> List.map (fun (name, table) -> Raw'.of_table ~name table)
|> List.map Sexp.to_sexp
|> CCSexp.list
let of_string s =
let res =
try CCSexp.parse_string s
with _ -> Error "Failed to parse string into sexp"
in
match res with Error _ -> None | Ok x -> of_sexp x
end
end
let available_time_zones = Db.names db
let local () : t option =
match Timedesc_tzlocal.local () with
| [] -> None
| l ->
List.fold_left
(fun tz name -> match tz with Some tz -> Some tz | None -> make name)
None l
let local_exn () : t =
match local () with
| None -> invalid_arg "local_exn: Could not determine the local timezone"
| Some x -> x