On this page:
gen:  time-provider
time-provider?
->time
->hours
->minutes
->seconds
->milliseconds
->microseconds
->nanoseconds
on-date
8.12

8 Generic Time Operations🔗ℹ

An interface, implemented by time, datetime, and moment, that supplies generic operations on times.

procedure

(time-provider? x)  boolean

  x : any/c
Returns #t if x implements gen:time-provider; #f otherwise.

procedure

(->time t)  time?

  t : time-provider?
Returns the local time corresponding to t.

Examples:
> (->time (time 12 45 10))

#<time 12:45:10>

> (->time (datetime 1969 7 21 2 56))

#<time 02:56:00>

> (->time (moment 2015 3 8 1 #:tz "America/New_York"))

#<time 01:00:00>

procedure

(->hours t)  (integer-in 0 23)

  t : time-provider?
Returns the hour of the day from t.

Examples:
> (->hours (time 12 45 10))

12

> (->hours (datetime 1969 7 21 2 56))

2

> (->hours (moment 2015 3 8 1 #:tz "America/New_York"))

1

procedure

(->minutes t)  (integer-in 0 59)

  t : time-provider?
Returns the minute of the hour from t.

Examples:
> (->minutes (time 12 45 10))

45

> (->minutes (datetime 1969 7 21 2 56))

56

> (->minutes (moment 2015 3 8 1 #:tz "America/New_York"))

0

procedure

(->seconds t [fractional?])

  
(if fractional?
    (and/c rational? (>=/c 0) (</c 60))
    (integer-in 0 59))
  t : time-provider?
  fractional? : boolean? = #f
Returns the seconds from t. If fractional? is #t, then the fraction of the second will be included; otherwise, the result is an integer.

Examples:
> (->seconds (time 12 45 10 123456789))

10

> (->seconds (datetime 1969 7 21 2 56 30 123456789) #t)

30123456789/1000000000

> (->seconds (moment 2015 3 8 1 0 0 999999999 #:tz "America/New_York") #t)

999999999/1000000000

procedure

(->milliseconds t)  exact-integer?

  t : time-provider?
Returns the milliseconds from t.

Examples:
> (->milliseconds (time 12 45 10 123456789))

123

> (->milliseconds (datetime 1969 7 21 2 56 30 123456789))

123

> (->milliseconds (moment 2015 3 8 1 0 0 999999999 #:tz "America/New_York"))

999

procedure

(->microseconds t)  exact-integer?

  t : time-provider?
Returns the microseconds from t.

Examples:
> (->microseconds (time 12 45 10 123456789))

123456

> (->microseconds (datetime 1969 7 21 2 56 30 123456789))

123456

> (->microseconds (moment 2015 3 8 1 0 0 999999999 #:tz "America/New_York"))

999999

procedure

(->nanoseconds t)  exact-integer?

  t : time-provider?
Returns the nanoseconds from t.

Examples:
> (->nanoseconds (time 12 45 10 123456789))

123456789

> (->nanoseconds (datetime 1969 7 21 2 56 30 123456789))

123456789

> (->nanoseconds (moment 2015 3 8 1 0 0 999999999 #:tz "America/New_York"))

999999999

procedure

(on-date t d [#:resolve-offset resolve])  datetime-provider?

  t : time-provider?
  d : date-provider?
  resolve : offset-resolver/c = resolve-offset/raise
Combines the time fields of t with the date fields of d to produce a datetime provider. Any time zone information from t will be preserved in the result.

Examples:
> (on-date (time 0) (date 1970))

#<datetime 1970-01-01T00:00:00>

> (on-date (datetime 1 2 3 4 5 6 7) (datetime 2020 12 20))

#<datetime 2020-12-20T04:05:06.000000007>

> (on-date (moment 2000 1 1 2 #:tz "America/New_York")
           (date 2015 3 8)
           #:resolve-offset resolve-offset/post)

#<moment 2015-03-08T03:00:00-04:00[America/New_York]>