Time in Scheme

(import (scheme base)
        (scheme time))

(define (p . args)
  (for-each display args)
  (newline))

; We'll start by getting the current time.
(define now (current-time))
(p now)

; You can build a time struct by providing the
; year, month, day, etc. Times are always associated
; with a Location, i.e. time zone.
(define then (make-time time-utc 651387237 1258488898))
(p then)

; You can extract the various components of the time
; value as expected.
(p (time-year then))
(p (time-month then))
(p (time-day then))
(p (time-hour then))
(p (time-minute then))
(p (time-second then))
(p (time-nanosecond then))
(p (time-zone-name then))

; The Monday-Sunday Weekday is also available.
(p (time-weekday then))

; These procedures compare two times, testing if the
; first occurs before, after, or at the same time
; as the second, respectively.
(p (time<? then now))
(p (time>? then now))
(p (time=? then now))

; The time-difference procedure returns a time-duration representing
; the interval between two times.
(define diff (time-difference now then))
(p diff)

; We can compute the length of the duration in
; various units.
(p (time-duration->hours diff))
(p (time-duration->minutes diff))
(p (time-duration->seconds diff))
(p (time-duration->nanoseconds diff))

; You can use add-duration to advance a time by a given
; duration, or with a negative duration to move backwards.
(p (add-duration then diff))
(p (add-duration then (make-time time-duration 0 (- (time-second diff)))))

This Scheme code demonstrates time manipulation similar to the original Go example. Here’s a breakdown of the translation:

  1. We import the necessary modules: (scheme base) for basic Scheme functionality and (scheme time) for time-related procedures.

  2. We define a p procedure to mimic the fmt.Println functionality from Go.

  3. We use current-time to get the current time, equivalent to time.Now() in Go.

  4. To create a specific time, we use make-time with the time-utc type, providing nanoseconds and seconds since the epoch.

  5. We extract components of the time using procedures like time-year, time-month, etc.

  6. For comparisons, we use time<?, time>?, and time=?.

  7. We calculate the time difference using time-difference.

  8. To get duration in different units, we use procedures like time-duration->hours.

  9. Finally, we demonstrate adding durations with add-duration.

Note that Scheme’s time functionality might not be as extensive as Go’s, so some adaptations were made. For example, Scheme doesn’t have a built-in concept of time zones, so we use UTC throughout. Also, some specific functionalities like getting the weekday might require additional implementation in Scheme.

To run this program, you would typically save it to a file (e.g., time-example.scm) and run it with a Scheme interpreter that supports the time library, such as Chez Scheme:

$ chez --script time-example.scm

The output will depend on the current time when you run the script, but it will show similar information to the Go example, demonstrating various time manipulations and calculations.