Source file time_zone_info.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
type t = {
tz : Time_zone.t;
fixed_offset_from_utc : Span.t option;
}
let tz (x : t) = x.tz
let fixed_offset_from_utc (x : t) = x.fixed_offset_from_utc
let equal (x : t) (y : t) =
match (x, y) with
| ( { tz = tz1; fixed_offset_from_utc = None },
{ tz = tz2; fixed_offset_from_utc = None } ) ->
Time_zone.equal tz1 tz2
| ( { tz = tz1; fixed_offset_from_utc = Some x1 },
{ tz = tz2; fixed_offset_from_utc = Some x2 } ) ->
Time_zone.equal tz1 tz2 && Span.equal x1 x2
| _, _ -> false
type error =
[ `Missing_both_tz_and_fixed_offset_from_utc
| `Invalid_offset of Span.t
| `Unrecorded_offset of Span.t
]
let make ?tz ?fixed_offset_from_utc () : (t, error) result =
let fixed_offset_from_utc =
Option.map
(fun offset -> Span.(make ~s:(Span.get_s offset) ()))
fixed_offset_from_utc
in
match (tz, fixed_offset_from_utc) with
| None, None -> Error `Missing_both_tz_and_fixed_offset_from_utc
| Some tz, None ->
Ok { tz; fixed_offset_from_utc = Time_zone.to_fixed_offset_from_utc tz }
| None, Some offset_from_utc -> (
match Time_zone.make_offset_only offset_from_utc with
| None -> Error (`Invalid_offset offset_from_utc)
| Some tz -> Ok { tz; fixed_offset_from_utc = Some offset_from_utc })
| Some tz, Some offset_from_utc ->
if Time_zone.offset_is_recorded offset_from_utc tz then
Ok { tz; fixed_offset_from_utc = Some offset_from_utc }
else Error (`Unrecorded_offset offset_from_utc)
let make_exn ?tz ?fixed_offset_from_utc () : t =
match make ?tz ?fixed_offset_from_utc () with
| Ok x -> x
| Error _ -> invalid_arg "make_exn"