]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/calendar.factor
calendar: Add word for getting dates from ordinals.
[factor.git] / basis / calendar / calendar.factor
1 ! Copyright (C) 2007 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors classes.tuple combinators
4 combinators.short-circuit kernel locals math math.functions
5 math.intervals math.order math.parser sequences splitting system
6 vocabs vocabs.loader ;
7 QUALIFIED-WITH: math.ranges R
8 IN: calendar
9
10 ERROR: not-in-interval value interval ;
11
12 : check-interval ( value interval -- value )
13     2dup interval-contains? [ drop ] [ not-in-interval ] if ;
14
15 HOOK: gmt-offset os ( -- hours minutes seconds )
16
17 HOOK: gmt os ( -- timestamp )
18
19 TUPLE: duration
20     { year real }
21     { month real }
22     { day real }
23     { hour real }
24     { minute real }
25     { second real } ;
26
27 C: <duration> duration
28
29 : instant ( -- duration ) 0 0 0 0 0 0 <duration> ;
30
31 TUPLE: timestamp
32     { year integer }
33     { month integer }
34     { day integer }
35     { hour integer }
36     { minute integer }
37     { second real }
38     { gmt-offset duration } ;
39
40 CONSTANT: day-counts { 0 31 28 31 30 31 30 31 31 30 31 30 31 }
41
42 GENERIC: leap-year? ( obj -- ? )
43
44 M: integer leap-year?
45     dup 100 divisor? 400 4 ? divisor? ;
46
47 M: timestamp leap-year?
48     year>> leap-year? ;
49
50 : (days-in-month) ( year month -- n )
51     dup 2 = [ drop leap-year? 29 28 ? ] [ nip day-counts nth ] if ;
52
53 :: <timestamp> ( year month day hour minute second gmt-offset -- timestamp )
54     year
55     month 1 12 [a,b] check-interval
56     day 1 year month (days-in-month) [a,b] check-interval
57     hour 0 23 [a,b] check-interval
58     minute 0 59 [a,b] check-interval
59     second 0 60 [a,b) check-interval
60     gmt-offset timestamp boa ;
61
62 M: timestamp clone (clone) [ clone ] change-gmt-offset ;
63
64 : gmt-offset-duration ( -- duration )
65     0 0 0 gmt-offset <duration> ; inline
66
67 : <date> ( year month day -- timestamp )
68     0 0 0 gmt-offset-duration <timestamp> ; inline
69
70 : <date-gmt> ( year month day -- timestamp )
71     0 0 0 instant <timestamp> ; inline
72
73 : <year> ( year -- timestamp )
74     1 1 <date> ; inline
75
76 : <year-gmt> ( year -- timestamp )
77     1 1 <date-gmt> ; inline
78
79 CONSTANT: average-month 30+5/12
80 CONSTANT: months-per-year 12
81 CONSTANT: days-per-year 3652425/10000
82 CONSTANT: hours-per-year 876582/100
83 CONSTANT: minutes-per-year 5259492/10
84 CONSTANT: seconds-per-year 31556952
85
86 :: julian-day-number ( year month day -- n )
87     ! Returns a composite date number
88     ! Not valid before year -4800
89     14 month - 12 /i :> a
90     year 4800 + a - :> y
91     month 12 a * + 3 - :> m
92
93     day 153 m * 2 + 5 /i + 365 y * +
94     y 4 /i + y 100 /i - y 400 /i + 32045 - ;
95
96 :: julian-day-number>date ( n -- year month day )
97     ! Inverse of julian-day-number
98     n 32044 + :> a
99     4 a * 3 + 146097 /i :> b
100     a 146097 b * 4 /i - :> c
101     4 c * 3 + 1461 /i :> d
102     c 1461 d * 4 /i - :> e
103     5 e * 2 + 153 /i :> m
104
105     100 b * d + 4800 -
106     m 10 /i + m 3 +
107     12 m 10 /i * -
108     e 153 m * 2 + 5 /i - 1 + ;
109
110 GENERIC: easter ( obj -- obj' )
111
112 :: easter-month-day ( year -- month day )
113     year 19 mod :> a
114     year 100 /mod :> ( b c )
115     b 4 /mod :> ( d e )
116     b 8 + 25 /i :> f
117     b f - 1 + 3 /i :> g
118     19 a * b + d - g - 15 + 30 mod :> h
119     c 4 /mod :> ( i k )
120     32 2 e * + 2 i * + h - k - 7 mod :> l
121     a 11 h * + 22 l * + 451 /i :> m
122
123     h l + 7 m * - 114 + 31 /mod 1 + ;
124
125 M: integer easter
126     dup easter-month-day <date> ;
127
128 M: timestamp easter
129     clone
130     dup year>> easter-month-day
131     swapd >>day swap >>month ;
132
133 : >date< ( timestamp -- year month day )
134     [ year>> ] [ month>> ] [ day>> ] tri ;
135
136 : >time< ( timestamp -- hour minute second )
137     [ hour>> ] [ minute>> ] [ second>> ] tri ;
138
139 : set-time! ( timestamp hours minutes seconds -- timestamp )
140     [ >>hour ] [ >>minute ] [ >>second ] tri* ;
141
142 : set-time ( timestamp hours minutes seconds -- timestamp )
143     clone set-time! ;
144
145 : time>hms ( str -- hms-seq )
146     ":" split [ string>number ] map
147     3 0 pad-tail ;
148
149 : time>offset ( str -- hms-seq )
150     "-" ?head [ time>hms ] dip
151     [ [ neg ] map ] when ;
152
153 : years ( x -- duration ) instant swap >>year ;
154 : months ( x -- duration ) instant swap >>month ;
155 : days ( x -- duration ) instant swap >>day ;
156 : weeks ( x -- duration ) 7 * days ;
157 : hours ( x -- duration ) instant swap >>hour ;
158 : minutes ( x -- duration ) instant swap >>minute ;
159 : seconds ( x -- duration ) instant swap >>second ;
160 : milliseconds ( x -- duration ) 1000 / seconds ;
161 : microseconds ( x -- duration ) 1000000 / seconds ;
162 : nanoseconds ( x -- duration ) 1000000000 / seconds ;
163
164 <PRIVATE
165
166 GENERIC: +year ( timestamp x -- timestamp )
167 GENERIC: +month ( timestamp x -- timestamp )
168 GENERIC: +day ( timestamp x -- timestamp )
169 GENERIC: +hour ( timestamp x -- timestamp )
170 GENERIC: +minute ( timestamp x -- timestamp )
171 GENERIC: +second ( timestamp x -- timestamp )
172
173 : /rem ( f n -- q r )
174     ! q is positive or negative, r is positive from 0 <= r < n
175     [ / floor >integer ] 2keep rem ;
176
177 : float>whole-part ( float -- int float )
178     [ floor >integer ] keep over - ;
179
180 : adjust-leap-year ( timestamp -- timestamp )
181     dup
182     { [ day>> 29 = ] [ month>> 2 = ] [ leap-year? not ] } 1&&
183     [ 3 >>month 1 >>day ] when ;
184
185 M: integer +year
186     [ + ] curry change-year adjust-leap-year ;
187
188 M: real +year
189     [ float>whole-part swapd days-per-year * +day swap +year ] unless-zero ;
190
191 : months/years ( n -- months years )
192     12 /rem [ 1 - 12 ] when-zero swap ; inline
193
194 M: integer +month
195     [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
196
197 M: real +month
198     [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
199
200 M: integer +day
201     [
202         over >date< julian-day-number + julian-day-number>date
203         [ >>year ] [ >>month ] [ >>day ] tri*
204     ] unless-zero ;
205
206 M: real +day
207     [ float>whole-part swapd 24 * +hour swap +day ] unless-zero ;
208
209 : hours/days ( n -- hours days )
210     24 /rem swap ;
211
212 M: integer +hour
213     [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
214
215 M: real +hour
216     float>whole-part swapd 60 * +minute swap +hour ;
217
218 : minutes/hours ( n -- minutes hours )
219     60 /rem swap ;
220
221 M: integer +minute
222     [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
223
224 M: real +minute
225     [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
226
227 : seconds/minutes ( n -- seconds minutes )
228     60 /rem swap >integer ;
229
230 M: number +second
231     [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
232
233 : (time+) ( timestamp duration -- timestamp' duration )
234     [ second>> +second ] keep
235     [ minute>> +minute ] keep
236     [ hour>>   +hour   ] keep
237     [ day>>    +day    ] keep
238     [ month>>  +month  ] keep
239     [ year>>   +year   ] keep ; inline
240
241 : +slots ( obj1 obj2 quot -- n obj1 obj2 )
242     [ bi@ + ] curry 2keep ; inline
243
244 PRIVATE>
245
246 GENERIC#: time+ 1 ( time1 time2 -- time3 )
247
248 M: timestamp time+
249     [ clone ] dip (time+) drop ;
250
251 M: duration time+
252     dup timestamp? [
253         swap time+
254     ] [
255         [ year>> ] +slots
256         [ month>> ] +slots
257         [ day>> ] +slots
258         [ hour>> ] +slots
259         [ minute>> ] +slots
260         [ second>> ] +slots
261         2drop <duration>
262     ] if ;
263
264 : duration>years ( duration -- x )
265     ! Uses average month/year length since duration loses calendar
266     ! data
267     0 swap
268     {
269         [ year>> + ]
270         [ month>> months-per-year / + ]
271         [ day>> days-per-year / + ]
272         [ hour>> hours-per-year / + ]
273         [ minute>> minutes-per-year / + ]
274         [ second>> seconds-per-year / + ]
275     } cleave ;
276
277 M: duration <=> [ duration>years ] compare ;
278
279 : duration>months ( duration -- x ) duration>years months-per-year * ;
280 : duration>days ( duration -- x ) duration>years days-per-year * ;
281 : duration>hours ( duration -- x ) duration>years hours-per-year * ;
282 : duration>minutes ( duration -- x ) duration>years minutes-per-year * ;
283 : duration>seconds ( duration -- x ) duration>years seconds-per-year * ;
284 : duration>milliseconds ( duration -- x ) duration>seconds 1000 * ;
285 : duration>microseconds ( duration -- x ) duration>seconds 1000000 * ;
286 : duration>nanoseconds ( duration -- x ) duration>seconds 1000000000 * ;
287
288 GENERIC: time- ( time1 time2 -- time3 )
289
290 : convert-timezone ( timestamp duration -- timestamp' )
291     over gmt-offset>> over = [ drop ] [
292         [ over gmt-offset>> time- time+ ] keep >>gmt-offset
293     ] if ;
294
295 : >local-time ( timestamp -- timestamp' )
296     clone gmt-offset-duration convert-timezone ;
297
298 : >gmt ( timestamp -- timestamp' )
299     clone dup gmt-offset>> dup instant =
300     [ drop ] [
301         [ neg +second 0 ] change-second
302         [ neg +minute 0 ] change-minute
303         [ neg +hour   0 ] change-hour
304         [ neg +day    0 ] change-day
305         [ neg +month  0 ] change-month
306         [ neg +year   0 ] change-year drop
307     ] if ;
308
309 M: timestamp <=> [ >gmt tuple-slots ] compare ;
310
311 : same-day? ( ts1 ts2 -- ? )
312     [ >gmt >date< <date> ] same? ;
313
314 : (time-) ( timestamp timestamp -- n )
315     [ >gmt ] bi@
316     [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
317     [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
318
319 M: timestamp time-
320     ! Exact calendar-time difference
321     (time-) seconds ;
322
323 : time* ( obj1 obj2 -- obj3 )
324     dup real? [ swap ] when
325     dup real? [ * ] [
326         {
327             [   year>> * ]
328             [  month>> * ]
329             [    day>> * ]
330             [   hour>> * ]
331             [ minute>> * ]
332             [ second>> * ]
333         } 2cleave <duration>
334     ] if ;
335
336 : before ( duration -- -duration )
337     -1 time* ;
338
339 <PRIVATE
340
341 : -slots ( obj1 obj2 quot -- n obj1 obj2 )
342     [ bi@ - ] curry 2keep ; inline
343
344 PRIVATE>
345
346 M: duration time-
347     over timestamp? [
348         before time+
349     ] [
350         [ year>> ] -slots
351         [ month>> ] -slots
352         [ day>> ] -slots
353         [ hour>> ] -slots
354         [ minute>> ] -slots
355         [ second>> ] -slots
356         2drop <duration>
357     ] if ;
358
359 : unix-1970 ( -- timestamp )
360     1970 <year-gmt> ; inline
361
362 : millis>timestamp ( x -- timestamp )
363     [ unix-1970 ] dip 1000 / +second ;
364
365 : timestamp>millis ( timestamp -- n )
366     unix-1970 (time-) 1000 * >integer ;
367
368 : micros>timestamp ( x -- timestamp )
369     [ unix-1970 ] dip 1000000 / +second ;
370
371 : timestamp>micros ( timestamp -- n )
372     unix-1970 (time-) 1000000 * >integer ;
373
374 : now ( -- timestamp )
375     gmt gmt-offset-duration (time+) >>gmt-offset ;
376
377 : hence ( duration -- timestamp ) now swap time+ ;
378
379 : ago ( duration -- timestamp ) now swap time- ;
380
381 : zeller-congruence ( year month day -- n )
382     ! Zeller Congruence
383     ! http://web.textfiles.com/computers/formulas.txt
384     ! good for any date since October 15, 1582
385     [
386         dup 2 <= [ [ 1 - ] [ 12 + ] bi* ] when
387         [ dup [ 4 /i + ] [ 100 /i - ] [ 400 /i + ] tri ] dip
388         [ 1 + 3 * 5 /i + ] keep 2 * +
389     ] dip 1 + + 7 mod ;
390
391 GENERIC: days-in-year ( obj -- n )
392
393 M: integer days-in-year leap-year? 366 365 ? ;
394
395 M: timestamp days-in-year year>> days-in-year ;
396
397 : days-in-month ( timestamp -- n )
398     >date< drop (days-in-month) ;
399
400 : day-of-week ( timestamp -- n )
401     >date< zeller-congruence ;
402
403 :: (day-of-year) ( year month day -- n )
404     day-counts month head-slice sum day +
405     year leap-year? [
406         year month day <date>
407         year 3 1 <date>
408         after=? [ 1 + ] when
409     ] when ;
410
411 : day-of-year ( timestamp -- n )
412     >date< (day-of-year) ;
413
414 : midnight! ( timestamp -- new-timestamp )
415     0 >>hour 0 >>minute 0 >>second ; inline
416
417 : midnight ( timestamp -- new-timestamp )
418     clone midnight! ; inline
419
420 : noon ( timestamp -- new-timestamp )
421     midnight 12 >>hour ; inline
422
423 : today ( -- timestamp )
424     now midnight! ; inline
425
426 : tomorrow ( -- timestamp )
427     1 days hence midnight! ; inline
428
429 : yesterday ( -- timestamp )
430     1 days ago midnight! ; inline
431
432 GENERIC: beginning-of-day ( object -- new-timestamp )
433 M: timestamp beginning-of-day midnight ;
434
435 : end-of-day! ( timestamp -- timestamp )
436     23 >>hour 59 >>minute 59+999/1000 >>second ;
437
438 GENERIC: end-of-day ( object -- new-timestamp )
439 M: timestamp end-of-day clone end-of-day! ;
440
441 : beginning-of-month ( timestamp -- new-timestamp )
442     midnight 1 >>day ; inline
443
444 : end-of-month ( timestamp -- new-timestamp )
445     [ end-of-day ] [ days-in-month ] bi >>day ;
446
447 GENERIC: beginning-of-year ( object -- new-timestamp )
448 M: timestamp beginning-of-year beginning-of-month 1 >>month ;
449 M: integer beginning-of-year <year> ;
450
451 GENERIC: end-of-year ( object -- new-timestamp )
452 M: timestamp end-of-year end-of-day 12 >>month 31 >>day ;
453 M: integer end-of-year 12 31 <date> end-of-day! ;
454
455 <PRIVATE
456
457 : day-offset ( timestamp m -- new-timestamp n )
458     over day-of-week - ; inline
459
460 : day-this-week ( timestamp n -- new-timestamp )
461     day-offset days time+ ;
462
463 :: nth-day-this-month ( timestamp n day -- new-timestamp )
464     timestamp beginning-of-month day day-this-week
465     dup timestamp [ month>> ] same? [ 1 weeks time+ ] unless ;
466
467 : last-day-this-month ( timestamp day -- new-timestamp )
468     [ 1 months time+ 1 ] dip nth-day-this-month 1 weeks time- ;
469
470 PRIVATE>
471
472 GENERIC: january ( obj -- timestamp )
473 GENERIC: february ( obj -- timestamp )
474 GENERIC: march ( obj -- timestamp )
475 GENERIC: april ( obj -- timestamp )
476 GENERIC: may ( obj -- timestamp )
477 GENERIC: june ( obj -- timestamp )
478 GENERIC: july ( obj -- timestamp )
479 GENERIC: august ( obj -- timestamp )
480 GENERIC: september ( obj -- timestamp )
481 GENERIC: october ( obj -- timestamp )
482 GENERIC: november ( obj -- timestamp )
483 GENERIC: december ( obj -- timestamp )
484
485 M: integer january 1 1 <date> ;
486 M: integer february 2 1 <date> ;
487 M: integer march 3 1 <date> ;
488 M: integer april 4 1 <date> ;
489 M: integer may 5 1 <date> ;
490 M: integer june 6 1 <date> ;
491 M: integer july 7 1 <date> ;
492 M: integer august 8 1 <date> ;
493 M: integer september 9 1 <date> ;
494 M: integer october 10 1 <date> ;
495 M: integer november 11 1 <date> ;
496 M: integer december 12 1 <date> ;
497
498 M: timestamp january clone 1 >>month ;
499 M: timestamp february clone 2 >>month ;
500 M: timestamp march clone 3 >>month ;
501 M: timestamp april clone 4 >>month ;
502 M: timestamp may clone 5 >>month ;
503 M: timestamp june clone 6 >>month ;
504 M: timestamp july clone 7 >>month ;
505 M: timestamp august clone 8 >>month ;
506 M: timestamp september clone 9 >>month ;
507 M: timestamp october clone 10 >>month ;
508 M: timestamp november clone 11 >>month ;
509 M: timestamp december clone 12 >>month ;
510
511 : <january> ( year day -- timestamp ) 1 swap <date> ; inline
512 : <february> ( year day -- timestamp ) 2 swap <date> ; inline
513 : <march> ( year day -- timestamp ) 3 swap <date> ; inline
514 : <april> ( year day -- timestamp ) 4 swap <date> ; inline
515 : <may> ( year day -- timestamp ) 5 swap <date> ; inline
516 : <june> ( year day -- timestamp ) 6 swap <date> ; inline
517 : <july> ( year day -- timestamp ) 7 swap <date> ; inline
518 : <august> ( year day -- timestamp ) 8 swap <date> ; inline
519 : <september> ( year day -- timestamp ) 9 swap <date> ; inline
520 : <october> ( year day -- timestamp ) 10 swap <date> ; inline
521 : <november> ( year day -- timestamp ) 11 swap <date> ; inline
522 : <december> ( year day -- timestamp ) 12 swap <date> ; inline
523
524 : sunday ( timestamp -- new-timestamp ) 0 day-this-week ;
525 : monday ( timestamp -- new-timestamp ) 1 day-this-week ;
526 : tuesday ( timestamp -- new-timestamp ) 2 day-this-week ;
527 : wednesday ( timestamp -- new-timestamp ) 3 day-this-week ;
528 : thursday ( timestamp -- new-timestamp ) 4 day-this-week ;
529 : friday ( timestamp -- new-timestamp ) 5 day-this-week ;
530 : saturday ( timestamp -- new-timestamp ) 6 day-this-week ;
531
532 : sunday? ( timestamp -- ? ) day-of-week 0 = ;
533 : monday? ( timestamp -- ? ) day-of-week 1 = ;
534 : tuesday? ( timestamp -- ? ) day-of-week 2 = ;
535 : wednesday? ( timestamp -- ? ) day-of-week 3 = ;
536 : thursday? ( timestamp -- ? ) day-of-week 4 = ;
537 : friday? ( timestamp -- ? ) day-of-week 5 = ;
538 : saturday? ( timestamp -- ? ) day-of-week 6 = ;
539
540 : sunday-of-month ( timestamp n -- new-timestamp ) 0 nth-day-this-month ;
541 : monday-of-month ( timestamp n -- new-timestamp ) 1 nth-day-this-month ;
542 : tuesday-of-month ( timestamp n -- new-timestamp ) 2 nth-day-this-month ;
543 : wednesday-of-month ( timestamp n -- new-timestamp ) 3 nth-day-this-month ;
544 : thursday-of-month ( timestamp n -- new-timestamp ) 4 nth-day-this-month ;
545 : friday-of-month ( timestamp n -- new-timestamp ) 5 nth-day-this-month ;
546 : saturday-of-month ( timestamp n -- new-timestamp ) 6 nth-day-this-month ;
547
548 : last-sunday-of-month ( timestamp -- new-timestamp ) 0 last-day-this-month ;
549 : last-monday-of-month ( timestamp -- new-timestamp ) 1 last-day-this-month ;
550 : last-tuesday-of-month ( timestamp -- new-timestamp ) 2 last-day-this-month ;
551 : last-wednesday-of-month ( timestamp -- new-timestamp ) 3 last-day-this-month ;
552 : last-thursday-of-month ( timestamp -- new-timestamp ) 4 last-day-this-month ;
553 : last-friday-of-month ( timestamp -- new-timestamp ) 5 last-day-this-month ;
554 : last-saturday-of-month ( timestamp -- new-timestamp ) 6 last-day-this-month ;
555
556 : beginning-of-week ( timestamp -- new-timestamp )
557     midnight sunday ;
558
559 : o'clock ( timestamp n -- new-timestamp )
560     [ midnight ] dip >>hour ;
561
562 : am ( timestamp n -- new-timestamp )
563     0 12 [a,b] check-interval o'clock ;
564
565 : pm ( timestamp n -- new-timestamp )
566     0 12 [a,b] check-interval 12 + o'clock ;
567
568 : time-since-midnight ( timestamp -- duration )
569     dup midnight time- ; inline
570
571 : since-1970 ( duration -- timestamp )
572     unix-1970 time+ ; inline
573
574 : timestamp>unix-time ( timestamp -- seconds )
575     unix-1970 (time-) ; inline
576
577 : unix-time>timestamp ( seconds -- timestamp )
578     [ unix-1970 ] dip +second ; inline
579
580 : (week-number) ( timestamp -- [0,53] )
581     [ day-of-year ] [ day-of-week [ 7 ] when-zero ] bi - 10 + 7 /i ;
582
583 : week-number ( timestamp -- [1,53] )
584     dup (week-number) {
585         {  0 [ year>> 1 - end-of-year (week-number) ] }
586         { 53 [ year>> 1 + <year> (week-number) 1 = 1 53 ? ] }
587         [ nip ]
588     } case ;
589
590 : quarter ( timestamp -- [1,4] )
591     month>> 3 /mod [ drop 1 + ] unless-zero ; inline
592
593 ! January and February need a fixup with this algorithm.
594 ! Find a better algorithm.
595 : ymd>ordinal ( year month day -- ordinal )
596     [ leap-year? dup -2 -3 ? ]
597     [ tuck dup 3 < [ 12 + ] when [ 1 - 30 * ] [ 1 + .6 * floor ] bi + ]
598     [ ] tri* + + >integer
599     rot 366 365 ?
600     rot 3 < [ - ] [ drop ] if ;
601
602 : timestamp>year-dates ( timestamp -- seq )
603     [ beginning-of-year >date< julian-day-number ]
604     [ days-in-year ] bi
605     [ drop ] [ + ] 2bi
606     R:[a,b) [ julian-day-number>date <date> ] map ;
607
608 : year-ordinal>timestamp ( year ordinal -- timestamp )
609     [ 1 1 julian-day-number ] dip
610     + 1 - julian-day-number>date <date> ;
611
612 GENERIC: weeks-in-week-year ( obj -- n )
613 M: integer weeks-in-week-year
614     { [ 1 1 <date> thursday? ] [ 12 31 <date> thursday? ] } 1|| 53 52 ? ;
615
616 M: timestamp weeks-in-week-year
617     { [ january 1 >>day thursday? ] [ december 31 >>day thursday? ] } 1|| 53 52 ? ;
618
619 {
620     { [ os unix? ] [ "calendar.unix" ] }
621     { [ os windows? ] [ "calendar.windows" ] }
622 } cond require
623
624 { "threads" "calendar" } "calendar.threads" require-when