time.scm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Mike Sperber
  3. ; Time
  4. (import-dynamic-externals "=scheme48external/posix")
  5. ;----------------
  6. ; Time - seconds since the epoch.
  7. (define-record-type time :time
  8. (make-time seconds)
  9. time?
  10. (seconds time-seconds))
  11. (define-record-discloser :time
  12. (lambda (time)
  13. (let ((string (time->string time)))
  14. (list 'time (substring string 0 (- (string-length string) 1))))))
  15. ; We need to make these in the outside world.
  16. (define-exported-binding "posix-time-type" :time)
  17. (define (time=? time1 time2)
  18. (= (time-seconds time1)
  19. (time-seconds time2)))
  20. (define (time<? time1 time2)
  21. (< (time-seconds time1)
  22. (time-seconds time2)))
  23. (define (time<=? time1 time2)
  24. (not (time<? time2 time1)))
  25. (define (time>? time1 time2)
  26. (time<? time2 time1))
  27. (define (time>=? time1 time2)
  28. (not (time<? time1 time2)))
  29. (import-lambda-definition-2 current-time () "posix_time")
  30. (import-lambda-definition-2 posix-time->string (time) "posix_ctime")
  31. (define (time->string t)
  32. (os-string->string
  33. (byte-vector->os-string
  34. (posix-time->string t))))
  35. ;----------------
  36. ; Dates - what a mess.
  37. (define-record-type date :date
  38. (make-date second minute hour month-day month year week-day year-day dst)
  39. date?
  40. (second date-second)
  41. (minute date-minute)
  42. (hour date-hour)
  43. (month-day date-month-day)
  44. (month date-month)
  45. (year date-year) ; Since 1900 (why?)
  46. (week-day date-week-day)
  47. (year-day date-year-day)
  48. (dst date-dst) ; #t, #f or unspecific
  49. ; (time-zone date-time-zone) ; maybe later
  50. )
  51. (define-record-discloser :date
  52. (lambda (r)
  53. (list 'date
  54. (let ((s (date->string r)))
  55. (substring s 0 (- (string-length s) 1))))))
  56. ; the C interface sees date objects as vectors
  57. (define (vector->date v)
  58. (apply make-date (vector->list v)))
  59. (define (date->vector d)
  60. (vector (date-second d)
  61. (date-minute d)
  62. (date-hour d)
  63. (date-month-day d)
  64. (date-month d)
  65. (date-year d)
  66. (date-week-day d)
  67. (date-year-day d)
  68. (date-dst d)))
  69. (import-lambda-definition-2 posix-date->string (date) "posix_asctime")
  70. (import-lambda-definition-2 posix-time->utc-date (time) "posix_gmtime")
  71. (import-lambda-definition-2 posix-time->local-date (time) "posix_localtime")
  72. (import-lambda-definition-2 posix-date->time (date) "posix_mktime")
  73. (import-lambda-definition-2 posix-strftime (format date) "posix_strftime")
  74. (define (date->string d)
  75. (os-string->string
  76. (byte-vector->os-string
  77. (posix-date->string (date->vector d)))))
  78. (define (time->utc-date t)
  79. (vector->date (posix-time->utc-date t)))
  80. (define (time->local-date t)
  81. (vector->date (posix-time->local-date t)))
  82. (define (date->time d)
  83. (posix-date->time (date->vector d)))
  84. (define (format-date format d)
  85. (os-string->string
  86. (byte-vector->os-string
  87. (posix-strftime (x->os-byte-vector format) (date->vector d)))))