times
nimony/lib/std/times.nim
The std/times module provides basic support for working with time.
This is a minimal implementation for Nimony: it covers Unix-epoch based points in time with nanosecond resolution, simple durations, and a calendar breakdown in UTC, the process's local zone, or a Timezone of the application's own. For monotonic timestamps suitable for measuring durations, use std/monotimes.
type Month = HoleyEnumfunc dollar`.Month(e: Month): stringtype WeekDay = enum dMon = (0, "dMon") dTue = (1, "dTue") dWed = (2, "dWed") dThu = (3, "dThu") dFri = (4, "dFri") dSat = (5, "dSat") dSun = (6, "dSun")
func dollar`.WeekDay(e: WeekDay): stringtype Time = object seconds: int64 nanosecond: int32
type Duration = object seconds: int64 nanosecond: int32
type ZonedTime = object time: Time offsetEast: int64 isDst: bool
type Timezone = ref Timezone.Obj not niltype Timezone.Obj = object name: string zonedTimeFromTimeImpl: proc (time: Time): ZonedTime {.closure.} zonedTimeFromAdjTimeImpl: proc (adjTime: Time): ZonedTime {.closure.}
type DateTime = object year: int64 month: Month monthday: int32 hour: int32 minute: int32 second: int32 nanosecond: int32 weekday: WeekDay yearday: int32 offsetEast: int64 isDst: bool timezone: ref Timezone.Obj
func initTime(seconds: int64; nanoseconds: int64): TimeCreates a new
Timefrom a Unix timestamp.func fromUnix(unix: int64): TimeConvert a Unix timestamp (seconds since 1970-01-01 UTC) to a
Time.func toUnix(t: Time): int64Converts
tto a Unix timestamp.func seconds(t: Time): int64func nanosecond(t: Time): int32func initDuration(seconds: int64; nanoseconds: int64; milliseconds: int64; microseconds: int64; minutes: int64; hours: int64; days: int64; weeks: int64): DurationCreates a new
Duration.func inSeconds(d: Duration): int64func inMilliseconds(d: Duration): int64func inMicroseconds(d: Duration): int64func inNanoseconds(d: Duration): int64func ==(a: Time; b: Time): boolfunc <(a: Time; b: Time): boolfunc <=(a: Time; b: Time): boolfunc ==(a: Duration; b: Duration): boolfunc <(a: Duration; b: Duration): boolfunc <=(a: Duration; b: Duration): boolfunc -(a: Time; b: Time): DurationReturns the duration between two times.
func +(t: Time; d: Duration): TimeAdds a duration to a time.
func -(t: Time; d: Duration): TimeSubtracts a duration from a time.
proc getTime(): TimeGets the current time as a
Timewith up to nanosecond resolution.func isLeapYear(year: int64): boolReturns true if
yearis a leap year in the proleptic Gregorian calendar.func getDaysInMonth(month: Month; year: int64): int32Get the number of days in
monthofyear.func toTime(dt: DateTime): TimeConverts a
DateTimeto the instant it names: its fields, less itsoffsetEast.func offsetEast(dt: DateTime): int64Seconds east of UTC, DST included:
+12:00is43200, solocal = utc + offsetEast, as in ISO 8601. Nim 2'sutcOffsetcounts west; the different name keeps ported code from silently flipping sign.func isDst(dt: DateTime): boolWhether DST was in effect at this instant in
dt's zone.func timezone(dt: DateTime): ref Timezone.ObjThe zone the fields are expressed in; nil only for a
default(DateTime).proc newTimezone(name: string; zonedTimeFromTimeImpl: proc (time: Time): ZonedTime {.closure.}; zonedTimeFromAdjTimeImpl: proc (adjTime: Time): ZonedTime {.closure.}): ref Timezone.Obj not nilCreates a zone from its name and its two conversions.
proc name(zone: ref Timezone.Obj not nil): stringproc zonedTimeFromTime(zone: ref Timezone.Obj not nil; time: Time): ZonedTimeThe zone's offset and DST state at the instant
time.proc zonedTimeFromAdjTime(zone: ref Timezone.Obj not nil; adjTime: Time): ZonedTimeThe instant named by a wall-clock reading (
adjTime: the fields read as if they were UTC), with the offset that applied to it.proc $(zone: ref Timezone.Obj not nil): stringproc utc(): ref Timezone.Obj not nilThe UTC zone, named
Etc/UTC.proc local(): ref Timezone.Obj not nilThe process's zone (
TZ, else the system default), namedLOCAL. A process that inherits UTC answers UTC.proc inZone(time: Time; zone: ref Timezone.Obj not nil): DateTimeThe
DateTimefor the instanttimeinzone, sotoTime(inZone(t, zone)) == t.proc inZone(dt: DateTime; zone: ref Timezone.Obj not nil): DateTimeThe same instant, expressed in
zone.proc initDateTime(year: int64; month: Month; monthday: int32; hour: int32; minute: int32; second: int32; nanosecond: int32; zone: ref Timezone.Obj not nil): DateTimeCreates a
DateTimefrom a wall-clock reading inzone.proc utc(t: Time): DateTimeConverts a
Timeto aDateTimein UTC.proc local(t: Time): DateTimeConverts a
Timeto aDateTimein the process's zone.proc utc(dt: DateTime): DateTimeproc local(dt: DateTime): DateTimeproc now(): DateTimeReturns the current UTC date and time.
func $(dt: DateTime): stringConverts a
DateTimeto ISO-8601:YYYY-MM-DDTHH:MM:SSfollowed byZin UTC (or with no zone) and±HH:MMin any other zone.func $(t: Time): stringA
Timenames an instant, so it renders in UTC, withZ.func $(d: Duration): stringFormats a
DurationasNs M.N(seconds.nanoseconds).