On this page:
copy-date
time->date
time-utc?
time-tai?
time-duration?
leap-year?
days-in-month
date-valid?
date-day-of-the-week
date-week-day?
time->ago-string
seconds->ago-string
current-year
current-time-zone-offset
time-tai->string
time-utc->string

30 SRFI19 time utilities

 (require (planet untyped/unlib/time))
Utility procedures for use with SRFI 19 times and dates.

(copy-date date    
  [#:nanosecond nanosecond    
  #:second second    
  #:minute minute    
  #:hour hour    
  #:day day    
  #:month month    
  #:year year    
  #:zone-offset zone-offset])  srfi:date?
  date : srfi:date?
  nanosecond : (U integer #f) = #f
  second : (U integer #f) = #f
  minute : (U integer #f) = #f
  hour : (U integer #f) = #f
  day : (U integer #f) = #f
  month : (U integer #f) = #f
  year : (U integer #f) = #f
  zone-offset : (U integer #f) = #f
Creates a copy of date, substituting non-#f arguments for the values of the relevant fields.

(time->date time [tz])  srfi:date?
  time : (U time-tai? time-utc?)
  tz : integer? = (current-time-zone-offset)
Converts time to a date.

(time-utc? item)  boolean?
  item : any
Predicate that recognises SRFI-19 times of the time-utc type.

(time-tai? item)  boolean?
  item : any
Predicate that recognises SRFI-19 times of the time-tai type.

(time-duration? item)  boolean?
  item : any
Predicate that recognises SRFI-19 times of the time-duration type.

(leap-year? year)  boolean?
  year : integer?
Returns #t if year is a leap year.

(days-in-month month [year])  integer?
  month : integer?
  year : integer? = 2001
Returns the number of days in month in year. month is numbered from 1 to 12. year defaults to a non-leap year if omitted.

Examples:

  > (days-in-month 2)

  28

  > (days-in-month 2 2000)

  29

(date-valid? date)  boolean?
  date : srfi:date?
Returns #t if date is a valid date (its hour, day, month and so on are all in the correct ranges).

(date-day-of-the-week date)
  (U 'mon 'tue 'wed 'thu 'fri 'sat 'sun)
  date : date?
Returns a symbol representing the day of the week on date.

(date-week-day? date)  boolean?
  date : date?
Returns #t if date is a Monday, Tuesday, Wednesday or Friday.

(time->ago-string then    
  [now    
  #:format format-string]    
  #:short? boolean?)  string?
  then : (U time-tai time-utc)
  now : (U time-tai time-utc) = (current-time (time-type then))
  format-string : string? = "~a ~a ago"
  boolean? : #f
Given the time of an event the past (and, optionally, another argument representing the current time), returns a textual description of the time passed since the event. Raises exn:fail:unlib if now is before then. See seconds->ago-string for examples.

The optional format-string argument should have two wildcards in it: one for the number (1, 2, 59) and one for the unit (seconds, minutes, days). The format string is ignored if the result is "yesterday".
The optional short? argument can be set to #t to use abbreviated time units (such as "min" rather than "minute").

(seconds->ago-string secs    
  [now    
  #:format format-string]    
  #:short? boolean?)  string?
  secs : integer?
  now : integer? = (current-seconds)
  format-string : string? = "~a ~a ago"
  boolean? : #f
Like time->ago-string but then and now are integer values like those output by current-seconds.

Examples:

  > (seconds->ago-string (- (current-seconds) 45))

  "45 seconds ago"

  > (seconds->ago-string (- (current-seconds) (* 30 60)))

  "30 minutes ago"

  > (seconds->ago-string (- (current-seconds) (* 20 60 60)))

  "20 hours ago"

  > (seconds->ago-string (- (current-seconds) (* 25 60 60)))

  "yesterday"

  > (seconds->ago-string (- (current-seconds) (* 50 60 60)))

  "2 days ago"

(current-year)  integer?
Returns the current four digit year.

(current-time-zone-offset)  integer?
Returns the current local time-zone offset in seconds (taking into account DST when and where appropriate).

(time-tai->string time format-string)  string?
  time : time-tai
  format-string : string?
Formats time (a time-tai struct) according to the specified srfi/19 format-string.

(time-utc->string time format-string)  string?
  time : time-utc
  format-string : string?
Formats time (a time-utc struct) according to the specified srfi/19 format-string.