derive Comparable DateTime derive Comparable LocalDateTime /** * The DateTime type represents a date and time at UTC. * * @since 0.15.0 * @example * now() */ export type DateTime = DateTime(Integer) /** * The LocalDateTime type represents a date and time for * the timezone of the local machine. * * @since 0.15.0 */ export type LocalDateTime = LocalDateTime(Integer) /** * Date info is a record representation of a DateTime with * fields going up to the millisecond. * * month range is: [1-12] * day range is: [1-31] * hours range is: [0-23] * minutes and seconds range is: [0-59] * milliseconds range is: [0-999] * * @since 0.15.0 */ export alias DateInfo = { milliseconds :: Integer, seconds :: Integer, minutes :: Integer, hours :: Integer, day :: Integer, month :: Integer, year :: Integer, } /** * TimeUnit type used for some operations on dates * * @since 0.15.0 */ export type TimeUnit = Millisecond | Second | Minute | Hour | Day | Month | Year #iftarget llvm nowFFI :: {} -> Integer nowFFI = extern "madlib__date__now" toISOStringFFI :: Integer -> String toISOStringFFI = extern "madlib__date__toISOString" toDateInfoFFI :: Integer -> DateInfo toDateInfoFFI = extern "madlib__date__toDateInfo" fromDateInfoFFI :: DateInfo -> Integer fromDateInfoFFI = extern "madlib__date__fromDateInfo" #elseif js nowFFI :: {} -> Integer nowFFI = extern "Date.now" toISOStringFFI :: Integer -> String toISOStringFFI = (date) => #- new Date(date).toISOString() -# toDateInfoFFI :: Integer -> DateInfo toDateInfoFFI = (date) => #- { const d = new Date(date) return { milliseconds: d.getUTCMilliseconds(), seconds: d.getUTCSeconds(), minutes: d.getUTCMinutes(), hours: d.getUTCHours(), day: d.getUTCDate(), month: d.getUTCMonth() + 1, year: d.getUTCFullYear(), } } -# fromDateInfoFFI :: DateInfo -> Integer fromDateInfoFFI = (dateInfo) => #- { return Date.UTC( dateInfo.year, dateInfo.month - 1, dateInfo.day, dateInfo.hours, dateInfo.minutes, dateInfo.seconds, dateInfo.milliseconds ) } -# #endif /** * Returns the current time as a DateTime value * * @since 0.15.0 * @example * now() */ now :: {} -> DateTime export now = pipe(nowFFI, DateTime) /** * Converts a DateTime to a String in ISO format: * {year}-{month}-{day}T{hours}:{minutes}:{seconds}.{milliseconds}Z * * @since 0.15.0 * @example * toISOString(now()) // "2022-10-13T19:01:00.000Z" at the time of writing this */ toISOString :: DateTime -> String export toISOString = (date) => pipe( getTimestamp, toISOStringFFI )(date) /** * Returns the inner integer timestamp of a DateTime * * @since 0.15.0 * @example * getTimeStamp(now()) */ getTimestamp :: DateTime -> Integer export getTimestamp = (date) => where(date) { DateTime(t) => t } /** * Converts a DateTime value to a DateInfo record, allowing to easily read * all components of a date such as the month or day. * * @since 0.15.0 * @example * toDateInfo(now()) */ toDateInfo :: DateTime -> DateInfo export toDateInfo = (date) => pipe( getTimestamp, toDateInfoFFI )(date) /** * Creates a DateTime value from a DateInfo record. * * @since 0.15.0 * @example * fromDateInfo({ * year: 2022, * month: 12, * day: 31, * hours: 21, * minutes: 45, * seconds: 0, * milliseconds: 0, * }) * // DateTime(1672523100000) */ fromDateInfo :: DateInfo -> DateTime export fromDateInfo = (dateInfo) => pipe( fromDateInfoFFI, DateTime )(dateInfo) /** * Adds a temporal value to a DateTime * * @since 0.15.0 * @example * add(1, Hour, DateTime(0)) // DateTime(3600000) */ add :: Integer -> TimeUnit -> DateTime -> DateTime export add = (x, unit, date) => where(unit) { Millisecond => DateTime(getTimestamp(date) + x) Second => DateTime(getTimestamp(date) + x * 1000) Minute => DateTime(getTimestamp(date) + x * 60 * 1000) Hour => DateTime(getTimestamp(date) + x * 60 * 60 * 1000) Day => DateTime(getTimestamp(date) + x * 24 * 60 * 60 * 1000) Month => pipe( toDateInfo, (di) => ({ ...di, month: di.month + x }), fromDateInfo )(date) Year => pipe( toDateInfo, (di) => ({ ...di, year: di.year + x }), fromDateInfo )(date) } addMilliseconds :: Integer -> DateTime -> DateTime export addMilliseconds = add($, Millisecond) addSeconds :: Integer -> DateTime -> DateTime export addSeconds = add($, Second) addMinutes :: Integer -> DateTime -> DateTime export addMinutes = add($, Minute) addHours :: Integer -> DateTime -> DateTime export addHours = add($, Hour) addDays :: Integer -> DateTime -> DateTime export addDays = add($, Day) addMonths :: Integer -> DateTime -> DateTime export addMonths = add($, Month) addYears :: Integer -> DateTime -> DateTime export addYears = add($, Year)