Source file date.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
let months =
[|
"Jan"
; "Feb"
; "Mar"
; "Apr"
; "May"
; "Jun"
; "Jul"
; "Aug"
; "Sep"
; "Oct"
; "Nov"
; "Dec"
|]
let days = [|"Sun"; "Mon"; "Tue"; "Wed"; "Thu"; "Fri"; "Sat"|]
type print_timezone = Empty | TZ of string
type t = Ptime.date * Ptime.time * print_timezone
let utc = TZ "Z"
let of_dt print_type dt =
let date, time = dt in
(date, time, print_type)
let to_dt (date, time, _) = (date, time)
let best_effort_iso8601_to_rfc3339 x =
let x =
try
Scanf.sscanf x "%04d%02d%02dT%s" (fun y mon d rest ->
Printf.sprintf "%04d-%02d-%02dT%s" y mon d rest
)
with _ -> x
in
let tz =
try
Scanf.sscanf x "%04d-%02d-%02dT%02d:%02d:%02d%s" (fun _ _ _ _ _ _ tz ->
Some tz
)
with _ -> None
in
match tz with
| None | Some "" ->
(Printf.sprintf "%sZ" x, Empty)
| Some tz ->
(x, TZ tz)
let of_iso8601 x =
let rfc3339, print_timezone = best_effort_iso8601_to_rfc3339 x in
match Ptime.of_rfc3339 rfc3339 |> Ptime.rfc3339_error_to_msg with
| Error _ ->
invalid_arg (Printf.sprintf "%s: %s" __FUNCTION__ x)
| Ok (t, tz, _) -> (
match tz with
| None | Some 0 ->
Ptime.to_date_time t |> of_dt print_timezone
| Some _ ->
invalid_arg (Printf.sprintf "%s: %s" __FUNCTION__ x)
)
let to_rfc3339 ((y, mon, d), ((h, min, s), _), print_type) =
match print_type with
| TZ tz ->
Printf.sprintf "%04i%02i%02iT%02i:%02i:%02i%s" y mon d h min s tz
| Empty ->
Printf.sprintf "%04i%02i%02iT%02i:%02i:%02i" y mon d h min s
let weekday ~year ~mon ~day =
let a = (14 - mon) / 12 in
let y = year - a in
let m = mon + (12 * a) - 2 in
(day + y + (y / 4) - (y / 100) + (y / 400) + (31 * m / 12)) mod 7
let to_rfc822 ((year, mon, day), ((h, min, s), _), print_type) =
let timezone =
match print_type with Empty | TZ "Z" -> "GMT" | TZ tz -> tz
in
let weekday = weekday ~year ~mon ~day in
Printf.sprintf "%s, %d %s %d %02d:%02d:%02d %s" days.(weekday) day
months.(mon - 1)
year h min s timezone
let to_ptime_t t =
match to_dt t |> Ptime.of_date_time with
| Some t ->
t
| None ->
let _, (_, offset), _ = t in
invalid_arg
(Printf.sprintf "%s: dt='%s', offset='%i' is invalid" __FUNCTION__
(to_rfc3339 t) offset
)
let to_ptime = to_ptime_t
let of_ptime t = Ptime.to_date_time t |> of_dt utc
let of_unix_time s =
match Ptime.of_float_s s with
| None ->
invalid_arg (Printf.sprintf "%s: %f" __FUNCTION__ s)
| Some t ->
of_ptime t
let to_unix_time t = to_ptime_t t |> Ptime.to_float_s
let _localtime current_tz_offset t =
let tz_offset_s = current_tz_offset |> Option.value ~default:0 in
let localtime = t |> Ptime.to_date_time ~tz_offset_s |> of_dt Empty in
let _, (_, localtime_offset), _ = localtime in
if localtime_offset <> tz_offset_s then
invalid_arg
(Printf.sprintf "%s: offsets don't match. offset='%i', t='%s'"
__FUNCTION__ tz_offset_s (Ptime.to_rfc3339 t)
) ;
localtime
let _localtime_string current_tz_offset t =
_localtime current_tz_offset t |> to_rfc3339
let localtime () =
_localtime (Ptime_clock.current_tz_offset_s ()) (Ptime_clock.now ())
let now () = of_ptime (Ptime_clock.now ())
let epoch = of_ptime Ptime.epoch
let eq x y = x = y
let assert_utc _ = ()
let never = epoch
let of_string = of_iso8601
let to_string = to_rfc3339
let of_float = of_unix_time
let to_float = to_unix_time
let rfc822_of_float = of_unix_time
let rfc822_to_string = to_rfc822
type iso8601 = t
type rfc822 = t