Date structure

May 20, 2026 ยท View on GitHub

Up to index

The Date structure provides an abstract type for calendar dates and times, with fields for year, month, day, hour, minute, and second. Dates can be constructed from Time.time values, decomposed into fields, formatted as strings, and compared.

Specified by the Standard ML Basis Library.

Synopsis

type date
datatype month
  = Jan
  | Feb
  | Mar
  | Apr
  | May
  | Jun
  | Jul
  | Aug
  | Sep
  | Oct
  | Nov
  | Dec
datatype weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun

exception Date

val compare : date * date -> order
val date : {day: int, hour: int, minute: int, month: month, offset: time option, second: int, year: int} -> date
val day : date -> int
val fmt : string -> date -> string
val fromString : string -> date option
val fromTimeLocal : time -> date
val fromTimeUniv : time -> date
val hour : date -> int
val isDst : date -> bool option
val localOffset : unit -> time
val minute : date -> int
val month : date -> month
val second : date -> int
val toString : date -> string
val toTime : date -> time
val weekDay : date -> weekday
val year : date -> int
val yearDay : date -> int

type date

is an equality type representing a calendar date and time of day, with an associated timezone offset.

datatype month

is the type of month values.

datatype weekday

is the type of weekday values.

exception Date

is raised when a date cannot be constructed from the given fields (for example, if the day or month is out of range).

compare

compare (d1, d2) (or d1.compare d2) returns LESS, EQUAL, or GREATER depending on whether d1 is less than, equal to, or greater than d2 (comparing instants in time).

date

date {year, month, day, hour, minute, second, offset} constructs a date from the given fields. If offset is NONE, the date is in local time; if SOME t, the date is in the timezone with offset t from UTC.

day

day d (or d.day ()) returns the day of the month of d, in the range [1, 31].

fmt

fmt s d formats d using the strftime-style format string s. Recognized format codes include %Y (4-digit year), %m (2-digit month), %d (2-digit day), %H (hour), %M (minute), %S (second), %a (abbreviated weekday), %b (abbreviated month), and %% (literal %).

fromString

fromString s parses a date from the string s, which should be in the format produced by toString (e.g., "Thu Jan 1 00:00:00 1970"). Returns SOME d if successful, NONE otherwise.

fromTimeLocal

fromTimeLocal t converts the time value t to a date in the local timezone.

fromTimeUniv

fromTimeUniv t converts the time value t to a date in UTC.

hour

hour d (or d.hour ()) returns the hour of d, in the range [0, 23].

isDst

isDst d (or d.isDst ()) returns SOME true if d is in daylight saving time, SOME false if not, or NONE if the information is not available.

localOffset

localOffset () returns the offset of the local timezone from UTC as a time value (nanoseconds).

minute

minute d (or d.minute ()) returns the minute of d, in the range [0, 59].

month

month d (or d.month ()) returns the month of d.

second

second d (or d.second ()) returns the second of d, in the range [0, 59].

toString

toString d (or d.toString ()) formats d as a string in the format "Www Mmm DD HH:MM:SS YYYY", for example "Thu Jan 1 00:00:00 1970".

toTime

toTime d (or d.toTime ()) converts d to a time value (nanoseconds since the Unix epoch).

weekDay

weekDay d (or d.weekDay ()) returns the day of the week of d.

year

year d (or d.year ()) returns the year of d.

yearDay

yearDay d (or d.yearDay ()) returns the day of the year of d, in the range [0, 365].