-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrfi-19.scm
1612 lines (1391 loc) · 56.1 KB
/
srfi-19.scm
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; SRFI-19: Time Data Types and Procedures.
;;
;; Original work Copyright (C) I/NET, Inc. (2000, 2002, 2003). All Rights Reserved.
;; Modified work Copyright (C) 2017 Geoff Taylor.
;;
;; Permission is hereby granted, free of charge, to any person obtaining
;; a copy of this software and associated documentation files (the
;; "Software"), to deal in the Software without restriction, including
;; without limitation the rights to use, copy, modify, merge, publish,
;; distribute, sublicense, and/or sell copies of the Software, and to
;; permit persons to whom the Software is furnished to do so, subject to
;; the following conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;; -- Bug fixes.
;;
;; MAKE-TIME had parameters seconds and nanoseconds reversed; change all
;; references in file to match. Will F: 2002-10-15
;;
;; DATE-YEAR-DAY returned the wrong day; tm:year-day fixed to do the right
;; thing. Will F: 2002-10-15
;; It also called an undefined error procedure.
;;
;; DISPLAYING procedure removed. Will F: 2002-10-15.
;;
;; TM:NANO constant corrected. 2002-11-04.
;;
;; The following fixes by Will Fitzgerald, February, 2003.
;; -- Thanks to Steven Ma and others.
;;
;; (CURRENT-TIME 'TIME-THREAD) added.
;;
;; TIME-RESOLUTION for TIME-PROCESS added.
;;
;; TIME comparison procedures (time=?, etc. fixed.
;;
;; Corrected errors in converting between TAI and UTC time.
;;
;; TAI and UTC date converters no longer look at leap seconds,
;; which was an error.
;;
;; corrections to calls to tm:time-error
;;
;; timezone offset not used in date->time-utc and date->julian-day
;;
;; typos in tm:integer-reader-exact, tm:string->date,
;; time-monotonic->time-utc!, tm:char->int fixed
;;
;; corrected "~k", "~f" formatting for date->string (includes fix for
;; "~4"
;;
;; 'split-real' fixed.
;;
;; fixed julian-day->time-utc and variants.
;;
;; changes 2003-02-26, based on comments by Martin Gasbichler.
;;
;; moronic, overly complicated COPY-TIME procedure changed
;; to simple version suggested by Martin Gasbichler.
;;
;; To provide more portability, changed #\Space to #\space
;; and #\tab to #\Tab to (integer->char 9)
;;
;; changed arity-3 calls to / and - to arity 2 calls (again,
;; for more general portability).
;;
;; split-real fixed again -- by removing it, and using
;; 'fractional part'. Will Fitzgerald 5/16/2003.
;; --------------------------------------------------------------
;; Implement Chibi-specific bridge code for Chibi Scheme, otherwise use the
;; standard implementation.
(cond-expand
(chibi
;; Some functions the SRFI expects to have available
(define current-seconds chibi:current-seconds)
(define (current-milliseconds)
(/ (current-jiffy) (/ (jiffies-per-second) 1000)))
(define (seconds->date value)
(chibi:seconds->time value))
(define (date-time-zone-offset value)
(chibi:time-offset value))
(define inexact->exact exact)
(define exact->inexact inexact)
(define (current-process-milliseconds)
(floor (/ (chibi:timeval-microseconds
(chibi:resource-usage-time (chibi:get-resource-usage))) 1000)))
;; I don't see any way to access garbage collection time in Chibi, and it's only
;; mentioned as an extension to the SRFI, not a requirement, so just throw an
;; error if someone tries to use it.
(define (current-gc-milliseconds)
(error "Not implemented."))
(define-record-type time
(_make-time type nanosecond second)
time?
(type time-type set-time-type!)
(nanosecond time-nanosecond set-time-nanosecond!)
(second time-second set-time-second!))
;; Sometimes make-time is called with #f for one or more parameters, so we need to handle that.
(define (boolean-safe-div x y)
(if (and x y)
(quotient x y)
#f))
(define (boolean-safe-mod x y)
(if (and x y)
(remainder x y)
#f))
(define (boolean-safe-plus x y)
(if (and x y)
(+ x y)
#f))
(define (make-time type nanosecond second)
(let* ((seconds-adjustment (boolean-safe-div nanosecond tm:nano))
(nsec (boolean-safe-mod nanosecond tm:nano))
(seconds (boolean-safe-plus second seconds-adjustment)))
(_make-time type nsec seconds)))
(define-record-type date
(make-date nanosecond second minute hour day month year zone-offset)
date?
(nanosecond date-nanosecond set-date-nanosecond!)
(second date-second set-date-second!)
(minute date-minute set-date-minute!)
(hour date-hour set-date-hour!)
(day date-day set-date-day!)
(month date-month set-date-month!)
(year date-year set-date-year!)
(zone-offset date-zone-offset set-date-zone-offset!)))
(else
;; Simple R&RS approximation of time in process. It's very rough, so other schemes
;; should probably use their own primitives to come up with more accurate values.
(define (current-milliseconds)
(/ (current-jiffy) (/ (jiffies-per-second) 1000)))
(define srfi19-load-time (current-milliseconds))
(define (current-process-milliseconds)
(- (current-milliseconds) srfi19-load-time))
;; Moved from later in the file to be in our single cond-expand.
;; ----8<--------8<--------8<--------8<----
;;; the time structure; creates the accessors, too.
;;; wf: changed to match srfi documentation. uses mzscheme structures & inspectors
(define-struct time (type nanosecond second) (make-inspector))
;; thanks, Martin Gasbichler ...
;; ----8<--------8<--------8<--------8<----
;; End of moved block.
;; Moved from later in the file to be in our single cond-expand.
;; ----8<--------8<--------8<--------8<----
;; -- date structures
(define-struct date (nanosecond second minute hour day month year zone-offset) (make-inspector))
;; ----8<--------8<--------8<--------8<----
;; End of moved block.
;; Moved from later in the file to be in our single cond-expand.
;; ----8<--------8<--------8<--------8<----
(define (set-date-second! date val)
(tm:time-error 'set-date-second! 'dates-are-immutable date))
(define (set-date-minute! date val)
(tm:time-error 'set-date-minute! 'dates-are-immutable date))
(define (set-date-day! date val)
(tm:time-error 'set-date-day! 'dates-are-immutable date))
(define (set-date-month! date val)
(tm:time-error 'set-date-month! 'dates-are-immutable date))
(define (set-date-year! date val)
(tm:time-error 'set-date-year! 'dates-are-immutable date))
(define (set-date-zone-offset! date val)
(tm:time-error 'set-date-zone-offset! 'dates-are-immutable date))
;; ----8<--------8<--------8<--------8<----
;; End of moved block.
))
;; End of cond-expand.
(define-syntax receive
(syntax-rules ()
((receive formals expression body ...)
(call-with-values (lambda () expression)
(lambda formals body ...)))))
;;; -- we want receive later on for a couple of small things
;;
;; :OPTIONAL is nice, too
(define-syntax :optional
(syntax-rules ()
((_ val default-value)
(if (null? val) default-value (car val)))))
;;> Symbol representing TAI time.
(define time-tai 'time-tai)
;;> Symbol representting UTC time.
(define time-utc 'time-utc)
;;> Symbol representing monotonic time.
(define time-monotonic 'time-monotonic)
;;> Symbol representing time spent in current thread.
(define time-thread 'time-thread)
;;> Symbol representing time spent in current process.
(define time-process 'time-process)
;;> Symbol representing Time duration.
(define time-duration 'time-duration)
;; example of extension (MZScheme specific)
(define time-gc 'time-gc)
;;-- LOCALE dependent constants
(define tm:locale-number-separator ".")
(define tm:locale-abbr-weekday-vector (vector "Sun" "Mon" "Tue" "Wed"
"Thu" "Fri" "Sat"))
(define tm:locale-long-weekday-vector (vector "Sunday" "Monday"
"Tuesday" "Wednesday"
"Thursday" "Friday"
"Saturday"))
;; note empty string in 0th place.
(define tm:locale-abbr-month-vector (vector "" "Jan" "Feb" "Mar"
"Apr" "May" "Jun" "Jul"
"Aug" "Sep" "Oct" "Nov"
"Dec"))
(define tm:locale-long-month-vector (vector "" "January" "February"
"March" "April" "May"
"June" "July" "August"
"September" "October"
"November" "December"))
(define tm:ordinal-suffix-alist '((1 . "st") (2 . "nd") (3 . "rd")
(21 . "st") (22 . "nd") (23 . "rd")
(31 . "st")))
(define tm:locale-pm "PM")
(define tm:locale-am "AM")
;; See date->string
(define tm:locale-date-time-format "~a ~b ~d ~H:~M:~S~z ~Y")
(define tm:locale-short-date-format "~m/~d/~y")
(define tm:locale-time-format "~H:~M:~S")
(define tm:iso-8601-date-time-format "~Y-~m-~dT~H:~M:~S~z")
;;-- Miscellaneous Constants.
;;-- only the tm:tai-epoch-in-jd might need changing if
;; a different epoch is used.
(define tm:nano (expt 10 9))
(define tm:sid 86400) ; seconds in a day
(define tm:sihd 43200) ; seconds in a half day
(define tm:tai-epoch-in-jd 4881175/2) ; julian day number for 'the epoch'
;;; A Very simple Error system for the time procedures
;;;
(define tm:time-error-types
'(invalid-clock-type
unsupported-clock-type
incompatible-time-types
not-duration
dates-are-immutable
bad-date-format-string
bad-date-template-string
invalid-month-specification
))
(define (tm:time-error caller type value)
(if (member type tm:time-error-types)
(if value
(error caller "TIME-ERROR type ~S: ~S" type value)
(error caller "TIME-ERROR type ~S" type))
(error caller "TIME-ERROR unsupported error type ~S" type)))
;; A table of leap seconds
;; See ftp://maia.usno.navy.mil/ser7/tai-utc.dat
;; and update as necessary.
;; this procedures reads the file in the abover
;; format and creates the leap second table
;; it also calls the almost standard, but not R5 procedures read-line
;; & open-input-string
;; ie (set! tm:leap-second-table (tm:read-tai-utc-date "tai-utc.dat"))
(define (tm:read-tai-utc-data filename)
(define (convert-jd jd)
(* (- (inexact->exact jd) tm:tai-epoch-in-jd) tm:sid))
(define (convert-sec sec)
(inexact->exact sec))
(let ( (port (open-input-file filename))
(table '()) )
(let loop ((line (read-line port)))
;; Fix for Chibi version - use (eof-object? line) instead of (eq? line eof) since
;; Chibi doesn't define eof symbol.
(if (not (eof-object? line))
(begin
(let* ( (data (read (open-input-string (string-append "(" line ")"))))
(year (car data))
(jd (cadddr (cdr data)))
(secs (cadddr (cdddr data))) )
(if (>= year 1972)
(set! table (cons (cons (convert-jd jd) (convert-sec secs)) table)))
(loop (read-line port))))))
table))
;; each entry is ( utc seconds since epoch . # seconds to add for tai )
;; note they go higher to lower, and end in 1972.
(define tm:leap-second-table
'((1483228800 . 37)
(1435708800 . 36)
(1341100800 . 35)
(1230768000 . 34)
(1136073600 . 33)
(915148800 . 32)
(867715200 . 31)
(820454400 . 30)
(773020800 . 29)
(741484800 . 28)
(709948800 . 27)
(662688000 . 26)
(631152000 . 25)
(567993600 . 24)
(489024000 . 23)
(425865600 . 22)
(394329600 . 21)
(362793600 . 20)
(315532800 . 19)
(283996800 . 18)
(252460800 . 17)
(220924800 . 16)
(189302400 . 15)
(157766400 . 14)
(126230400 . 13)
(94694400 . 12)
(78796800 . 11)
(63072000 . 10)))
(define (read-leap-second-table filename)
(set! tm:leap-second-table (tm:read-tai-utc-data filename))
(values))
(define (tm:leap-second-delta utc-seconds)
(letrec ( (lsd (lambda (table)
(cond
((>= utc-seconds (caar table))
(cdar table))
(else (lsd (cdr table)))))) )
(if (< utc-seconds (* (- 1972 1970) 365 tm:sid)) 0
(lsd tm:leap-second-table))))
;; going from tai seconds to utc seconds ...
(define (tm:leap-second-neg-delta tai-seconds)
(letrec ( (lsd (lambda (table)
(cond ((null? table) 0)
((<= (cdar table) (- tai-seconds (caar table)))
(cdar table))
(else (lsd (cdr table)))))) )
(if (< tai-seconds (* (- 1972 1970) 365 tm:sid)) 0
(lsd tm:leap-second-table))))
;;> \procedure{(copy-time time)}
;;> Creates a new time object, with the same time \var{type}, \var{nanosecond}, and \var{second} as \var{time}.
(define (copy-time time)
(make-time (time-type time)
(time-nanosecond time)
(time-second time)
))
;;; current-time
;;; specific time getters.
;;; these should be rewritten to be os specific.
;;
;; -- using gnu gettimeofday() would be useful here -- gets
;; second + millisecond
;; let's pretend we do, using mzscheme's current-seconds & current-milliseconds
;; this is supposed to return utc.
;;
(define (tm:get-time-of-day)
(values (current-seconds)
(abs (remainder (current-milliseconds) 1000))))
(define (tm:current-time-utc)
(receive (seconds ms) (tm:get-time-of-day)
(make-time time-utc (* ms 10000) seconds )))
(define (tm:current-time-tai)
(receive (seconds ms) (tm:get-time-of-day)
(make-time time-tai
(* ms 10000)
(+ seconds (tm:leap-second-delta seconds))
)))
(define (tm:current-time-ms-time time-type proc)
(let ((current-ms (proc)))
(make-time time-type
(* (remainder current-ms 1000) 10000)
(quotient current-ms 10000)
)))
;; -- we define it to be the same as tai.
;; a different implemation of current-time-montonic
;; will require rewriting all of the time-monotonic converters,
;; of course.
(define (tm:current-time-monotonic)
(receive (seconds ms) (tm:get-time-of-day)
(make-time time-monotonic
(* ms 10000)
(+ seconds (tm:leap-second-delta seconds))
)))
(define (tm:current-time-thread)
(tm:current-time-ms-time time-process current-process-milliseconds))
(define (tm:current-time-process)
(tm:current-time-ms-time time-process current-process-milliseconds))
(define (tm:current-time-gc)
(tm:current-time-ms-time time-gc current-gc-milliseconds))
(define (current-time . clock-type)
(let ( (clock-type (:optional clock-type time-utc)) )
(cond
((eq? clock-type time-tai) (tm:current-time-tai))
((eq? clock-type time-utc) (tm:current-time-utc))
((eq? clock-type time-monotonic) (tm:current-time-monotonic))
((eq? clock-type time-thread) (tm:current-time-thread))
((eq? clock-type time-process) (tm:current-time-process))
((eq? clock-type time-gc) (tm:current-time-gc))
(else (tm:time-error 'current-time 'invalid-clock-type clock-type)))))
;; -- time resolution
;; this is the resolution of the clock in nanoseconds.
;; this will be implementation specific.
(define (time-resolution . clock-type)
(let ((clock-type (:optional clock-type time-utc)))
(cond
((eq? clock-type time-tai) 10000)
((eq? clock-type time-utc) 10000)
((eq? clock-type time-monotonic) 10000)
((eq? clock-type time-thread) 10000)
((eq? clock-type time-process) 10000)
((eq? clock-type time-gc) 10000)
(else (tm:time-error 'time-resolution 'invalid-clock-type clock-type)))))
;; -- time comparisons
(define (tm:time-compare-check time1 time2 caller)
(if (or (not (and (time? time1) (time? time2)))
(not (eq? (time-type time1) (time-type time2))))
(tm:time-error caller 'incompatible-time-types #f)
#t))
(define (time=? time1 time2)
(tm:time-compare-check time1 time2 'time=?)
(and (= (time-second time1) (time-second time2))
(= (time-nanosecond time1) (time-nanosecond time2))))
(define (time>? time1 time2)
(tm:time-compare-check time1 time2 'time>?)
(or (> (time-second time1) (time-second time2))
(and (= (time-second time1) (time-second time2))
(> (time-nanosecond time1) (time-nanosecond time2)))))
(define (time<? time1 time2)
(tm:time-compare-check time1 time2 'time<?)
(or (< (time-second time1) (time-second time2))
(and (= (time-second time1) (time-second time2))
(< (time-nanosecond time1) (time-nanosecond time2)))))
(define (time>=? time1 time2)
(tm:time-compare-check time1 time2 'time>=?)
(or (> (time-second time1) (time-second time2))
(and (= (time-second time1) (time-second time2))
(>= (time-nanosecond time1) (time-nanosecond time2)))))
(define (time<=? time1 time2)
(tm:time-compare-check time1 time2 'time<=?)
(or (< (time-second time1) (time-second time2))
(and (= (time-second time1) (time-second time2))
(<= (time-nanosecond time1) (time-nanosecond time2)))))
;; -- time arithmetic
(define (tm:time->nanoseconds time)
(define (sign1 n)
(if (negative? n) -1 1))
(+ (* (time-second time) tm:nano)
(time-nanosecond time)))
(define (tm:nanoseconds->time time-type nanoseconds)
(make-time time-type
(remainder nanoseconds tm:nano)
(quotient nanoseconds tm:nano)))
(define (tm:nanoseconds->values nanoseconds)
(values (abs (remainder nanoseconds tm:nano))
(quotient nanoseconds tm:nano)))
(define (tm:time-difference time1 time2 time3)
(if (or (not (and (time? time1) (time? time2)))
(not (eq? (time-type time1) (time-type time2))))
(tm:time-error 'time-difference 'incompatible-time-types #f))
(set-time-type! time3 time-duration)
(if (time=? time1 time2)
(begin
(set-time-second! time3 0)
(set-time-nanosecond! time3 0))
(receive
(nanos secs)
(tm:nanoseconds->values (- (tm:time->nanoseconds time1)
(tm:time->nanoseconds time2)))
(set-time-second! time3 secs)
(set-time-nanosecond! time3 nanos)))
time3)
(define (time-difference time1 time2)
(tm:time-difference time1 time2 (make-time #f #f #f)))
(define (time-difference! time1 time2)
(tm:time-difference time1 time2 time1))
(define (tm:add-duration time1 duration time3)
(if (not (and (time? time1) (time? duration)))
(tm:time-error 'add-duration 'incompatible-time-types #f))
(if (not (eq? (time-type duration) time-duration))
(tm:time-error 'add-duration 'not-duration duration)
(let ( (sec-plus (+ (time-second time1) (time-second duration)))
(nsec-plus (+ (time-nanosecond time1) (time-nanosecond duration))) )
(let ((r (remainder nsec-plus tm:nano))
(q (quotient nsec-plus tm:nano)))
; (set-time-type! time3 (time-type time1))
(if (negative? r)
(begin
(set-time-second! time3 (+ sec-plus q -1))
(set-time-nanosecond! time3 (+ tm:nano r)))
(begin
(set-time-second! time3 (+ sec-plus q))
(set-time-nanosecond! time3 r)))
time3))))
(define (add-duration time1 duration)
(tm:add-duration time1 duration (make-time (time-type time1) #f #f)))
(define (add-duration! time1 duration)
(tm:add-duration time1 duration time1))
(define (tm:subtract-duration time1 duration time3)
(if (not (and (time? time1) (time? duration)))
(tm:time-error 'add-duration 'incompatible-time-types #f))
(if (not (eq? (time-type duration) time-duration))
(tm:time-error 'tm:subtract-duration 'not-duration duration)
(let ( (sec-minus (- (time-second time1) (time-second duration)))
(nsec-minus (- (time-nanosecond time1) (time-nanosecond duration))) )
(let ((r (remainder nsec-minus tm:nano))
(q (quotient nsec-minus tm:nano)))
(if (negative? r)
(begin
(set-time-second! time3 (- sec-minus q 1))
(set-time-nanosecond! time3 (+ tm:nano r)))
(begin
(set-time-second! time3 (- sec-minus q))
(set-time-nanosecond! time3 r)))
time3))))
(define (subtract-duration time1 duration)
(tm:subtract-duration time1 duration (make-time (time-type time1) #f #f)))
(define (subtract-duration! time1 duration)
(tm:subtract-duration time1 duration time1))
;; -- converters between types.
(define (tm:time-tai->time-utc! time-in time-out caller)
(if (not (eq? (time-type time-in) time-tai))
(tm:time-error caller 'incompatible-time-types time-in))
(set-time-type! time-out time-utc)
(set-time-nanosecond! time-out (time-nanosecond time-in))
(set-time-second! time-out (- (time-second time-in)
(tm:leap-second-neg-delta
(time-second time-in))))
time-out)
(define (time-tai->time-utc time-in)
(tm:time-tai->time-utc! time-in (make-time #f #f #f) 'time-tai->time-utc))
(define (time-tai->time-utc! time-in)
(tm:time-tai->time-utc! time-in time-in 'time-tai->time-utc!))
(define (tm:time-utc->time-tai! time-in time-out caller)
(if (not (eq? (time-type time-in) time-utc))
(tm:time-error caller 'incompatible-time-types time-in))
(set-time-type! time-out time-tai)
(set-time-nanosecond! time-out (time-nanosecond time-in))
(set-time-second! time-out (+ (time-second time-in)
(tm:leap-second-delta
(time-second time-in))))
time-out)
(define (time-utc->time-tai time-in)
(tm:time-utc->time-tai! time-in (make-time #f #f #f) 'time-utc->time-tai))
(define (time-utc->time-tai! time-in)
(tm:time-utc->time-tai! time-in time-in 'time-utc->time-tai!))
;; -- these depend on time-monotonic having the same definition as time-tai!
(define (time-monotonic->time-utc time-in)
(if (not (eq? (time-type time-in) time-monotonic))
(tm:time-error 'time-monotoinc->time-utc 'incompatible-time-types time-in))
(let ((ntime (copy-time time-in)))
(set-time-type! ntime time-tai)
(tm:time-tai->time-utc! ntime ntime 'time-monotonic->time-utc)))
(define (time-monotonic->time-utc! time-in)
(if (not (eq? (time-type time-in) time-monotonic))
(tm:time-error 'time-monotonic->time-utc! 'incompatible-time-types time-in))
(set-time-type! time-in time-tai)
(tm:time-tai->time-utc! time-in time-in 'time-monotonic->time-utc))
(define (time-monotonic->time-tai time-in)
(if (not (eq? (time-type time-in) time-monotonic))
(tm:time-error 'time-monotonic->time-tai 'incompatible-time-types time-in))
(let ((ntime (copy-time time-in)))
(set-time-type! ntime time-tai)
ntime))
(define (time-monotonic->time-tai! time-in)
(if (not (eq? (time-type time-in) time-monotonic))
(tm:time-error 'time-monotonic->time-tai! 'incompatible-time-types time-in))
(set-time-type! time-in time-tai)
time-in)
(define (time-utc->time-monotonic time-in)
(if (not (eq? (time-type time-in) time-utc))
(tm:time-error 'time-utc->time-monotonic 'incompatible-time-types time-in))
(let ((ntime (tm:time-utc->time-tai! time-in (make-time #f #f #f)
'time-utc->time-monotonic)))
(set-time-type! ntime time-monotonic)
ntime))
(define (time-utc->time-monotonic! time-in)
(if (not (eq? (time-type time-in) time-utc))
(tm:time-error 'time-utc->time-montonic! 'incompatible-time-types time-in))
(let ((ntime (tm:time-utc->time-tai! time-in time-in
'time-utc->time-monotonic!)))
(set-time-type! ntime time-monotonic)
ntime))
(define (time-tai->time-monotonic time-in)
(if (not (eq? (time-type time-in) time-tai))
(tm:time-error 'time-tai->time-monotonic 'incompatible-time-types time-in))
(let ((ntime (copy-time time-in)))
(set-time-type! ntime time-monotonic)
ntime))
(define (time-tai->time-monotonic! time-in)
(if (not (eq? (time-type time-in) time-tai))
(tm:time-error 'time-tai->time-monotonic! 'incompatible-time-types time-in))
(set-time-type! time-in time-monotonic)
time-in)
;; redefine setters
(define tm:set-date-nanosecond! set-date-nanosecond!)
(define tm:set-date-second! set-date-second!)
(define tm:set-date-minute! set-date-minute!)
(define tm:set-date-hour! set-date-hour!)
(define tm:set-date-day! set-date-day!)
(define tm:set-date-month! set-date-month!)
(define tm:set-date-year! set-date-year!)
(define tm:set-date-zone-offset! set-date-zone-offset!)
;; gives the julian day which starts at noon.
(define (tm:encode-julian-day-number day month year)
(let* ((a (quotient (- 14 month) 12))
(y (- (- (+ year 4800) a) (if (negative? year) -1 0)))
(m (- (+ month (* 12 a)) 3)))
(+ day
(quotient (+ (* 153 m) 2) 5)
(* 365 y)
(quotient y 4)
(- (quotient y 100))
(quotient y 400)
-32045)))
(define (tm:char-pos char str index len)
(cond
((>= index len) #f)
((char=? (string-ref str index) char)
index)
(else
(tm:char-pos char str (+ index 1) len))))
(define (tm:fractional-part r)
(if (integer? r) "0"
(let ((str (number->string (exact->inexact r))))
(let ((ppos (tm:char-pos #\. str 0 (string-length str))))
(substring str (+ ppos 1) (string-length str))))))
;; gives the seconds/date/month/year
(define (tm:decode-julian-day-number jdn)
(let* ((days (truncate jdn))
(a (+ days 32044))
(b (quotient (+ (* 4 a) 3) 146097))
(c (- a (quotient (* 146097 b) 4)))
(d (quotient (+ (* 4 c) 3) 1461))
(e (- c (quotient (* 1461 d) 4)))
(m (quotient (+ (* 5 e) 2) 153))
(y (+ (* 100 b) d -4800 (quotient m 10))))
(values ; seconds date month year
(* (- jdn days) tm:sid)
(+ e (- (quotient (+ (* 153 m) 2) 5)) 1)
(+ m 3 (* -12 (quotient m 10)))
(if (>= 0 y) (- y 1) y))
))
;; relies on the fact that we named our time zone accessor
;; differently from MzScheme's....
;; This should be written to be OS specific.
(define (tm:local-tz-offset)
(date-time-zone-offset (seconds->date (current-seconds))))
;; special thing -- ignores nanos
(define (tm:time->julian-day-number seconds tz-offset)
(+ (/ (+ seconds
tz-offset
tm:sihd)
tm:sid)
tm:tai-epoch-in-jd))
(define (tm:find proc l)
(if (null? l)
#f
(if (proc (car l))
#t
(tm:find proc (cdr l)))))
(define (tm:tai-before-leap-second? second)
(tm:find (lambda (x)
(= second (- (+ (car x) (cdr x)) 1)))
tm:leap-second-table))
(define (tm:time->date time tz-offset ttype)
(if (not (eq? (time-type time) ttype))
(tm:time-error 'time->date 'incompatible-time-types time))
(let* ( (offset (:optional tz-offset (tm:local-tz-offset))) )
(receive (secs date month year)
(tm:decode-julian-day-number
(tm:time->julian-day-number (time-second time) offset))
(let* ( (hours (quotient secs (* 60 60)))
(rem (remainder secs (* 60 60)))
(minutes (quotient rem 60))
(seconds (remainder rem 60)) )
(make-date (time-nanosecond time)
seconds
minutes
hours
date
month
year
offset)))))
(define (time-tai->date time . tz-offset)
(if (tm:tai-before-leap-second? (time-second time))
;; if it's *right* before the leap, we need to pretend to subtract a second ...
(let ((d (tm:time->date (subtract-duration! (time-tai->time-utc time) (make-time time-duration 0 1)) tz-offset time-utc)))
(tm:set-date-second! d 60)
d)
(tm:time->date (time-tai->time-utc time) tz-offset time-utc)))
(define (time-utc->date time . tz-offset)
(tm:time->date time tz-offset time-utc))
;; again, time-monotonic is the same as time tai
(define (time-monotonic->date time . tz-offset)
(tm:time->date time tz-offset time-monotonic))
(define (date->time-utc date)
(let ( (nanosecond (date-nanosecond date))
(second (date-second date))
(minute (date-minute date))
(hour (date-hour date))
(day (date-day date))
(month (date-month date))
(year (date-year date))
(offset (date-zone-offset date)) )
(let ( (jdays (- (tm:encode-julian-day-number day month year)
tm:tai-epoch-in-jd)) )
(make-time
time-utc
nanosecond
(+ (* (- jdays 1/2) 24 60 60)
(* hour 60 60)
(* minute 60)
second
(- offset))
))))
(define (date->time-tai d)
(if (= (date-second d) 60)
(subtract-duration! (time-utc->time-tai! (date->time-utc d)) (make-time time-duration 0 1))
(time-utc->time-tai! (date->time-utc d))))
(define (date->time-monotonic date)
(time-utc->time-monotonic! (date->time-utc date)))
(define (tm:leap-year? year)
(or (= (modulo year 400) 0)
(and (= (modulo year 4) 0) (not (= (modulo year 100) 0)))))
(define (leap-year? date)
(tm:leap-year? (date-year date)))
;; tm:year-day fixed: adding wrong number of days.
(define tm:month-assoc '((0 . 0) (1 . 31) (2 . 59) (3 . 90) (4 . 120)
(5 . 151) (6 . 181) (7 . 212) (8 . 243)
(9 . 273) (10 . 304) (11 . 334)))
(define (tm:year-day day month year)
(let ((days-pr (assoc (- month 1) tm:month-assoc)))
(if (not days-pr)
(tm:time-error 'date-year-day 'invalid-month-specification month))
(if (and (tm:leap-year? year) (> month 2))
(+ day (cdr days-pr) 1)
(+ day (cdr days-pr)))))
(define (date-year-day date)
(tm:year-day (date-day date) (date-month date) (date-year date)))
;; from calendar faq
(define (tm:week-day day month year)
(let* ((a (quotient (- 14 month) 12))
(y (- year a))
(m (+ month (* 12 a) -2)))
(modulo (+ day y (quotient y 4) (- (quotient y 100))
(quotient y 400) (quotient (* 31 m) 12))
7)))
(define (date-week-day date)
(tm:week-day (date-day date) (date-month date) (date-year date)))
(define (tm:days-before-first-week date day-of-week-starting-week)
(let* ( (first-day (make-date 0 0 0 0
1
1
(date-year date)
#f))
(fdweek-day (date-week-day first-day)) )
(modulo (- day-of-week-starting-week fdweek-day)
7)))
(define (date-week-number date day-of-week-starting-week)
(quotient (- (date-year-day date)
(tm:days-before-first-week date day-of-week-starting-week))
7))
(define (current-date . tz-offset)
(time-utc->date (current-time time-utc)
(:optional tz-offset (tm:local-tz-offset))))
;; given a 'two digit' number, find the year within 50 years +/-
(define (tm:natural-year n)
(let* ( (current-year (date-year (current-date)))
(current-century (* (quotient current-year 100) 100)) )
(cond
((>= n 100) n)
((< n 0) n)
((<= (- (+ current-century n) current-year) 50)
(+ current-century n))
(else
(+ (- current-century 100) n)))))
(define (date->julian-day date)
(let ( (nanosecond (date-nanosecond date))
(second (date-second date))
(minute (date-minute date))
(hour (date-hour date))
(day (date-day date))
(month (date-month date))
(year (date-year date))
(offset (date-zone-offset date)) )
(+ (tm:encode-julian-day-number day month year)
(- 1/2)
(+ (/ (/ (+ (* hour 60 60)
(* minute 60) second (/ nanosecond tm:nano)) tm:sid)
(- offset))))))
(define (date->modified-julian-day date)
(- (date->julian-day date)
4800001/2))
(define (time-utc->julian-day time)
(if (not (eq? (time-type time) time-utc))
(tm:time-error 'time-utc->julian-day 'incompatible-time-types time))
(+ (/ (+ (time-second time) (/ (time-nanosecond time) tm:nano))
tm:sid)
tm:tai-epoch-in-jd))
(define (time-utc->modified-julian-day time)
(- (time-utc->julian-day time)
4800001/2))
(define (time-tai->julian-day time)
(if (not (eq? (time-type time) time-tai))
(tm:time-error 'time-tai->julian-day 'incompatible-time-types time))
(+ (/ (+ (- (time-second time)
(tm:leap-second-delta (time-second time)))
(/ (time-nanosecond time) tm:nano))
tm:sid)
tm:tai-epoch-in-jd))
(define (time-tai->modified-julian-day time)
(- (time-tai->julian-day time)
4800001/2))
;; this is the same as time-tai->julian-day
(define (time-monotonic->julian-day time)
(if (not (eq? (time-type time) time-monotonic))
(tm:time-error 'time-monotonic->julian-day 'incompatible-time-types time))
(+ (/ (+ (- (time-second time)
(tm:leap-second-delta (time-second time)))
(/ (time-nanosecond time) tm:nano))
tm:sid)
tm:tai-epoch-in-jd))
(define (time-monotonic->modified-julian-day time)
(- (time-monotonic->julian-day time)
4800001/2))
(define (julian-day->time-utc jdn)
(let ( (nanosecs (* tm:nano tm:sid (- jdn tm:tai-epoch-in-jd))) )
(make-time time-utc
(remainder nanosecs tm:nano)
(floor (/ nanosecs tm:nano)))))
(define (julian-day->time-tai jdn)
(time-utc->time-tai! (julian-day->time-utc jdn)))
(define (julian-day->time-monotonic jdn)
(time-utc->time-monotonic! (julian-day->time-utc jdn)))
(define (julian-day->date jdn . tz-offset)
(let ((offset (:optional tz-offset (tm:local-tz-offset))))