@@ -807,6 +807,108 @@ wrong-type-arg
807807(check-true (undefined? (date-year (make-time TIME-UTC 0 0 ))))
808808(check-catch 'wrong-type-arg (date-zone-offset #f ))
809809
810+ ; ; ====================
811+ ; ; Time/Date Converters
812+ ; ; ====================
813+
814+ #|
815+ time-utc->date
816+ 将 TIME-UTC 时间对象转换为日期对象。
817+
818+ date->time-utc
819+ 将日期对象转换为 TIME-UTC 时间对象。
820+ |#
821+
822+ ; ; time-utc->date basic (UTC)
823+ (let* ((t (make-time TIME-UTC 0 0 ))
824+ (d (time-utc->date t 0 )))
825+ (check (date-year d) => 1970 )
826+ (check (date-month d) => 1 )
827+ (check (date-day d) => 1 )
828+ (check (date-hour d) => 0 )
829+ (check (date-minute d) => 0 )
830+ (check (date-second d) => 0 )
831+ (check (date-zone-offset d) => 0 ))
832+
833+ ; ; time-utc->date with positive tz offset (+8)
834+ (let* ((t (make-time TIME-UTC 0 0 ))
835+ (d (time-utc->date t 28800 )))
836+ (check (date-year d) => 1970 )
837+ (check (date-month d) => 1 )
838+ (check (date-day d) => 1 )
839+ (check (date-hour d) => 8 )
840+ (check (date-minute d) => 0 )
841+ (check (date-second d) => 0 )
842+ (check (date-zone-offset d) => 28800 ))
843+
844+ ; ; time-utc->date with negative tz offset (-1 hour)
845+ (let* ((t (make-time TIME-UTC 0 0 ))
846+ (d (time-utc->date t -3600)))
847+ (check (date-year d) => 1969 )
848+ (check (date-month d) => 12 )
849+ (check (date-day d) => 31 )
850+ (check (date-hour d) => 23 )
851+ (check (date-minute d) => 0 )
852+ (check (date-second d) => 0 )
853+ (check (date-zone-offset d) => -3600))
854+
855+ ; ; time-utc->date before 1970
856+ (let* ((t (make-time TIME-UTC 0 -1))
857+ (d (time-utc->date t 0 )))
858+ (check (date-year d) => 1969 )
859+ (check (date-month d) => 12 )
860+ (check (date-day d) => 31 )
861+ (check (date-hour d) => 23 )
862+ (check (date-minute d) => 59 )
863+ (check (date-second d) => 59 ))
864+
865+ ; ; time-utc->date negative day boundaries
866+ (let* ((t (make-time TIME-UTC 0 -86400))
867+ (d (time-utc->date t 0 )))
868+ (check (date-year d) => 1969 )
869+ (check (date-month d) => 12 )
870+ (check (date-day d) => 31 )
871+ (check (date-hour d) => 0 )
872+ (check (date-minute d) => 0 )
873+ (check (date-second d) => 0 ))
874+
875+ (let* ((t (make-time TIME-UTC 0 -86401))
876+ (d (time-utc->date t 0 )))
877+ (check (date-year d) => 1969 )
878+ (check (date-month d) => 12 )
879+ (check (date-day d) => 30 )
880+ (check (date-hour d) => 23 )
881+ (check (date-minute d) => 59 )
882+ (check (date-second d) => 59 ))
883+
884+ ; ; date->time-utc basic
885+ (let* ((d (make-date 0 0 0 8 1 1 1970 28800 ))
886+ (t (date->time-utc d)))
887+ (check (time-type t) => TIME-UTC)
888+ (check (time-second t) => 0 )
889+ (check (time-nanosecond t) => 0 ))
890+
891+ ; ; round-trip date -> time -> date with same tz-offset
892+ (let* ((d1 (make-date 123456789 45 30 14 25 12 2023 28800 ))
893+ (t (date->time-utc d1))
894+ (d2 (time-utc->date t (date-zone-offset d1))))
895+ (check (date-year d2) => (date-year d1))
896+ (check (date-month d2) => (date-month d1))
897+ (check (date-day d2) => (date-day d1))
898+ (check (date-hour d2) => (date-hour d1))
899+ (check (date-minute d2) => (date-minute d1))
900+ (check (date-second d2) => (date-second d1))
901+ (check (date-nanosecond d2) => (date-nanosecond d1))
902+ (check (date-zone-offset d2) => (date-zone-offset d1)))
903+
904+ ; ; converter error conditions
905+ (check-catch 'wrong-type-arg
906+ (time-utc->date (make-time TIME-TAI 0 0 ) 0 ))
907+ (check-catch 'wrong-type-arg
908+ (time-utc->date (make-time TIME-UTC 0 0 ) " bad-offset" ))
909+ (check-catch 'wrong-type-arg
910+ (date->time-utc " not-a-date" ))
911+
810912; ; ====================
811913; ; Date to String/String to Date Converters
812914; ; ====================
0 commit comments