Source file calendar_builder.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
module Make(D: Date_sig.S)(T: Time_sig.S) = struct
include Utils.Float
module Date = D
module Time = T
type day = D.day = Sun | Mon | Tue | Wed | Thu | Fri | Sat
type month = D.month =
Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec
type year = int
type second = T.second
type field = [ D.field | T.field ]
let convert x t1 t2 = x +. float (Time_Zone.gap t1 t2) /. 24.
let to_gmt x = convert x (Time_Zone.current ()) Time_Zone.UTC
let from_gmt x = convert x Time_Zone.UTC (Time_Zone.current ())
let from_date x = to_gmt (float (D.to_jd x)) -. 0.5
let to_date x = D.from_jd (int_of_float (from_gmt x +. 0.5))
let to_time x =
let t, _ = modf (from_gmt x +. 0.5) in
let i = t *. 86400. in
assert (i < 86400.);
T.from_seconds (T.Second.from_float i)
let is_valid x = x >= 0. && x < 2914695.
let create d t =
to_gmt
(float (D.to_jd d)
+. T.Second.to_float (T.to_seconds t) /. 86400.) -. 0.5
let make y m d h mn s =
let x = create (D.make y m d) (T.make h mn s) in
if is_valid x then x else raise D.Out_of_bounds
let lmake ~year ?(month=1) ?(day=1) ?(hour=0) ?(minute=0)
?(second=T.Second.from_int 0) () =
make year month day hour minute second
let now () =
let now = Unix.gettimeofday () in
let gmnow = Unix.gmtime now in
let frac, _ = modf now in
from_gmt (make
(gmnow.Unix.tm_year + 1900)
(gmnow.Unix.tm_mon + 1)
gmnow.Unix.tm_mday
gmnow.Unix.tm_hour
gmnow.Unix.tm_min
(T.Second.from_float (float gmnow.Unix.tm_sec +. frac)))
let from_jd x = to_gmt x
let from_mjd x = to_gmt x +. 2400000.5
let to_jd x = from_gmt x
let to_mjd x = from_gmt x -. 2400000.5
let days_in_month x = D.days_in_month (to_date x)
let day_of_week x = D.day_of_week (to_date x)
let day_of_month x = D.day_of_month (to_date x)
let day_of_year x = D.day_of_year (to_date x)
let week x = D.week (to_date x)
let month x = D.month (to_date x)
let year x = D.year (to_date x)
let hour x = T.hour (to_time x)
let minute x = T.minute (to_time x)
let second x = T.second (to_time x)
let from_unixtm x =
make
(x.Unix.tm_year + 1900) (x.Unix.tm_mon + 1) x.Unix.tm_mday
x.Unix.tm_hour x.Unix.tm_min (T.Second.from_int x.Unix.tm_sec)
let to_unixtm x =
let tm = D.to_unixtm (to_date x)
and t = to_time x in
{ tm with
Unix.tm_sec = T.Second.to_int (T.second t);
Unix.tm_min = T.minute t;
Unix.tm_hour = T.hour t }
let jan_1_1970 = 2440587.5
let from_unixfloat x = to_gmt (x /. 86400. +. jan_1_1970)
let to_unixfloat x = (from_gmt x -. jan_1_1970) *. 86400.
let is_leap_day x = D.is_leap_day (to_date x)
let is_gregorian x = D.is_gregorian (to_date x)
let is_julian x = D.is_julian (to_date x)
let is_pm x = T.is_pm (to_time x)
let is_am x = T.is_am (to_time x)
module Period = struct
type +'a p = { d : 'a D.Period.period; t : 'a T.Period.period }
constraint 'a = [< Period.date_field ]
type +'a period = 'a p
type t = Period.date_field period
let split x =
let rec aux s =
if s < 86400. then 0, s else let d, s = aux (s -. 86400.) in d + 1, s
in
let s = T.Second.to_float (T.Period.length x.t) in
let d, s =
if s >= 0. then aux s
else let d, s = aux (-. s) in - (d + 1), -. s +. 86400.
in
assert (s >= 0. && s < 86400.);
D.Period.day d, T.Period.second (T.Second.from_float s)
let normalize x =
let days, seconds = split x in
{ d = D.Period.add x.d days; t = seconds }
let empty = { d = D.Period.empty; t = T.Period.empty }
let make y m d h mn s =
normalize { d = D.Period.make y m d; t = T.Period.make h mn s }
let lmake ?(year=0) ?(month=0) ?(day=0) ?(hour=0) ?(minute=0)
?(second=T.Second.from_int 0) () =
make year month day hour minute second
let year x = { empty with d = D.Period.year x }
let month x = { empty with d = D.Period.month x }
let week x = { empty with d = D.Period.week x }
let day x = { empty with d = D.Period.day x }
let hour x = normalize { empty with t = T.Period.hour x }
let minute x = normalize { empty with t = T.Period.minute x }
let second x = normalize { empty with t = T.Period.second x }
let add x y =
normalize { d = D.Period.add x.d y.d; t = T.Period.add x.t y.t }
let sub x y =
normalize { d = D.Period.sub x.d y.d; t = T.Period.sub x.t y.t }
let opp x = normalize { d = D.Period.opp x.d; t = T.Period.opp x.t }
let compare x y =
let n = D.Period.compare x.d y.d in
if n = 0 then T.Period.compare x.t y.t else n
let equal x y = D.Period.equal x.d y.d && T.Period.equal x.t y.t
let hash = Hashtbl.hash
let to_date x = x.d
let from_date x = { empty with d = x }
let from_time x = { empty with t = x }
exception Not_computable = D.Period.Not_computable
let gen_to_time f x = T.Period.add (T.Period.hour (f x.d * 24)) x.t
let to_time x = gen_to_time D.Period.nb_days x
let safe_to_time x = gen_to_time D.Period.safe_nb_days x
let ymds x =
let y, m, d = D.Period.ymd x.d in
y, m, d, T.Period.to_seconds x.t
end
let split x =
let t, d = modf (from_gmt (x +. 0.5)) in
let t, d = t *. 86400., int_of_float d in
let t, d = if t < 0. then t +. 86400., d - 1 else t, d in
assert (t >= 0. && t < 86400.);
D.from_jd d, T.from_seconds (T.Second.from_float t)
let unsplit d t =
to_gmt
(float (D.to_jd d)
+. (T.Second.to_float (T.to_seconds t) /. 86400.)) -. 0.5
let add x p =
let d, t = split x in
unsplit (D.add d (p.Period.d :> D.Period.t)) (T.add t p.Period.t)
let rem x p = add x (Period.opp (p :> Period.t))
let sub x y =
let d1, t1 = split x in
let d2, t2 = split y in
Period.normalize { Period.d = D.sub d1 d2; Period.t = T.sub t1 t2 }
let precise_sub x y =
let d1, t1 = split x in
let d2, t2 = split y in
Period.normalize { Period.d = D.precise_sub d1 d2; Period.t = T.sub t1 t2 }
let next x f =
let d, t = split x in
match f with
| #D.field as f -> unsplit (D.next d f) t
| #T.field as f -> unsplit d (T.next t f)
let prev x f =
let d, t = split x in
match f with
| #D.field as f -> unsplit (D.prev d f) t
| #T.field as f -> unsplit d (T.prev t f)
end
module Make_Precise(D: Date_sig.S)(T: Time_sig.S) = struct
module Date = D
module Time = T
type t = { date: D.t; time: T.t }
type day = D.day = Sun | Mon | Tue | Wed | Thu | Fri | Sat
type month = D.month =
Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec
type year = int
type second = T.second
type field = [ D.field | T.field ]
let equal x y = D.equal x.date y.date && T.equal x.time y.time
let compare x y =
let n = D.compare x.date y.date in
if n = 0 then T.compare x.time y.time else n
let hash = Hashtbl.hash
let normalize d t =
let t, days = T.normalize t in
{ date = D.add d (D.Period.day days); time = t }
let convert x t1 t2 =
let gap = T.Period.hour (Time_Zone.gap t1 t2) in
normalize x.date (T.add x.time gap)
let to_gmt x = convert x (Time_Zone.current ()) Time_Zone.UTC
let from_gmt x = convert x Time_Zone.UTC (Time_Zone.current ())
let from_date d = to_gmt { date = d; time = T.make 0 0 (T.Second.from_int 0) }
let to_date x = (from_gmt x).date
let to_time x = (from_gmt x).time
let create d t = to_gmt { date = d; time = t }
let lower_bound, upper_bound =
let compute () =
let midday = T.midday () in
let low, up =
create (D.make (-4712) 1 1) midday, create (D.make 3268 1 22) midday
in
low, up
in
Time_Zone.on compute Time_Zone.UTC ()
let is_valid x = compare x lower_bound >= 0 && compare x upper_bound <= 0
let make y m d h mn s =
let x = create (D.make y m d) (T.make h mn s) in
if is_valid x then x else raise D.Out_of_bounds
let lmake ~year ?(month=1) ?(day=1) ?(hour=0) ?(minute=0)
?(second=T.Second.from_int 0) () =
make year month day hour minute second
let now () =
let now = Unix.gettimeofday () in
let gmnow = Unix.gmtime now in
let frac, _ = modf now in
from_gmt (make
(gmnow.Unix.tm_year + 1900)
(gmnow.Unix.tm_mon + 1)
gmnow.Unix.tm_mday
gmnow.Unix.tm_hour
gmnow.Unix.tm_min
(T.Second.from_float (float gmnow.Unix.tm_sec +. frac)))
let from_jd x =
let frac, intf = modf x in
to_gmt
{ date = D.from_jd (int_of_float intf);
time = T.from_seconds (T.Second.from_float (frac *. 86400. +. 43200.)) }
let from_mjd x = from_jd (x +. 2400000.5)
let to_jd x =
let x = from_gmt x in
float (D.to_jd x.date) +. T.Second.to_float (T.to_seconds x.time) /. 86400.
-. 0.5
let to_mjd x = to_jd x -. 2400000.5
let days_in_month x = D.days_in_month (to_date x)
let day_of_week x = D.day_of_week (to_date x)
let day_of_month x = D.day_of_month (to_date x)
let day_of_year x = D.day_of_year (to_date x)
let week x = D.week (to_date x)
let month x = D.month (to_date x)
let year x = D.year (to_date x)
let hour x = T.hour (to_time x)
let minute x = T.minute (to_time x)
let second x = T.second (to_time x)
let from_unixtm x =
make
(x.Unix.tm_year + 1900) (x.Unix.tm_mon + 1) x.Unix.tm_mday
x.Unix.tm_hour x.Unix.tm_min (T.Second.from_int x.Unix.tm_sec)
let to_unixtm x =
let tm = D.to_unixtm (to_date x)
and t = to_time x in
{ tm with
Unix.tm_sec = T.Second.to_int (T.second t);
Unix.tm_min = T.minute t;
Unix.tm_hour = T.hour t }
let jan_1_1970 = 2440587.5
let from_unixfloat x = from_jd (x /. 86400. +. jan_1_1970)
let to_unixfloat x = (to_jd x -. jan_1_1970) *. 86400.
let is_leap_day x = D.is_leap_day (to_date x)
let is_gregorian x = D.is_gregorian (to_date x)
let is_julian x = D.is_julian (to_date x)
let is_pm x = T.is_pm (to_time x)
let is_am x = T.is_am (to_time x)
module Period = struct
type +'a p = { d : 'a D.Period.period; t : 'a T.Period.period }
constraint 'a = [< Period.date_field ]
type +'a period = 'a p
type t = Period.date_field period
let split x =
let rec aux s =
if s < 86400. then 0, s else let d, s = aux (s -. 86400.) in d + 1, s
in
let s = T.Second.to_float (T.Period.length x.t) in
let d, s =
if s >= 0. then aux s
else let d, s = aux (-. s) in - (d + 1), -. s +. 86400.
in
assert (s >= 0. && s < 86400.);
D.Period.day d, T.Period.second (T.Second.from_float s)
let normalize x =
let days, seconds = split x in
{ d = D.Period.add x.d days; t = seconds }
let empty = { d = D.Period.empty; t = T.Period.empty }
let make y m d h mn s =
normalize { d = D.Period.make y m d; t = T.Period.make h mn s }
let lmake ?(year=0) ?(month=0) ?(day=0) ?(hour=0) ?(minute=0)
?(second=T.Second.from_int 0) () =
make year month day hour minute second
let year x = { empty with d = D.Period.year x }
let month x = { empty with d = D.Period.month x }
let week x = { empty with d = D.Period.week x }
let day x = { empty with d = D.Period.day x }
let hour x = normalize { empty with t = T.Period.hour x }
let minute x = normalize { empty with t = T.Period.minute x }
let second x = normalize { empty with t = T.Period.second x }
let add x y =
normalize { d = D.Period.add x.d y.d; t = T.Period.add x.t y.t }
let sub x y =
normalize { d = D.Period.sub x.d y.d; t = T.Period.sub x.t y.t }
let opp x = normalize { d = D.Period.opp x.d; t = T.Period.opp x.t }
let compare x y =
let n = D.Period.compare x.d y.d in
if n = 0 then T.Period.compare x.t y.t else n
let equal x y = D.Period.equal x.d y.d && T.Period.equal x.t y.t
let hash = Hashtbl.hash
let to_date x = x.d
let from_date x = { empty with d = x }
let from_time x = { empty with t = x }
exception Not_computable = D.Period.Not_computable
let gen_to_time f x = T.Period.add (T.Period.hour (f x.d * 24)) x.t
let to_time x = gen_to_time D.Period.nb_days x
let safe_to_time x = gen_to_time D.Period.safe_nb_days x
let ymds x =
let y, m, d = D.Period.ymd x.d in
y, m, d, T.Period.to_seconds x.t
end
let add x p =
normalize
(D.add x.date (p.Period.d :> D.Period.t)) (T.add x.time p.Period.t)
let rem x p = add x (Period.opp (p :> Period.t))
let sub x y =
Period.normalize
{ Period.d = D.sub x.date y.date; Period.t = T.sub x.time y.time }
let precise_sub x y =
Period.normalize
{ Period.d = D.precise_sub x.date y.date;
Period.t = T.sub x.time y.time }
let next x = function
| #D.field as f -> normalize (D.next x.date f) x.time
| #T.field as f -> normalize x.date (T.next x.time f)
let prev x = function
| #D.field as f -> normalize (D.prev x.date f) x.time
| #T.field as f -> normalize x.date (T.prev x.time f)
end