On this page:
msec->date
time->formatted-date-string
time->formatted-time-string
time->formatted-datetime-string
timestamp->time-string
timestamp->date-string
YMD-string->timestamp
min-of-timestamps
date-range-expand
time-unit
time-units
abbr->time-unit
number->msec
msec->time-unit
duration->unit-string
8.0

9 Time Utilities

 (require "../time-utilities.rkt") package: base

procedure

(msec->date msec)  date?

  msec : nonnegative-integer?

procedure

(time->formatted-date-string msec    
  [pattern    
  offset])  string?
  msec : nonnegative-integer?
  pattern : string? = "~Y-~m-~d"
  offset : nonnegative-integer? = 0
uses srfi19 pattern to format given time (in ms) as a date string, with default of "YYYY-MM-DD." Can include optional offset in ms, applied before formatting.

procedure

(time->formatted-time-string msec    
  [pattern    
  offset])  string?
  msec : nonnegative-integer?
  pattern : string? = "~H:~M"
  offset : nonnegative-integer? = 0
uses srfi19 pattern to format given time (in ms) as time string, with default of "HH:MM." Can include optional offset in ms, applied before formatting.

procedure

(time->formatted-datetime-string msec    
  [pattern])  string?
  msec : nonnegative-integer?
  pattern : string? = "~Y-~m-~d ~H:~M"
uses srfi19 pattern to format given time (in ms) as date and time string, with default of "YYYY-MM-DD HH:MM." Can include optional offset in ms, applied before formatting.

procedure

(timestamp->time-string timestamp    
  [#:format time-format    
  #:template template])  string?
  timestamp : nonnegative-integer?
  time-format : symbol? = 'hour-min
  template : (or/c null? string?) = null
converts given timestamp in milliseconds to a string representation of time, but modulo 24 hours (so clock time).

If template string is omitted (or null), then the time-format argument is used for formatting. ’hour-min ("HH:MM") is the default format if neither template nor time-format are specified.

time-format argument can be
  • 'hour-min for HH:MM format

  • 'decimal for hour as a decimal, with either 0 precision if an integer hour (eg "13") or 2 precision if between hours (eg. "13.25" for 13:15

If template string is provided then string is formatted using srfi/19 routine for time-utc.

Rounded to the nearest minute.

procedure

(timestamp->date-string timestamp    
  [#:template template])  string?
  timestamp : nonnegative-integer?
  template : string? = "~m/~d"
converts given timestamp in milliseconds to a string representation of time, using the given template string as per srfi/19. If no template string is provided, defaults to "MM/DD" format. Rounded down to the nearest second.

procedure

(YMD-string->timestamp date-string)  nonnegative-integer?

  date-string : string?
parses given date-string in format "YYYY-MM-DD" to a utc timestamp in milliseconds as per srfi/19 using the template "~Y-~m-~d".

procedure

(min-of-timestamps timestamps)  nonnegative-integer?

  timestamps : list?
parses given the list of timestamps in milliseconds, returns the smallest (earliest) timestamp in the list.

procedure

(date-range-expand s)  list-of-strings?

  s : string?

Returns a list of date strings mm/dd/yyyy by expanding comma-separated date ranges in given string s. Date ranges can be in 3 formats:
  • "M/D/Y" (a single day)

  • "M/D1-D2/Y" (a range of days within a single month)

  • "M1/D1/Y1-M2/D2/Y2" (range of days beginning and ending in different months)

All 3 formats can be mixed in a single string for expansion. Spaces are removed from s before expansion

Examples
(date-range-expand "5/6/2018, 7/30/2018-8/2/2018, 9/2-5/2018") ->
'("2018-05-06" "2018-07-30" "2018-07-31" "2018-08-01" "2018-08-02" "2018-09-02" "2018-09-03" "2018-09-04" "2018-09-05")
 
(date-range-expand "2/27/2018-3/2/2018, 2/27/2020-3/2/2020") ->
'("2018-02-27" "2018-02-28" "2018-03-01" "2018-03-02" "2020-02-27" "2020-02-28" "2020-02-29" "2020-03-01" "2020-03-02")

struct

(struct time-unit (name abbr size limit))

  name : string?
  abbr : string?
  size : nonnegative-integer?
  limit : nonnegative-integer?
defines a time duration unit with a name, an abbreviation, and length in ms. Not that units of "month", "year", and above are based on average lengths.

value

time-units : list?

Default list of time duration units.

Unit

 

Abbr

 

"Durations (ms)"

millsecond

 

ms

 

1

second

 

s

 

1000

minute

 

min

 

60000

hour

 

h

 

3600000

day

 

d

 

86400000

week

 

wk

 

604800000

month

 

mo

 

2592000000

year

 

y

 

31557600000

decade

 

Dy

 

315576000000

century

 

Cy

 

3155760000000

millennium

 

Ky

 

3155760000000000

procedure

(abbr->time-unit abbr)  struct?

  abbr : (or/c symbol? string?)
given abbr, returns a time-unit structure. abbr can be either a string (eg "min") or a symbol (’min) .

procedure

(number->msec num unit-abbr)  nonnegative-integer?

  num : number?
  unit-abbr : (or/c symbol? string?)
given a number representing units of time in the given unit-abbr, returns milliseconds

procedure

(msec->time-unit x)  struct?

  x : nonnegative-integer?
finds the time-unit which best matches the given x milliseconds. The matching time-unit is found based on its size and upper limit. Examples:
(msec->time-unit 120000) -> (time-unit "minute"...)
 
  (msec->time-unit 3900000) -> (time-unit "hour"...)
 

procedure

(duration->unit-string x    
  [#:precision precision    
  #:unit-in-msec scale    
  #:unit user-abbr])  string?
  x : number?
  precision : nonnegative-integer? = 2
  scale : number? = 1
  user-abbr : (or/c #f symbol? string?) = #f
returns a string representation of the given duration. The duration is assumed to be in milliseconds, but will be scaled by #:unit-in-msec scale if provided. THe duration string will be given in units determined either by best match using msec->time-unit, or by using the user-provided #: unit abbreviation (whcih can be a sting or a symbol). The precision of the number defaults to 2, but can be set with #:precision argument. Examples:
 
(duration->unit-string 300000) -> "5.00 min"
 
(duration->unit-string 4500000) -> "1.25 h"
 
(duration->unit-string 4500000 #:unit 'min) -> "75 min"
 
(duration->unit-string 4800000 #:precision:4) -> "1.3333 h"