summaryrefslogtreecommitdiff
path: root/lib/date/date.myr
blob: bbfd3264609e5fb1ef86220bc5fa781760271fd9 (plain)
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
use std
use "types"
use "zoneinfo"

pkg date =
	/* useful constructors */
	const utcnow	: (-> instant)
	const now	: (tz : byte[:] -> instant)
	const tozone	: (d : instant, zone : byte[:]	-> instant)
	const mkdate	: (y : int, m : int, day : int, zone : byte[:]	-> instant)
	const mkdatetime	: (year : int, mon : int, day : int, \
		h : int, m : int, s : int, zone : byte[:]	-> instant)
	const mkinstant	: (tm : std.time, zone : byte[:]	-> instant)

	const localoff	: (tm : std.time -> duration)
	const tzoff	: (tzname : byte[:], tm : std.time	-> duration)
	const isleap	: (d : instant	-> bool)

	/* date differences */
	const add	: (d : instant, dt : duration	-> instant)
	const sub	: (d : instant, dt : duration	-> instant)
	const addperiod	: (d : instant, dt : period	-> instant)
	const subperiod	: (d : instant, dt : period	-> instant)

	const duration	: (a : instant, b : instant	-> duration)

	pkglocal const recalc	: (inst : instant# -> std.time)
;;

const Days400y	= 365*400 + 4*25 - 3
const Days4y	= 365*4 + 1
const DayUsec	= (24*60*60*1_000_000)
const Mdays	= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

const utcnow = {
	-> mkinstantoff(std.now(), "", 0)
}

const now = {tz : byte[:]
	var tm

	tm = std.now()
	-> mkinstant(tm, tz)
}

const tozone = {d, tz
	-> mkinstant(d.actual, tz)
}

const mkdate = {y, m, d, tz
	-> mkinstant(recalc(&[.year=y, .mon=m, .day=d]), tz)
}

const mkdatetime = {year, mon, day, h, m, s, tz
	var tm

	tm = recalc(&[.year=year, .mon=mon, .day=day,
		.h=h, .m=m, .s=s,])

	match _zoneinfo.findtzoff(tz, tm)
	| `std.Some off:
		-> mkinstantoff(tm, tz, off)
	| `std.None:
		std.fatal("invalid timezone {}\n", tz)
	;;
}

const mkinstant = {tm, tz
	match _zoneinfo.findtzoff(tz, tm)
	| `std.Some o:	-> mkinstantoff(tm, tz, o)
	| `std.None:	std.fatal("no zone named {}\n", tz)
	;;
}

const mkinstantoff = {tm, tz, tzoff
	var j, y, m, d
	var t, e
	var inst

	inst.actual = tm
	/* time zones */
	std.assert(tz.len <= inst._tzbuf.len, "time zone name too long\n")
	std.slcp(inst._tzbuf[:tz.len], tz)
	inst.tzname = inst._tzbuf[:tz.len]
	inst.tzoff = tzoff
	tm += (inst.tzoff : std.time)

	/* break up time */
	t = tm % DayUsec	/* time */
	e = tm / DayUsec	/* epoch day */

	if t < 0
		t += DayUsec
		e -= 1
	;;

	/* microseconds, seconds, minutes, hours */
	inst.us  = (t % 1_000_000 : int)
	t /= 1_000_000
	inst.s = (t % 60 : int)
	t /= 60
	inst.m = (t % 60 : int)
	t /= 60
	inst.h = (t : int)

	/* weekday */
	inst.wday = ((e + 4) % 7 : int)	/* the world started on Thursday */

	/*
	split up year, month, day.

	Implemented according to "Algorithm 199, conversions between calendar 
	date and Julian day number", Robert G. Tantzen, Air Force Missile Development
	Center, Holloman AFB, New Mex.

	Lots of magic. Yer a wizard, 'arry.
	*/
	j = (tm + 2440588 * DayUsec) / DayUsec 
	j -= 1721119

	y = (4 * j - 1) / Days400y
	j = 4 * j - 1 - Days400y * y
	d = j / 4

	j = (4 * d + 3) / Days4y
	d = 4 * d + 3 - Days4y * j
	d = (d + 4) / 4 ;

	m = (5 * d - 3) / 153
	d = 5 * d - 3 - 153 * m
	d = (d + 5) / 5

	y = 100 * y + j
	if m < 10
		m += 3
	else
		m -= 9 
		y++
	;;

	/* there's no year 0 */
	if y <= 0
		y--
	;;
	/* and if j negative, the day and month are also negative */
	if m < 0
		m += 12
	;;
	if d < 0
		d += Mdays[m - 1] 
	;;

	inst.year = (y : int)
	inst.mon = (m : int)
	inst.day = (d : int)
	-> inst
}

const localoff = {tm
	-> tzoff("local", tm)
}

const tzoff = {tz, tm
	match _zoneinfo.findtzoff(tz, tm)
	| `std.Some dt:	-> dt
	| `std.None:	std.fatal("unable to load zoneinfo\n")
	;;
}

const isleap = {d
	-> d.year % 4 == 0 && (d.year % 100 != 0 || d.year % 400 == 0)
}

const add  = {d, dt
	-> mkinstantoff(d.actual + (dt : std.time), d.tzname, d.tzoff)
}

const sub  = {d, dt
	-> mkinstantoff(d.actual - (dt : std.time), d.tzname, d.tzoff)
}

const addperiod = {inst, p
	match p
	| `Year	y:	inst.year += y
	| `Month m:	inst.mon += m
	| `Day	d:	inst.day += d
	| `Hour	h:	inst.h += h
	| `Minute m:	inst.m += m
	| `Second s:	inst.s += s
	;;
	-> mkinstantoff(recalc(&inst), inst.tzname, inst.tzoff)
}

const subperiod = {inst, p
	match p
	| `Year	y:	inst.year -= y
	| `Month m:	inst.mon -= m
	| `Day	d:	inst.day -= d
	| `Hour	h:	inst.h -= h
	| `Minute m:	inst.m -= m
	| `Second s:	inst.s -= s
	;;
	-> mkinstantoff(recalc(&inst), inst.tzname, inst.tzoff)
}

const duration = {a, b
	-> (b.actual - a.actual : duration)
}

const recalc = {inst
	var c, ya, j, tm
	var y, m, d


	if inst.mon > 2
		m = (inst.mon - 3 : std.time)
		y = (inst.year : std.time)
	else
		m = (inst.mon + 9 : std.time)
		y = (inst.year - 1 : std.time)
	;;
	d = (inst.day : std.time)

	c = y / 100
	ya = y - 100 * c
	j = c * Days400y / 4 + \
		Days4y * ya / 4 + \
		(153 * m + 2)/5 + d - \
		719469
	tm = j * DayUsec
	tm += (inst.h : std.time) * 3600*1_000_000
	tm += (inst.m : std.time) * 60*1_000_000
	tm += (inst.s : std.time) * 1_000_000
	tm += (inst.us : std.time)
	-> tm
}