-
Notifications
You must be signed in to change notification settings - Fork 10
/
moment.rkt
133 lines (110 loc) · 4.67 KB
/
moment.rkt
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
#lang racket/base
(require racket/contract/base
racket/match
racket/serialize
data/order
tzinfo
"core/compare.rkt"
"core/hmsn.rkt"
"core/ymd.rkt"
"datetime.rkt"
"offset-resolvers.rkt"
"moment-base.rkt")
(define current-timezone (make-parameter (system-tzid)))
(define (moment year [month 1] [day 1] [hour 0] [minute 0] [second 0] [nano 0]
#:tz [tz (current-timezone)]
#:resolve-offset [resolve resolve-offset/raise])
(datetime+tz->moment (datetime year month day hour minute second nano) tz resolve))
(define (datetime+tz->moment dt zone resolve)
(cond [(string? zone)
(define res (local-seconds->tzoffset zone (datetime->posix dt)))
(match res
[(tzoffset sec _ _) (make-moment dt sec zone)]
[_ (resolve res dt zone #f)])]
[else
(make-moment dt zone #f)]))
(define moment->datetime/local Moment-datetime/local)
(define moment->utc-offset Moment-utc-offset)
(define moment->tzid Moment-zone)
(define (moment->timezone m)
(or (moment->tzid m)
(moment->utc-offset m)))
(define (moment-in-utc m)
(if (equal? UTC (moment->timezone m))
m
(timezone-adjust m UTC)))
(define moment->jd
(compose1 datetime->jd
moment->datetime/local
moment-in-utc))
(define moment->posix
(compose1 datetime->posix
moment->datetime/local
moment-in-utc))
(define (posix->moment p z)
(define-values (off tzid)
(cond [(string? z)
(values (tzoffset-utc-seconds (utc-seconds->tzoffset z p)) z)]
[else
(values z #f)]))
(define dt (posix->datetime (+ p off)))
(make-moment dt off tzid))
(define (moment-add-nanoseconds m n)
(posix->moment (+ (moment->posix m) (* n (/ 1 NS/SECOND)))
(moment->timezone m)))
(define (timezone-adjust m z)
(match-define (Moment dt neg-sec _) m)
(define dt/utc (datetime-add-seconds dt (- neg-sec)))
(cond [(string? z)
(define posix (datetime->posix dt/utc))
(match-define (tzoffset offset _ _) (utc-seconds->tzoffset z posix))
(define local (datetime-add-seconds dt/utc offset))
(make-moment local offset z)]
[else
(define local (datetime-add-seconds dt/utc z))
(make-moment local z #f)]))
(define (timezone-coerce m z #:resolve-offset [resolve resolve-offset/raise])
(datetime+tz->moment (moment->datetime/local m) z resolve))
(match-define (comparison moment=? moment<? moment<=? moment>? moment>=? moment-comparator moment-order)
(build-comparison 'moment moment? moment->jd))
(define UTC "Etc/UTC")
(define tz/c (or/c string?
(integer-in -64800 64800)))
(provide tz/c)
(provide/contract
[current-timezone (parameter/c tz/c)]
[moment? (-> any/c boolean?)]
[moment (->i ([year exact-integer?])
([month (integer-in 1 12)]
[day (year month) (day-of-month/c year month)]
[hour (integer-in 0 23)]
[minute (integer-in 0 59)]
[second (integer-in 0 59)]
[nanosecond (integer-in 0 (sub1 NS/SECOND))]
#:tz [tz tz/c]
#:resolve-offset [resolve offset-resolver/c])
[res moment?])]
[datetime+tz->moment (-> datetime? tz/c offset-resolver/c moment?)]
[moment->iso8601 (-> moment? string?)]
[moment->iso8601/tzid (-> moment? string?)]
[moment->datetime/local (-> moment? datetime?)]
[moment->utc-offset (-> moment? exact-integer?)]
[moment->timezone (-> moment? tz/c)]
[moment->tzid (-> moment? (or/c string? #f))]
[moment->jd (-> moment? rational?)]
[moment->posix (-> moment? rational?)]
[posix->moment (-> rational? tz/c moment?)]
[moment-add-nanoseconds (-> moment? exact-integer? moment?)]
[moment-in-utc (-> moment? moment?)]
[timezone-adjust (-> moment? tz/c moment?)]
[timezone-coerce (->i ([m moment?]
[z tz/c])
(#:resolve-offset [r offset-resolver/c])
[res moment?])]
[moment=? (-> moment? moment? boolean?)]
[moment<? (-> moment? moment? boolean?)]
[moment<=? (-> moment? moment? boolean?)]
[moment>? (-> moment? moment? boolean?)]
[moment>=? (-> moment? moment? boolean?)]
[moment-order order?]
[UTC tz/c])