/*----------------------------------------------------------------------------
   Copyright 2012-2021, Microsoft Research, Daan Leijen

   Licensed under the Apache License, Version 2.0 ("The Licence"). You may not
   use this file except in compliance with the License. A copy of the License
   can be found in the LICENSE file at the root of this distribution.
----------------------------------------------------------------------------*/

/* Date, clock, and week days support.

   Defines basic types for human readable time. Years, months, days etc. are always 1-based.
   Week dates are absent as they are treated as normal dates, using the `std/time/calendars/cal-iso-week` calendar.
*/
module std/time/datestd/time/date

import std/num/float64std/num/float64
import std/num/ddoublestd/num/ddouble

/*----------------------------------------------------------------------------

----------------------------------------------------------------------------*/

// A date consists of a the year, month, and day.
pub value struct datestd/time/date/date: V
  yearstd/time/date/date/year: (date : date) -> int : intstd/core/types/int: V
  monthstd/time/date/date/month: (date : date) -> int: intstd/core/types/int: V
  daystd/time/date/date/day: (date : date) -> int  : intstd/core/types/int: V


// Create an ISO weekdate where the "month" is the ISO week number.
pub fun weekdatestd/time/date/weekdate: (year : int, month : int, weekday : weekday) -> date( yearyear: int : intstd/core/types/int: V, monthmonth: int: intstd/core/types/int: V, weekdayweekday: weekday : weekdaystd/time/date/weekday: V )result: -> total date : datestd/time/date/date: V
  Datestd/time/date/Date: (year : int, month : int, day : int) -> date(yearyear: int,monthmonth: int,weekdayweekday: weekday.intstd/time/date/int: (wd : weekday) -> int)

// Show a year in ISO format (using 5+ digits and explicit sign for years < 0 or years > 9999)).
pub fun show-yearstd/time/date/show-year: (year : int) -> string( yearyear: int : intstd/core/types/int: V )result: -> total string : stringstd/core/types/string: V
  val (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)ysignysign: string,ywidthywidth: int)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) = if yearyear: int >std/core/int/(>): (x : int, y : int) -> bool 9999literal: int
dec = 9999
hex16= 0x270F
bit16= 0b0010011100001111
then (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)"+"literal: string
count= 1
,5literal: int
dec = 5
hex8 = 0x05
bit8 = 0b00000101
)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) elif yearyear: int.is-negstd/core/int/is-neg: (i : int) -> bool then (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)"-"literal: string
count= 1
,5literal: int
dec = 5
hex8 = 0x05
bit8 = 0b00000101
)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) else (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)""literal: string
count= 0
,4literal: int
dec = 4
hex8 = 0x04
bit8 = 0b00000100
)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) ysignysign: string ++std/core/types/(++): (x : string, y : string) -> string yearyear: int.absstd/core/int/abs: (i : int) -> int.show0std/time/date/show0: (i : int, width : ? int) -> string(ywidthywidth: int
) // Show a date in ISO format. `Date(2000,1,1).show == "2000-01-01"`. // Takes an optional `month-prefix` (=`""`) that is used by the ISO week // and month calendar to add a `"W"` or `"M"` prefix respectively. pub fun showstd/time/date/show: (d : date, month-prefix : ? string) -> string( dd: date : datestd/time/date/date: V, month-prefixmonth-prefix: ? string : stringstd/core/types/string: V = ""literal: string
count= 0
)result: -> total string : stringstd/core/types/string: V val day-widthday-width: int = if month-prefixmonth-prefix: string==std/core/string/(==): (string, string) -> bool"W"literal: string
count= 1
then 1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
else 2literal: int
dec = 2
hex8 = 0x02
bit8 = 0b00000010
dd: date.yearstd/time/date/date/year: (date : date) -> int.show-yearstd/time/date/show-year: (year : int) -> string ++std/core/types/(++): (x : string, y : string) -> string "-"literal: string
count= 1
++std/core/types/(++): (x : string, y : string) -> string month-prefixmonth-prefix: string ++std/core/types/(++): (x : string, y : string) -> string dd: date.monthstd/time/date/date/month: (date : date) -> int.show0std/time/date/show0: (i : int, width : ? int) -> string ++std/core/types/(++): (x : string, y : string) -> string "-"literal: string
count= 1
++std/core/types/(++): (x : string, y : string) -> string dd: date.daystd/time/date/date/day: (date : date) -> int.show0std/time/date/show0: (i : int, width : ? int) -> string(day-widthday-width: int
) // Compare two dates by fields. pub fun cmpstd/time/date/cmp: (d : date, e : date) -> order( dd: date : datestd/time/date/date: V, ee: date : datestd/time/date/date: V )result: -> total order : orderstd/core/types/order: V match(cmpstd/core/int/cmp: (x : int, y : int) -> order(dd: date.yearstd/time/date/date/year: (date : date) -> int,ee: date.yearstd/time/date/date/year: (date : date) -> int)) Eqstd/core/types/Eq: order -> match(cmpstd/core/int/cmp: (x : int, y : int) -> order(dd: date.monthstd/time/date/date/month: (date : date) -> int,ee: date.monthstd/time/date/date/month: (date : date) -> int)) Eqstd/core/types/Eq: order -> cmpstd/core/int/cmp: (x : int, y : int) -> order(dd: date.daystd/time/date/date/day: (date : date) -> int,ee: date.daystd/time/date/date/day: (date : date) -> int) ordord: order -> ordord: order ordord: order -> ordord: order pub fun (<)std/time/date/(<): (i : date, j : date) -> bool( ii: date : datestd/time/date/date: V, jj: date : datestd/time/date/date: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/cmp: (d : date, e : date) -> order(ii: date,jj: date) ==std/core/order/(==): (x : order, y : order) -> bool Ltstd/core/types/Lt: order } pub fun (<=)std/time/date/(<=): (i : date, j : date) -> bool( ii: date : datestd/time/date/date: V, jj: date : datestd/time/date/date: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/cmp: (d : date, e : date) -> order(ii: date,jj: date) !=std/core/order/(!=): (x : order, y : order) -> bool Gtstd/core/types/Gt: order } pub fun (>)std/time/date/(>): (i : date, j : date) -> bool( ii: date : datestd/time/date/date: V, jj: date : datestd/time/date/date: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/cmp: (d : date, e : date) -> order(ii: date,jj: date) ==std/core/order/(==): (x : order, y : order) -> bool Gtstd/core/types/Gt: order } pub fun (>=)std/time/date/(>=): (i : date, j : date) -> bool( ii: date : datestd/time/date/date: V, jj: date : datestd/time/date/date: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/cmp: (d : date, e : date) -> order(ii: date,jj: date) !=std/core/order/(!=): (x : order, y : order) -> bool Ltstd/core/types/Lt: order } pub fun (==)std/time/date/(==): (i : date, j : date) -> bool( ii: date : datestd/time/date/date: V, jj: date : datestd/time/date/date: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/cmp: (d : date, e : date) -> order(ii: date,jj: date) ==std/core/order/(==): (x : order, y : order) -> bool Eqstd/core/types/Eq: order } pub fun (!=)std/time/date/(!=): (i : date, j : date) -> bool( ii: date : datestd/time/date/date: V, jj: date : datestd/time/date/date: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/cmp: (d : date, e : date) -> order(ii: date,jj: date) !=std/core/order/(!=): (x : order, y : order) -> bool Eqstd/core/types/Eq: order } // Add two dates field-wise together. pub fun (+)std/time/date/(+): (d1 : date, d2 : date) -> date( d1d1: date : datestd/time/date/date: V, d2d2: date : datestd/time/date/date: V )result: -> total date : datestd/time/date/date: V Datestd/time/date/Date: (year : int, month : int, day : int) -> date(d1d1: date.yearstd/time/date/date/year: (date : date) -> int +std/core/int/(+): (x : int, y : int) -> int d2d2: date.yearstd/time/date/date/year: (date : date) -> int, d1d1: date.monthstd/time/date/date/month: (date : date) -> int +std/core/int/(+): (x : int, y : int) -> int d2d2: date.monthstd/time/date/date/month: (date : date) -> int, d1d1: date.daystd/time/date/date/day: (date : date) -> int +std/core/int/(+): (x : int, y : int) -> int d2d2: date.daystd/time/date/date/day: (date : date) -> int ) /*---------------------------------------------------------------------------- ----------------------------------------------------------------------------*/ // A clock consists of the hour, minute, second, and fractional second (between ``0.0` and `1.0`). pub struct clockstd/time/date/clock: V hoursstd/time/date/clock/hours: (clock : clock) -> int : intstd/core/types/int: V minutesstd/time/date/clock/minutes: (clock : clock) -> int : intstd/core/types/int: V secondsstd/time/date/clock/seconds: (clock : clock) -> ddouble : ddoublestd/num/ddouble/ddouble: V // Create a clock from a `:duration`; normalizes the clock with seconds and minutes under 60. //pub fun clock( d : duration ) : clock // clock(d.seconds) //} // Create a clock from seconds; normalizes the clock with seconds and minutes under 60. pub fun ddouble/clockstd/time/date/ddouble/clock: (seconds : ddouble) -> clock( secondsseconds: ddouble : ddoublestd/num/ddouble/ddouble: V )result: -> total clock : clockstd/time/date/clock: V clockstd/time/date/intddouble/clock: (seconds : int, frac : ? ddouble) -> clock(secondsseconds: ddouble.floorstd/num/ddouble/floor: (x : ddouble) -> ddouble.intstd/num/ddouble/int: (x : ddouble, nonfin : ? int) -> int,secondsseconds: ddouble.ffractionstd/num/ddouble/ffraction: (x : ddouble) -> ddouble) // Create a clock from a seconds as an `:int` with an optional fraction. // Normalizes the clock with seconds and minutes under 60 but // adds the fraction as is to the final seconds, so that might // be `>= 60` if the fraction `>= 1.0`; pub fun intfloat64/clockstd/time/date/intfloat64/clock: (seconds : int, frac : float64) -> clock( secondsseconds: int : intstd/core/types/int: V, fracfrac: float64 : float64std/core/types/float64: V )result: -> total clock : clockstd/time/date/clock: V clockstd/time/date/intddouble/clock: (seconds : int, frac : ? ddouble) -> clock(secondsseconds: int,fracfrac: float64.ddoublestd/num/ddouble/float64/ddouble: (d : float64) -> ddouble) // Create a clock from a seconds as an `:int` with an optional fraction. // Normalizes the clock with seconds and minutes under 60 but // adds the fraction as is to the final seconds, so that might // be `>= 60` if the fraction `>= 1.0`; pub fun intddouble/clockstd/time/date/intddouble/clock: (seconds : int, frac : ? ddouble) -> clock( secondsseconds: int : intstd/core/types/int: V, fracfrac: ? ddouble : ddoublestd/num/ddouble/ddouble: V = zerostd/num/ddouble/zero: ddouble)result: -> total clock : clockstd/time/date/clock: V val (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)hmhm: int,ss: int)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) = divmodstd/core/int/divmod: (x : int, y : int) -> (int, int)(secondsseconds: int,60literal: int
dec = 60
hex8 = 0x3C
bit8 = 0b00111100
) val (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)hh: int,mm: int)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) = divmodstd/core/int/divmod: (x : int, y : int) -> (int, int)(hmhm: int,60literal: int
dec = 60
hex8 = 0x3C
bit8 = 0b00111100
) Clockstd/time/date/Clock: (hours : int, minutes : int, seconds : ddouble) -> clock(hh: int,mm: int,ss: int.ddoublestd/num/ddouble/int/ddouble: (i : int) -> ddouble +std/num/ddouble/(+): (x : ddouble, y : ddouble) -> ddouble fracfrac: ddouble
) pub fun leap/clockstd/time/date/leap/clock: (seconds : ddouble, leap : int) -> clock( secondsseconds: ddouble : ddoublestd/num/ddouble/ddouble: V, leapleap: int : intstd/core/types/int: V)result: -> total clock : clockstd/time/date/clock: V clockstd/time/date/intddouble/clock: (seconds : int, frac : ? ddouble) -> clock(secondsseconds: ddouble.floorstd/num/ddouble/floor: (x : ddouble) -> ddouble.intstd/num/ddouble/int: (x : ddouble, nonfin : ? int) -> int, secondsseconds: ddouble.ffractionstd/num/ddouble/ffraction: (x : ddouble) -> ddouble +std/num/ddouble/(+): (x : ddouble, y : ddouble) -> ddouble leapleap: int.ddoublestd/num/ddouble/int/ddouble: (i : int) -> ddouble) // The zero clock pub val clock0std/time/date/clock0: clock = Clockstd/time/date/Clock: (hours : int, minutes : int, seconds : ddouble) -> clock(0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
,0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
,zerostd/num/ddouble/zero: ddouble
) // Is this a zero clock? pub fun is-zerostd/time/date/is-zero: (c : clock) -> bool( cc: clock : clockstd/time/date/clock: V )result: -> total bool : boolstd/core/types/bool: V (cc: clock.hoursstd/time/date/clock/hours: (clock : clock) -> int.is-zerostd/core/int/is-zero: (x : int) -> bool &&std/core/types/(&&): (x : bool, y : bool) -> bool cc: clock.minutesstd/time/date/clock/minutes: (clock : clock) -> int.is-zerostd/core/int/is-zero: (x : int) -> bool &&std/core/types/(&&): (x : bool, y : bool) -> bool cc: clock.secondsstd/time/date/clock/seconds: (clock : clock) -> ddouble.is-zerostd/num/ddouble/is-zero: (x : ddouble) -> bool) // Return the total seconds of a `:clock` assuming 60 seconds per // minute and 60 minutes per hour. pub fun total-secondsstd/time/date/total-seconds: (c : clock) -> ddouble( cc: clock : clockstd/time/date/clock: V )result: -> total ddouble : ddoublestd/num/ddouble/ddouble: V ddoublestd/num/ddouble/int/ddouble: (i : int) -> ddouble((cc: clock.hoursstd/time/date/clock/hours: (clock : clock) -> int*std/core/int/(*): (int, int) -> int60literal: int
dec = 60
hex8 = 0x3C
bit8 = 0b00111100
+std/core/int/(+): (x : int, y : int) -> int cc: clock.minutesstd/time/date/clock/minutes: (clock : clock) -> int)*std/core/int/(*): (int, int) -> int60literal: int
dec = 60
hex8 = 0x3C
bit8 = 0b00111100
) +std/num/ddouble/(+): (x : ddouble, y : ddouble) -> ddouble cc: clock.secondsstd/time/date/clock/seconds: (clock : clock) -> ddouble
// Return the whole seconds part of a `:clock`. pub fun whole-secondsstd/time/date/whole-seconds: (c : clock) -> int( cc: clock : clockstd/time/date/clock: V )result: -> total int : intstd/core/types/int: V cc: clock.secondsstd/time/date/clock/seconds: (clock : clock) -> ddouble.truncatestd/num/ddouble/truncate: (x : ddouble) -> ddouble.intstd/num/ddouble/int: (x : ddouble, nonfin : ? int) -> int // Return the fraction of the seconds as milli-seconds (10^-3^). pub fun milli-secondsstd/time/date/milli-seconds: (c : clock) -> int( cc: clock : clockstd/time/date/clock: V )result: -> total int : intstd/core/types/int: V (cc: clock.secondsstd/time/date/clock/seconds: (clock : clock) -> ddouble.fractionstd/num/ddouble/fraction: (x : ddouble) -> ddouble.float64std/num/ddouble/float64: (x : ddouble) -> float64 *std/num/float64/(*): (x : float64, y : float64) -> float64 1.0e3literal: float64
hex64= 0x1.f4p9
).truncatestd/num/float64/truncate: (d : float64) -> float64.intstd/num/float64/int: (f : float64) -> int
// Return the fraction of seconds as nano-seconds ((10^-9^). pub fun nano-secondsstd/time/date/nano-seconds: (c : clock) -> int( cc: clock : clockstd/time/date/clock: V )result: -> total int : intstd/core/types/int: V (cc: clock.secondsstd/time/date/clock/seconds: (clock : clock) -> ddouble.fractionstd/num/ddouble/fraction: (x : ddouble) -> ddouble.float64std/num/ddouble/float64: (x : ddouble) -> float64 *std/num/float64/(*): (x : float64, y : float64) -> float64 1.0e9literal: float64
hex64= 0x1.dcd65p29
).truncatestd/num/float64/truncate: (d : float64) -> float64.intstd/num/float64/int: (f : float64) -> int
// Round a clock time to a certain number of digits precision (of the fraction of seconds) (default `9`, nano seconds). pub fun round-to-precstd/time/date/round-to-prec: (c : clock, prec : ? int) -> clock( cc: clock : clockstd/time/date/clock: V, precprec: ? int : intstd/core/types/int: V = 9literal: int
dec = 9
hex8 = 0x09
bit8 = 0b00001001
)result: -> total clock : clockstd/time/date/clock: V Clockstd/time/date/Clock: (hours : int, minutes : int, seconds : ddouble) -> clock(cc: clock.hoursstd/time/date/clock/hours: (clock : clock) -> int, cc: clock.minutesstd/time/date/clock/minutes: (clock : clock) -> int, cc: clock.secondsstd/time/date/clock/seconds: (clock : clock) -> ddouble.round-to-precstd/num/ddouble/round-to-prec: (x : ddouble, prec : int) -> ddouble(precprec: int)
) // Show a clock in ISO format up to an optional maximum precision (=`9`).\ // `Clock(23,30,fixed(1.123)).show == "23:30:01.123"`\ // `Clock(23,30,fixed(1.123)).show(0) == "23:30:01"` pub fun clock/showstd/time/date/clock/show: (c : clock, prec : ? int) -> string( cc: clock : clockstd/time/date/clock: V, precprec: ? int : intstd/core/types/int: V = 9literal: int
dec = 9
hex8 = 0x09
bit8 = 0b00001001
)result: -> total string : stringstd/core/types/string: V cc: clock.hoursstd/time/date/clock/hours: (clock : clock) -> int.show0std/time/date/show0: (i : int, width : ? int) -> string ++std/core/types/(++): (x : string, y : string) -> string ":"literal: string
count= 1
++std/core/types/(++): (x : string, y : string) -> string cc: clock.minutesstd/time/date/clock/minutes: (clock : clock) -> int.show0std/time/date/show0: (i : int, width : ? int) -> string ++std/core/types/(++): (x : string, y : string) -> string ":"literal: string
count= 1
++std/core/types/(++): (x : string, y : string) -> string cc: clock.secondsstd/time/date/clock/seconds: (clock : clock) -> ddouble.show-secondsstd/time/date/show-seconds: (secs : ddouble, max-prec : ? int, secs-width : ? int, unit : ? string) -> string(precprec: int,2literal: int
dec = 2
hex8 = 0x02
bit8 = 0b00000010
) // Show seconds pub fun show-secondsstd/time/date/show-seconds: (secs : ddouble, max-prec : ? int, secs-width : ? int, unit : ? string) -> string( secssecs: ddouble : ddoublestd/num/ddouble/ddouble: V, max-precmax-prec: ? int : intstd/core/types/int: V = 9literal: int
dec = 9
hex8 = 0x09
bit8 = 0b00001001
, secs-widthsecs-width: ? int : intstd/core/types/int: V = 1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
, unitunit: ? string : stringstd/core/types/string: V = ""literal: string
count= 0
)result: -> total string : stringstd/core/types/string: V val ss: string = secssecs: ddouble.show-fixedstd/num/ddouble/show-fixed: (x : ddouble, prec : ? int) -> string(~std/core/int/(~): (i : int) -> intmax-precmax-prec: int.absstd/core/int/abs: (i : int) -> int) match(ss: string.findstd/core/sslice/find: (s : string, sub : string) -> maybe<sslice>("."literal: string
count= 1
)) Nothingstd/core/types/Nothing: forall<a> maybe<a> -> ss: string.pad-leftstd/core/string/pad-left: (s : string, width : int, fill : ? char) -> string(secs-widthsecs-width: int,'0'literal: char
unicode= u0030
) Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(dotdot: sslice) -> val ff: string = dotdot: sslice.afterstd/core/sslice/after: (slice : sslice) -> sslice.stringstd/core/sslice/string: (slice : sslice) -> string val len3len3: int = ((ff: string.countstd/core/string/count: (s : string) -> int +std/core/int/(+): (x : int, y : int) -> int 2literal: int
dec = 2
hex8 = 0x02
bit8 = 0b00000010
)/std/core/int/(/): (x : int, y : int) -> int3literal: int
dec = 3
hex8 = 0x03
bit8 = 0b00000011
)*std/core/int/(*): (int, int) -> int3literal: int
dec = 3
hex8 = 0x03
bit8 = 0b00000011
//trace("ts-show: " ++ s ++ ", " ++ len3.show ++ ", prec: " ++ max-prec.show ++ ", " ++ ts.since.show(-9)) dotdot: sslice.beforestd/core/sslice/before: (slice : sslice) -> sslice.stringstd/core/sslice/string: (slice : sslice) -> string.pad-leftstd/core/string/pad-left: (s : string, width : int, fill : ? char) -> string(secs-widthsecs-width: int,'0'literal: char
unicode= u0030
) ++std/core/types/(++): (x : string, y : string) -> string "."literal: string
count= 1
++std/core/types/(++): (x : string, y : string) -> string ff: string.pad-rightstd/core/string/pad-right: (s : string, width : int, fill : ? char) -> string(len3len3: int,'0'literal: char
unicode= u0030
) ++std/core/types/(++): (x : string, y : string) -> string unitunit: string
// Compare two clocks as by their total seconds. pub fun clock/cmpstd/time/date/clock/cmp: (c : clock, d : clock) -> order( cc: clock : clockstd/time/date/clock: V, dd: clock : clockstd/time/date/clock: V )result: -> total order : orderstd/core/types/order: V cmpstd/num/ddouble/cmp: (x : ddouble, y : ddouble) -> order(cc: clock.total-secondsstd/time/date/total-seconds: (c : clock) -> ddouble,dd: clock.total-secondsstd/time/date/total-seconds: (c : clock) -> ddouble) pub fun clock/(<)std/time/date/clock/(<): (i : clock, j : clock) -> bool( ii: clock : clockstd/time/date/clock: V, jj: clock : clockstd/time/date/clock: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/clock/cmp: (c : clock, d : clock) -> order(ii: clock,jj: clock) ==std/core/order/(==): (x : order, y : order) -> bool Ltstd/core/types/Lt: order } pub fun clock/(<=)std/time/date/clock/(<=): (i : clock, j : clock) -> bool( ii: clock : clockstd/time/date/clock: V, jj: clock : clockstd/time/date/clock: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/clock/cmp: (c : clock, d : clock) -> order(ii: clock,jj: clock) !=std/core/order/(!=): (x : order, y : order) -> bool Gtstd/core/types/Gt: order } pub fun clock/(>)std/time/date/clock/(>): (i : clock, j : clock) -> bool( ii: clock : clockstd/time/date/clock: V, jj: clock : clockstd/time/date/clock: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/clock/cmp: (c : clock, d : clock) -> order(ii: clock,jj: clock) ==std/core/order/(==): (x : order, y : order) -> bool Gtstd/core/types/Gt: order } pub fun clock/(>=)std/time/date/clock/(>=): (i : clock, j : clock) -> bool( ii: clock : clockstd/time/date/clock: V, jj: clock : clockstd/time/date/clock: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/clock/cmp: (c : clock, d : clock) -> order(ii: clock,jj: clock) !=std/core/order/(!=): (x : order, y : order) -> bool Ltstd/core/types/Lt: order } pub fun clock/(==)std/time/date/clock/(==): (i : clock, j : clock) -> bool( ii: clock : clockstd/time/date/clock: V, jj: clock : clockstd/time/date/clock: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/clock/cmp: (c : clock, d : clock) -> order(ii: clock,jj: clock) ==std/core/order/(==): (x : order, y : order) -> bool Eqstd/core/types/Eq: order } pub fun clock/(!=)std/time/date/clock/(!=): (i : clock, j : clock) -> bool( ii: clock : clockstd/time/date/clock: V, jj: clock : clockstd/time/date/clock: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/clock/cmp: (c : clock, d : clock) -> order(ii: clock,jj: clock) !=std/core/order/(!=): (x : order, y : order) -> bool Eqstd/core/types/Eq: order } // Add two clock together. pub fun clock/(+)std/time/date/clock/(+): (c : clock, d : clock) -> clock( cc: clock : clockstd/time/date/clock: V, dd: clock : clockstd/time/date/clock: V )result: -> total clock : clockstd/time/date/clock: V Clockstd/time/date/Clock: (hours : int, minutes : int, seconds : ddouble) -> clock(cc: clock.hoursstd/time/date/clock/hours: (clock : clock) -> int +std/core/int/(+): (x : int, y : int) -> int dd: clock.hoursstd/time/date/clock/hours: (clock : clock) -> int, cc: clock.minutesstd/time/date/clock/minutes: (clock : clock) -> int +std/core/int/(+): (x : int, y : int) -> int dd: clock.minutesstd/time/date/clock/minutes: (clock : clock) -> int, cc: clock.secondsstd/time/date/clock/seconds: (clock : clock) -> ddouble +std/num/ddouble/(+): (x : ddouble, y : ddouble) -> ddouble dd: clock.secondsstd/time/date/clock/seconds: (clock : clock) -> ddouble ) /*---------------------------------------------------------------------------- Days of the week ----------------------------------------------------------------------------*/ // An ISO day of the week, starting at Monday (=1) up to Sunday (=7). pub type weekdaystd/time/date/weekday: V Monstd/time/date/Mon: weekday Tuestd/time/date/Tue: weekday Wedstd/time/date/Wed: weekday Thustd/time/date/Thu: weekday Fristd/time/date/Fri: weekday Satstd/time/date/Sat: weekday Sunstd/time/date/Sun: weekday // Convert a `:weekday` to an `:int` using the ISO definition which starts at Monday as 1, // up to Sunday as 7. pub fun intstd/time/date/int: (wd : weekday) -> int( wdwd: weekday : weekdaystd/time/date/weekday: V )result: -> total int : intstd/core/types/int: V match wdwd: weekday Monstd/time/date/Mon: weekday -> 1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
Tuestd/time/date/Tue: weekday -> 2literal: int
dec = 2
hex8 = 0x02
bit8 = 0b00000010
Wedstd/time/date/Wed: weekday -> 3literal: int
dec = 3
hex8 = 0x03
bit8 = 0b00000011
Thustd/time/date/Thu: weekday -> 4literal: int
dec = 4
hex8 = 0x04
bit8 = 0b00000100
Fristd/time/date/Fri: weekday -> 5literal: int
dec = 5
hex8 = 0x05
bit8 = 0b00000101
Satstd/time/date/Sat: weekday -> 6literal: int
dec = 6
hex8 = 0x06
bit8 = 0b00000110
Sunstd/time/date/Sun: weekday ->
7literal: int
dec = 7
hex8 = 0x07
bit8 = 0b00000111
// Convert a weekday number to a `:weekday`(starting at Monday (=1) up to Sunday (=7)). // Takes the integer `i - 1` modulo 7, so `0` or `14` also become Sunday etc. pub fun weekdaystd/time/date/weekday: (i : int) -> weekday( ii: int : intstd/core/types/int: V )result: -> total weekday : weekdaystd/time/date/weekday: V val dd: int = ((ii: int -std/core/int/(-): (x : int, y : int) -> int 1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
)%std/core/int/(%): (int, int) -> int7literal: int
dec = 7
hex8 = 0x07
bit8 = 0b00000111
) if dd: int==std/core/int/(==): (x : int, y : int) -> bool0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
then Monstd/time/date/Mon: weekday elif dd: int==std/core/int/(==): (x : int, y : int) -> bool1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
then Tuestd/time/date/Tue: weekday elif dd: int==std/core/int/(==): (x : int, y : int) -> bool2literal: int
dec = 2
hex8 = 0x02
bit8 = 0b00000010
then Wedstd/time/date/Wed: weekday elif dd: int==std/core/int/(==): (x : int, y : int) -> bool3literal: int
dec = 3
hex8 = 0x03
bit8 = 0b00000011
then Thustd/time/date/Thu: weekday elif dd: int==std/core/int/(==): (x : int, y : int) -> bool4literal: int
dec = 4
hex8 = 0x04
bit8 = 0b00000100
then Fristd/time/date/Fri: weekday elif dd: int==std/core/int/(==): (x : int, y : int) -> bool5literal: int
dec = 5
hex8 = 0x05
bit8 = 0b00000101
then Satstd/time/date/Sat: weekday else Sunstd/time/date/Sun: weekday
// Show a `:weekday` as an English string (`Sun.show == "Sunday"`). pub fun weekday/showstd/time/date/weekday/show: (wd : weekday) -> string( wdwd: weekday : weekdaystd/time/date/weekday: V )result: -> total string : stringstd/core/types/string: V match wdwd: weekday Monstd/time/date/Mon: weekday -> "Monday"literal: string
count= 6
Tuestd/time/date/Tue: weekday -> "Tuesday"literal: string
count= 7
Wedstd/time/date/Wed: weekday -> "Wednesday"literal: string
count= 9
Thustd/time/date/Thu: weekday -> "Thursday"literal: string
count= 8
Fristd/time/date/Fri: weekday -> "Friday"literal: string
count= 6
Satstd/time/date/Sat: weekday -> "Saturday"literal: string
count= 8
Sunstd/time/date/Sun: weekday -> "Sunday"literal: string
count= 6
// Return the `:weekday` that comes `n` days after week day `wd`. pub fun weekday/(+)std/time/date/weekday/(+): (wd : weekday, n : int) -> weekday(wdwd: weekday : weekdaystd/time/date/weekday: V, nn: int : intstd/core/types/int: V )result: -> total weekday : weekdaystd/time/date/weekday: V (wdwd: weekday.intstd/time/date/int: (wd : weekday) -> int +std/core/int/(+): (x : int, y : int) -> int nn: int).weekdaystd/time/date/weekday: (i : int) -> weekday // Return the `:weekday` that comes `n` days before week day `wd`. pub fun weekdayint/(-)std/time/date/weekdayint/(-): (wd : weekday, n : int) -> weekday(wdwd: weekday : weekdaystd/time/date/weekday: V, nn: int : intstd/core/types/int: V )result: -> total weekday : weekdaystd/time/date/weekday: V (wdwd: weekday.intstd/time/date/int: (wd : weekday) -> int -std/core/int/(-): (x : int, y : int) -> int nn: int).weekdaystd/time/date/weekday: (i : int) -> weekday // Return the difference between two week days:\ // `wd2 == wd1 + (wd2 - wd1)` pub fun weekday/(-)std/time/date/weekday/(-): (wd1 : weekday, wd2 : weekday) -> int(wd1wd1: weekday : weekdaystd/time/date/weekday: V, wd2wd2: weekday : weekdaystd/time/date/weekday: V )result: -> total int : intstd/core/types/int: V (wd1wd1: weekday.intstd/time/date/int: (wd : weekday) -> int -std/core/int/(-): (x : int, y : int) -> int wd2wd2: weekday.intstd/time/date/int: (wd : weekday) -> int)%std/core/int/(%): (int, int) -> int7literal: int
dec = 7
hex8 = 0x07
bit8 = 0b00000111
// pad with zeros fun show0std/time/date/show0: (i : int, width : ? int) -> string( ii: int : intstd/core/types/int: V, widthwidth: ? int : intstd/core/types/int: V = 2literal: int
dec = 2
hex8 = 0x02
bit8 = 0b00000010
)result: -> total string : stringstd/core/types/string: V ii: int.showstd/core/int/show: (i : int) -> string.pad-leftstd/core/string/pad-left: (s : string, width : int, fill : ? char) -> string(widthwidth: int,'0'literal: char
unicode= u0030
) // Show a `:weekday` as a 3 letter English string (`Sun.show-short == "Sun"`) pub fun show-shortstd/time/date/show-short: (wd : weekday) -> string( wdwd: weekday : weekdaystd/time/date/weekday: V )result: -> total string : stringstd/core/types/string: V wdwd: weekday.showstd/time/date/weekday/show: (wd : weekday) -> string.firststd/core/sslice/first: (s : string, n : ? int) -> sslice(3literal: int
dec = 3
hex8 = 0x03
bit8 = 0b00000011
).stringstd/core/sslice/string: (slice : sslice) -> string
// Compare weekdays. pub fun weekday/cmpstd/time/date/weekday/cmp: (wd1 : weekday, wd2 : weekday) -> order( wd1wd1: weekday : weekdaystd/time/date/weekday: V, wd2wd2: weekday : weekdaystd/time/date/weekday: V )result: -> total order : orderstd/core/types/order: V cmpstd/core/int/cmp: (x : int, y : int) -> order(wd1wd1: weekday.intstd/time/date/int: (wd : weekday) -> int,wd2wd2: weekday.intstd/time/date/int: (wd : weekday) -> int) pub fun weekday/(<)std/time/date/weekday/(<): (i : weekday, j : weekday) -> bool( ii: weekday : weekdaystd/time/date/weekday: V, jj: weekday : weekdaystd/time/date/weekday: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/weekday/cmp: (wd1 : weekday, wd2 : weekday) -> order(ii: weekday,jj: weekday) ==std/core/order/(==): (x : order, y : order) -> bool Ltstd/core/types/Lt: order } pub fun weekday/(<=)std/time/date/weekday/(<=): (i : weekday, j : weekday) -> bool( ii: weekday : weekdaystd/time/date/weekday: V, jj: weekday : weekdaystd/time/date/weekday: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/weekday/cmp: (wd1 : weekday, wd2 : weekday) -> order(ii: weekday,jj: weekday) !=std/core/order/(!=): (x : order, y : order) -> bool Gtstd/core/types/Gt: order } pub fun weekday/(>)std/time/date/weekday/(>): (i : weekday, j : weekday) -> bool( ii: weekday : weekdaystd/time/date/weekday: V, jj: weekday : weekdaystd/time/date/weekday: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/weekday/cmp: (wd1 : weekday, wd2 : weekday) -> order(ii: weekday,jj: weekday) ==std/core/order/(==): (x : order, y : order) -> bool Gtstd/core/types/Gt: order } pub fun weekday/(>=)std/time/date/weekday/(>=): (i : weekday, j : weekday) -> bool( ii: weekday : weekdaystd/time/date/weekday: V, jj: weekday : weekdaystd/time/date/weekday: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/weekday/cmp: (wd1 : weekday, wd2 : weekday) -> order(ii: weekday,jj: weekday) !=std/core/order/(!=): (x : order, y : order) -> bool Ltstd/core/types/Lt: order } pub fun weekday/(==)std/time/date/weekday/(==): (i : weekday, j : weekday) -> bool( ii: weekday : weekdaystd/time/date/weekday: V, jj: weekday : weekdaystd/time/date/weekday: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/weekday/cmp: (wd1 : weekday, wd2 : weekday) -> order(ii: weekday,jj: weekday) ==std/core/order/(==): (x : order, y : order) -> bool Eqstd/core/types/Eq: order } pub fun weekday/(!=)std/time/date/weekday/(!=): (i : weekday, j : weekday) -> bool( ii: weekday : weekdaystd/time/date/weekday: V, jj: weekday : weekdaystd/time/date/weekday: V )result: -> total bool : boolstd/core/types/bool: V { cmpstd/time/date/weekday/cmp: (wd1 : weekday, wd2 : weekday) -> order(ii: weekday,jj: weekday) !=std/core/order/(!=): (x : order, y : order) -> bool Eqstd/core/types/Eq: order } /*---------------------------------------------------------------------------- ----------------------------------------------------------------------------*/ // Return the ISO calendar date of Easter in a given year (Algorithm by [J.M. Oudin](https://aa.usno.navy.mil/faq/docs/easter.php)). pub fun easterstd/time/date/easter: (year : int) -> date( yearyear: int : intstd/core/types/int: V )result: -> total date : datestd/time/date/date: V val cc: int = yearyear: int/std/core/int/(/): (x : int, y : int) -> int100literal: int
dec = 100
hex8 = 0x64
bit8 = 0b01100100
val nn: int = yearyear: int -std/core/int/(-): (x : int, y : int) -> int 19literal: int
dec = 19
hex8 = 0x13
bit8 = 0b00010011
*std/core/int/(*): (int, int) -> int(yearyear: int/std/core/int/(/): (x : int, y : int) -> int19literal: int
dec = 19
hex8 = 0x13
bit8 = 0b00010011
) val kk: int = (cc: int -std/core/int/(-): (x : int, y : int) -> int 17literal: int
dec = 17
hex8 = 0x11
bit8 = 0b00010001
)/std/core/int/(/): (x : int, y : int) -> int25literal: int
dec = 25
hex8 = 0x19
bit8 = 0b00011001
val i0i0: int = (((cc: int -std/core/int/(-): (x : int, y : int) -> int cc: int/std/core/int/(/): (x : int, y : int) -> int4literal: int
dec = 4
hex8 = 0x04
bit8 = 0b00000100
) -std/core/int/(-): (x : int, y : int) -> int (cc: int -std/core/int/(-): (x : int, y : int) -> int kk: int)/std/core/int/(/): (x : int, y : int) -> int3literal: int
dec = 3
hex8 = 0x03
bit8 = 0b00000011
) +std/core/int/(+): (x : int, y : int) -> int 19literal: int
dec = 19
hex8 = 0x13
bit8 = 0b00010011
*std/core/int/(*): (int, int) -> intnn: int) +std/core/int/(+): (x : int, y : int) -> int 15literal: int
dec = 15
hex8 = 0x0F
bit8 = 0b00001111
val i1i1: int = i0i0: int -std/core/int/(-): (x : int, y : int) -> int 30literal: int
dec = 30
hex8 = 0x1E
bit8 = 0b00011110
*std/core/int/(*): (int, int) -> int(i0i0: int/std/core/int/(/): (x : int, y : int) -> int30literal: int
dec = 30
hex8 = 0x1E
bit8 = 0b00011110
) val ii: int = i1i1: int -std/core/int/(-): (x : int, y : int) -> int (i1i1: int/std/core/int/(/): (x : int, y : int) -> int28literal: int
dec = 28
hex8 = 0x1C
bit8 = 0b00011100
)*std/core/int/(*): (int, int) -> int(1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
-std/core/int/(-): (x : int, y : int) -> int (i1i1: int/std/core/int/(/): (x : int, y : int) -> int28literal: int
dec = 28
hex8 = 0x1C
bit8 = 0b00011100
)*std/core/int/(*): (int, int) -> int(29literal: int
dec = 29
hex8 = 0x1D
bit8 = 0b00011101
/std/core/int/(/): (x : int, y : int) -> int(i1i1: int +std/core/int/(+): (x : int, y : int) -> int 1literal: int
dec = 1
hex8 = 0x01
bit8 = 0b00000001
))*std/core/int/(*): (int, int) -> int((21literal: int
dec = 21
hex8 = 0x15
bit8 = 0b00010101
-std/core/int/(-): (x : int, y : int) -> int nn: int)/std/core/int/(/): (x : int, y : int) -> int11literal: int
dec = 11
hex8 = 0x0B
bit8 = 0b00001011
)) val j0j0: int = ((yearyear: int +std/core/int/(+): (x : int, y : int) -> int yearyear: int/std/core/int/(/): (x : int, y : int) -> int4literal: int
dec = 4
hex8 = 0x04
bit8 = 0b00000100
+std/core/int/(+): (x : int, y : int) -> int ii: int +std/core/int/(+): (x : int, y : int) -> int 2literal: int
dec = 2
hex8 = 0x02
bit8 = 0b00000010
) -std/core/int/(-): (x : int, y : int) -> int cc: int) +std/core/int/(+): (x : int, y : int) -> int cc: int/std/core/int/(/): (x : int, y : int) -> int4literal: int
dec = 4
hex8 = 0x04
bit8 = 0b00000100
val jj: int = j0j0: int -std/core/int/(-): (x : int, y : int) -> int 7literal: int
dec = 7
hex8 = 0x07
bit8 = 0b00000111
*std/core/int/(*): (int, int) -> int(j0j0: int/std/core/int/(/): (x : int, y : int) -> int7literal: int
dec = 7
hex8 = 0x07
bit8 = 0b00000111
) val ll: int = ii: int -std/core/int/(-): (x : int, y : int) -> int jj: int val mm: int = 3literal: int
dec = 3
hex8 = 0x03
bit8 = 0b00000011
+std/core/int/(+): (x : int, y : int) -> int (ll: int +std/core/int/(+): (x : int, y : int) -> int 40literal: int
dec = 40
hex8 = 0x28
bit8 = 0b00101000
)/std/core/int/(/): (x : int, y : int) -> int44literal: int
dec = 44
hex8 = 0x2C
bit8 = 0b00101100
val dd: int = (ll: int +std/core/int/(+): (x : int, y : int) -> int 28literal: int
dec = 28
hex8 = 0x1C
bit8 = 0b00011100
) -std/core/int/(-): (x : int, y : int) -> int 31literal: int
dec = 31
hex8 = 0x1F
bit8 = 0b00011111
*std/core/int/(*): (int, int) -> int(mm: int/std/core/int/(/): (x : int, y : int) -> int4literal: int
dec = 4
hex8 = 0x04
bit8 = 0b00000100
) Datestd/time/date/Date: (year : int, month : int, day : int) -> date(yearyear: int,mm: int,dd: int
)