diff options
author | Adam Chlipala <adam@chlipala.net> | 2014-02-18 07:07:01 -0500 |
---|---|---|
committer | Adam Chlipala <adam@chlipala.net> | 2014-02-18 07:07:01 -0500 |
commit | f9989b616d8cf4fd1bfd968839bfdbe869468f7a (patch) | |
tree | a7d9f1a32d92963d45c8ab93f0fec2396d973bd5 /lib/ur | |
parent | c410ff27bef1eb39bc3a28f2f1d151ad2cb75b98 (diff) | |
parent | 63fb5cd97f53d36a6ffeec207688127e488447d7 (diff) |
Merge with small clean-ups
Diffstat (limited to 'lib/ur')
-rw-r--r-- | lib/ur/basis.urs | 10 | ||||
-rw-r--r-- | lib/ur/datetime.ur | 135 | ||||
-rw-r--r-- | lib/ur/datetime.urs | 38 |
3 files changed, 183 insertions, 0 deletions
diff --git a/lib/ur/basis.urs b/lib/ur/basis.urs index eefb5d2c..c94f2ba6 100644 --- a/lib/ur/basis.urs +++ b/lib/ur/basis.urs @@ -167,6 +167,16 @@ val diffInMilliseconds : time -> time -> int val timef : string -> time -> string (* Uses strftime() format string *) val readUtc : string -> option time +(* Takes a year, month, day, hour, minute, second. *) +val fromDatetime : int -> int -> int -> int -> int -> int -> time +val datetimeYear : time -> int +val datetimeMonth : time -> int +val datetimeDay : time -> int +val datetimeHour : time -> int +val datetimeMinute: time -> int +val datetimeSecond : time -> int +val datetimeDayOfWeek : time -> int + (** * Encryption *) diff --git a/lib/ur/datetime.ur b/lib/ur/datetime.ur new file mode 100644 index 00000000..9aeab291 --- /dev/null +++ b/lib/ur/datetime.ur @@ -0,0 +1,135 @@ +datatype day_of_week = Sunday | Monday | Tuesday | Wednesday | Thursday | + Friday | Saturday + +val show_day_of_week = mkShow (fn dow => case dow of + Sunday => "Sunday" + | Monday => "Monday" + | Tuesday => "Tuesday" + | Wednesday => "Wednesday" + | Thursday => "Thursday" + | Friday => "Friday" + | Saturday => "Saturday") + +fun dayOfWeekToInt dow = case dow of + Sunday => 0 + | Monday => 1 + | Tuesday => 2 + | Wednesday => 3 + | Thursday => 4 + | Friday => 5 + | Saturday => 6 + +fun intToDayOfWeek i = case i of + 0 => Sunday + | 1 => Monday + | 2 => Tuesday + | 3 => Wednesday + | 4 => Thursday + | 5 => Friday + | 6 => Saturday + | n => error <xml>Invalid day of week {[n]}</xml> + +val eq_day_of_week = mkEq (fn a b => dayOfWeekToInt a = dayOfWeekToInt b) + + +datatype month = January | February | March | April | May | June | July | + August | September | October | November | December + +val show_month = mkShow (fn m => case m of + January => "January" + | February => "February" + | March => "March" + | April => "April" + | May => "May" + | June => "June" + | July => "July" + | August => "August" + | September => "September" + | October => "October" + | November => "November" + | December => "December") + +type t = { + Year : int, + Month : month, + Day : int, + Hour : int, + Minute : int, + Second : int +} + +fun monthToInt m = case m of + January => 0 + | February => 1 + | March => 2 + | April => 3 + | May => 4 + | June => 5 + | July => 6 + | August => 7 + | September => 8 + | October => 9 + | November => 10 + | December => 11 + +fun intToMonth i = case i of + 0 => January + | 1 => February + | 2 => March + | 3 => April + | 4 => May + | 5 => June + | 6 => July + | 7 => August + | 8 => September + | 9 => October + | 10 => November + | 11 => December + | n => error <xml>Invalid month number {[n]}</xml> + +val eq_month = mkEq (fn a b => monthToInt a = monthToInt b) + + +fun toTime dt : time = fromDatetime dt.Year (monthToInt dt.Month) dt.Day + dt.Hour dt.Minute dt.Second + +fun fromTime t : t = { + Year = datetimeYear t, + Month = intToMonth (datetimeMonth t), + Day = datetimeDay t, + Hour = datetimeHour t, + Minute = datetimeMinute t, + Second = datetimeSecond t +} + +val ord_datetime = mkOrd { Lt = fn a b => toTime a < toTime b, + Le = fn a b => toTime a <= toTime b } + +fun format fmt dt : string = timef fmt (toTime dt) + +fun dayOfWeek dt : day_of_week = intToDayOfWeek (datetimeDayOfWeek (toTime dt)) + +val now : transaction t = + n <- now; + return (fromTime n) + +(* Normalize a datetime. This will convert, e.g., January 32nd into February + 1st. *) + +fun normalize dt = fromTime (toTime dt) +fun addToField [nm :: Name] [rest ::: {Type}] [[nm] ~ rest] + (delta : int) (r : $([nm = int] ++ rest)) + : $([nm = int] ++ rest) = + (r -- nm) ++ {nm = r.nm + delta} + + +(* Functions for adding to a datetime. There is no addMonths or addYears since + it's not clear what should be done; what's 1 month after January 31, or 1 + year after February 29th? + + These can't all be defined in terms of addSeconds because of leap seconds. *) + +fun addSeconds n dt = normalize (addToField [#Second] n dt) +fun addMinutes n dt = normalize (addToField [#Minute] n dt) +fun addHours n dt = normalize (addToField [#Hour] n dt) +fun addDays n dt = normalize (addToField [#Day] n dt) diff --git a/lib/ur/datetime.urs b/lib/ur/datetime.urs new file mode 100644 index 00000000..972f86bf --- /dev/null +++ b/lib/ur/datetime.urs @@ -0,0 +1,38 @@ +datatype day_of_week = Sunday | Monday | Tuesday | Wednesday | Thursday | + Friday | Saturday + +datatype month = January | February | March | April | May | June | July | + August | September | October | November | December + + +type t = { + Year : int, + Month : month, + Day : int, + Hour : int, + Minute : int, + Second : int +} + +val ord_datetime : ord t + +val show_day_of_week : show day_of_week +val show_month : show month +val eq_day_of_week : eq day_of_week +val eq_month : eq month +val dayOfWeekToInt : day_of_week -> int +val intToDayOfWeek : int -> day_of_week +val monthToInt : month -> int +val intToMonth : int -> month + +val toTime : t -> time +val fromTime : time -> t +val format : string -> t -> string +val dayOfWeek : t -> day_of_week +val now : transaction t +val normalize : t -> t + +val addSeconds : int -> t -> t +val addMinutes : int -> t -> t +val addHours : int -> t -> t +val addDays : int -> t -> t |