This module contains routines and types for dealing with time. This module is available for the JavaScript target.
Examples:
import times, os var t = cpuTime() sleep(100) # replace this with something to be timed echo "Time taken: ",cpuTime() - t echo "My formatted time: ", format(getLocalTime(getTime()), "d MMMM yyyy HH:mm") echo "Using predefined formats: ", getClockStr(), " ", getDateStr() echo "epochTime() float value: ", epochTime() echo "getTime() float value: ", toSeconds(getTime()) echo "cpuTime() float value: ", cpuTime() echo "An hour from now : ", getLocalTime(getTime()) + 1.hours echo "An hour from (UTC) now: ", getGmTime(getTime()) + initInterval(0,0,0,1)
Month = enum mJan, mFeb, mMar, mApr, mMay, mJun, mJul, mAug, mSep, mOct, mNov, mDec
WeekDay = enum dMon, dTue, dWed, dThu, dFri, dSat, dSun
Time = distinct TimeImpl
TimeInfo = object of RootObj second*: range[0 .. 61] ## The number of seconds after the minute, ## normally in the range 0 to 59, but can ## be up to 61 to allow for leap seconds. minute*: range[0 .. 59] ## The number of minutes after the hour, ## in the range 0 to 59. hour*: range[0 .. 23] ## The number of hours past midnight, ## in the range 0 to 23. monthday*: range[1 .. 31] ## The day of the month, in the range 1 to 31. month*: Month ## The current month. year*: int ## The current year. weekday*: WeekDay ## The current day of the week. yearday*: range[0 .. 365] ## The number of days since January 1, ## in the range 0 to 365. ## Always 0 if the target is JS. isDST*: bool ## Determines whether DST is in effect. ## Semantically, this adds another negative hour ## offset to the time in addition to the timezone. timezone*: int ## The offset of the (non-DST) timezone in seconds ## west of UTC. Note that the sign of this number ## is the opposite of the one in a formatted ## timezone string like ``+01:00`` (which would be ## parsed into the timezone ``-3600``).
TimeInterval = object milliseconds*: int ## The number of milliseconds seconds*: int ## The number of seconds minutes*: int ## The number of minutes hours*: int ## The number of hours days*: int ## The number of days months*: int ## The number of months years*: int ## The number of years
proc fromSeconds(since1970: int64): Time {.tags: [], raises: [], gcsafe, locks: 0.}
proc `<`(a, b: Time): bool {.gcsafe, extern: "ntLtTime", tags: [], raises: [], noSideEffect.}
a < b
, that is iff a happened before b. proc `<=`(a, b: Time): bool {.gcsafe, extern: "ntLeTime", tags: [], raises: [], noSideEffect.}
a <= b
. proc `==`(a, b: Time): bool {.gcsafe, extern: "ntEqTime", tags: [], raises: [], noSideEffect.}
a == b
, that is if both times represent the same value proc initInterval(milliseconds, seconds, minutes, hours, days, months, years: int = 0): TimeInterval {. raises: [], tags: [].}
creates a new TimeInterval
.
You can also use the convenience procedures called milliseconds
, seconds
, minutes
, hours
, days
, months
, and years
.
Example:
let day = initInterval(hours=24) let tomorrow = getTime() + day echo(tomorrow)
proc `+`(ti1, ti2: TimeInterval): TimeInterval {.raises: [], tags: [].}
TimeInterval
objects together. proc `-`(ti: TimeInterval): TimeInterval {.raises: [], tags: [].}
let day = -initInterval(hours=24) echo day # -> (milliseconds: 0, seconds: 0, minutes: 0, hours: 0, days: -1, months: 0, years: 0)
proc `-`(ti1, ti2: TimeInterval): TimeInterval {.raises: [], tags: [].}
Subtracts TimeInterval ti1
from ti2
.
Time components are compared one-by-one, see output:
let a = fromSeconds(1_000_000_000) let b = fromSeconds(1_500_000_000) echo b.toTimeInterval - a.toTimeInterval # (milliseconds: 0, seconds: -40, minutes: -6, hours: 1, days: -2, months: -2, years: 16)
proc isLeapYear(year: int): bool {.raises: [], tags: [].}
year
is a leap year proc getDaysInMonth(month: Month; year: int): int {.raises: [], tags: [].}
month
of a year
proc getDaysInYear(year: int): int {.raises: [], tags: [].}
year
proc `+`(a: TimeInfo; interval: TimeInterval): TimeInfo {.raises: [Exception], tags: [TimeEffect].}
adds interval
time from TimeInfo a
.
Note: This has been only briefly tested and it may not be very accurate.
proc `-`(a: TimeInfo; interval: TimeInterval): TimeInfo {.raises: [Exception], tags: [TimeEffect].}
subtracts interval
time from TimeInfo a
.
Note: This has been only briefly tested, it is inaccurate especially when you subtract so much that you reach the Julian calendar.
proc miliseconds(t: TimeInterval): int {.deprecated, raises: [], tags: [].}
proc miliseconds=(t: var TimeInterval; milliseconds: int) {.deprecated, raises: [], tags: [].}
An alias for a misspelled field in TimeInterval
.
Warning: This should not be used! It will be removed in the next version.
proc getDateStr(): string {.gcsafe, extern: "nt$1", tags: [TimeEffect], raises: [Exception].}
YYYY-MM-DD
. proc getClockStr(): string {.gcsafe, extern: "nt$1", tags: [TimeEffect], raises: [Exception].}
HH:MM:SS
. proc `$`(day: WeekDay): string {.raises: [], tags: [].}
WeekDay
. proc `$`(m: Month): string {.raises: [], tags: [].}
Month
. proc milliseconds(ms: int): TimeInterval {.inline, raises: [], tags: [].}
TimeInterval of ms milliseconds
Note: not all time functions have millisecond resolution
proc seconds(s: int): TimeInterval {.inline, raises: [], tags: [].}
TimeInterval of s seconds
echo getTime() + 5.second
proc minutes(m: int): TimeInterval {.inline, raises: [], tags: [].}
TimeInterval of m minutes
echo getTime() + 5.minutes
proc hours(h: int): TimeInterval {.inline, raises: [], tags: [].}
TimeInterval of h hours
echo getTime() + 2.hours
proc days(d: int): TimeInterval {.inline, raises: [], tags: [].}
TimeInterval of d days
echo getTime() + 2.days
proc months(m: int): TimeInterval {.inline, raises: [], tags: [].}
TimeInterval of m months
echo getTime() + 2.months
proc years(y: int): TimeInterval {.inline, raises: [], tags: [].}
TimeInterval of y years
echo getTime() + 2.years
proc `+=`(t: var Time; ti: TimeInterval) {.raises: [Exception], tags: [TimeEffect].}
proc `+`(t: Time; ti: TimeInterval): Time {.raises: [Exception], tags: [TimeEffect].}
adds the interval ti to Time t by converting to localTime, adding the interval, and converting back
echo getTime() + 1.day
proc `-=`(t: var Time; ti: TimeInterval) {.raises: [Exception], tags: [TimeEffect].}
proc `-`(t: Time; ti: TimeInterval): Time {.raises: [Exception], tags: [TimeEffect].}
subtracts the interval ti from Time t
echo getTime() - 1.day
proc format(info: TimeInfo; f: string): string {.raises: [ValueError], tags: [].}
Specifier | Description | Example |
---|---|---|
d | Numeric value of the day of the month, it will be one or two digits long. |
1/04/2012 -> 1 , 21/04/2012 -> 21
|
dd | Same as above, but always two digits. |
1/04/2012 -> 01 , 21/04/2012 -> 21
|
ddd | Three letter string which indicates the day of the week. |
Saturday -> Sat , Monday -> Mon
|
dddd | Full string for the day of the week. |
Saturday -> Saturday , Monday -> Monday
|
h | The hours in one digit if possible. Ranging from 0-12. |
5pm -> 5 , 2am -> 2
|
hh | The hours in two digits always. If the hour is one digit 0 is prepended. |
5pm -> 05 , 11am -> 11
|
H | The hours in one digit if possible, randing from 0-24. |
5pm -> 17 , 2am -> 2
|
HH | The hours in two digits always. 0 is prepended if the hour is one digit. |
5pm -> 17 , 2am -> 02
|
m | The minutes in 1 digit if possible. |
5:30 -> 30 , 2:01 -> 1
|
mm | Same as above but always 2 digits, 0 is prepended if the minute is one digit. |
5:30 -> 30 , 2:01 -> 01
|
M | The month in one digit if possible. |
September -> 9 , December -> 12
|
MM | The month in two digits always. 0 is prepended. |
September -> 09 , December -> 12
|
MMM | Abbreviated three-letter form of the month. |
September -> Sep , December -> Dec
|
MMMM | Full month string, properly capitalized. | September -> September |
s | Seconds as one digit if possible. | 00:00:06 -> 6 |
ss | Same as above but always two digits. 0 is prepended. | 00:00:06 -> 06 |
t |
A when time is in the AM. P when time is in the PM. |
|
tt | Same as above, but AM and PM instead of A and P respectively. |
|
y(yyyy) | This displays the year to different digits. You most likely only want 2 or 4 'y's | |
yy | Displays the year to two digits. | 2012 -> 12 |
yyyy | Displays the year to four digits. | 2012 -> 2012 |
z | Displays the timezone offset from UTC. |
GMT+7 -> +7 , GMT-5 -> -5
|
zz | Same as above but with leading 0. |
GMT+7 -> +07 , GMT-5 -> -05
|
zzz | Same as above but with :mm where mm represents minutes. |
GMT+7 -> +07:00 , GMT-5 -> -05:00
|
Other strings can be inserted by putting them in ''
. For example hh'->'mm
will give 01->56
. The following characters can be inserted without quoting them: :
-
(
)
/
[
]
,
. However you don't need to necessarily separate format specifiers, a unambiguous format string like yyyyMMddhhmmss
is valid too.
proc `$`(timeInfo: TimeInfo): string {.tags: [], raises: [], gcsafe, locks: 0.}
yyyy-MM-dd'T'HH-mm-sszzz
. proc `$`(time: Time): string {.tags: [TimeEffect], raises: [], gcsafe, locks: 0.}
yyyy-MM-dd'T'HH-mm-sszzz
. proc parse(value, layout: string): TimeInfo {. raises: [Exception, OverflowError, ValueError], tags: [TimeEffect].}
Specifier | Description | Example |
---|---|---|
d | Numeric value of the day of the month, it will be one or two digits long. |
1/04/2012 -> 1 , 21/04/2012 -> 21
|
dd | Same as above, but always two digits. |
1/04/2012 -> 01 , 21/04/2012 -> 21
|
ddd | Three letter string which indicates the day of the week. |
Saturday -> Sat , Monday -> Mon
|
dddd | Full string for the day of the week. |
Saturday -> Saturday , Monday -> Monday
|
h | The hours in one digit if possible. Ranging from 0-12. |
5pm -> 5 , 2am -> 2
|
hh | The hours in two digits always. If the hour is one digit 0 is prepended. |
5pm -> 05 , 11am -> 11
|
H | The hours in one digit if possible, randing from 0-24. |
5pm -> 17 , 2am -> 2
|
HH | The hours in two digits always. 0 is prepended if the hour is one digit. |
5pm -> 17 , 2am -> 02
|
m | The minutes in 1 digit if possible. |
5:30 -> 30 , 2:01 -> 1
|
mm | Same as above but always 2 digits, 0 is prepended if the minute is one digit. |
5:30 -> 30 , 2:01 -> 01
|
M | The month in one digit if possible. |
September -> 9 , December -> 12
|
MM | The month in two digits always. 0 is prepended. |
September -> 09 , December -> 12
|
MMM | Abbreviated three-letter form of the month. |
September -> Sep , December -> Dec
|
MMMM | Full month string, properly capitalized. | September -> September |
s | Seconds as one digit if possible. | 00:00:06 -> 6 |
ss | Same as above but always two digits. 0 is prepended. | 00:00:06 -> 06 |
t |
A when time is in the AM. P when time is in the PM. |
|
tt | Same as above, but AM and PM instead of A and P respectively. |
|
yy | Displays the year to two digits. | 2012 -> 12 |
yyyy | Displays the year to four digits. | 2012 -> 2012 |
z | Displays the timezone offset from UTC. Z is parsed as +0
|
GMT+7 -> +7 , GMT-5 -> -5
|
zz | Same as above but with leading 0. |
GMT+7 -> +07 , GMT-5 -> -05
|
zzz | Same as above but with :mm where mm represents minutes. |
GMT+7 -> +07:00 , GMT-5 -> -05:00
|
Other strings can be inserted by putting them in ''
. For example hh'->'mm
will give 01->56
. The following characters can be inserted without quoting them: :
-
(
)
/
[
]
,
. However you don't need to necessarily separate format specifiers, a unambiguous format string like yyyyMMddhhmmss
is valid too.
proc countLeapYears(yearSpan: int): int {.raises: [], tags: [].}
Returns the number of leap years spanned by a given number of years.
Note: For leap years, start date is assumed to be 1 AD. counts the number of leap years up to January 1st of a given year. Keep in mind that if specified year is a leap year, the leap day has not happened before January 1st of that year.
proc countDays(yearSpan: int): int {.raises: [], tags: [].}
proc countYears(daySpan: int): int {.raises: [], tags: [].}
proc countYearsAndDays(daySpan: int): tuple[years: int, days: int] {.raises: [], tags: [].}
proc getDayOfWeek(day, month, year: int): WeekDay {.raises: [], tags: [].}
proc getDayOfWeekJulian(day, month, year: int): WeekDay {.raises: [], tags: [].}
proc timeToTimeInfo(t: Time): TimeInfo {.deprecated, raises: [], tags: [].}
Converts a Time to TimeInfo.
Warning: This procedure is deprecated since version 0.14.0. Use getLocalTime
or getGMTime
instead.
proc timeToTimeInterval(t: Time): TimeInterval {.deprecated, raises: [], tags: [TimeEffect].}
Converts a Time to a TimeInterval.
Warning: This procedure is deprecated since version 0.14.0. Use toTimeInterval
instead.
proc toTimeInterval(t: Time): TimeInterval {.raises: [], tags: [TimeEffect].}
Converts a Time to a TimeInterval.
To be used when diffing times.
let a = fromSeconds(1_000_000_000) let b = fromSeconds(1_500_000_000) echo a, " ", b # real dates echo a.toTimeInterval # meaningless value, don't use it by itself echo b.toTimeInterval - a.toTimeInterval # (milliseconds: 0, seconds: -40, minutes: -6, hours: 1, days: -2, months: -2, years: 16)
proc `-`(a, b: Time): int64 {.gcsafe, extern: "ntDiffTime", tags: [], raises: [], noSideEffect, gcsafe, locks: 0.}
let a = fromSeconds(1_000_000_000) let b = fromSeconds(1_500_000_000) echo initInterval(seconds=int(b - a)) # (milliseconds: 0, seconds: 20, minutes: 53, hours: 0, days: 5787, months: 0, years: 0)
proc getStartMilsecs(): int {.deprecated, tags: [TimeEffect], gcsafe, locks: 0, raises: [].}
epochTime
or cpuTime
instead. proc getTime(): Time {.tags: [TimeEffect], gcsafe, locks: 0, raises: [].}
proc getLocalTime(t: Time): TimeInfo {.tags: [TimeEffect], raises: [], gcsafe, locks: 0.}
proc getGMTime(t: Time): TimeInfo {.tags: [TimeEffect], raises: [], gcsafe, locks: 0.}
proc toTime(timeInfo: TimeInfo): Time {.tags: [TimeEffect], gcsafe, locks: 0, raises: [].}
proc timeInfoToTime(timeInfo: TimeInfo): Time {.tags: [TimeEffect], gcsafe, locks: 0, deprecated, raises: [].}
converts a broken-down time structure to calendar time representation. The function ignores the specified contents of the structure members weekday and yearday and recomputes them from the other information in the broken-down time structure.
Warning: This procedure is deprecated since version 0.14.0. Use toTime
instead.
proc unixTimeToWinTime(t: Time): int64 {.raises: [], tags: [].}
time_t
) to a Windows file time proc winTimeToUnixTime(t: int64): Time {.raises: [], tags: [].}
time_t
) proc getTimezone(): int {.tags: [TimeEffect], raises: [], gcsafe, locks: 0.}
proc fromSeconds(since1970: float): Time {.tags: [], raises: [], gcsafe, locks: 0.}
proc toSeconds(time: Time): float {.tags: [], raises: [], gcsafe, locks: 0.}
proc epochTime(): float {.gcsafe, extern: "nt$1", tags: [TimeEffect], raises: [].}
proc cpuTime(): float {.gcsafe, extern: "nt$1", tags: [TimeEffect], raises: [].}
epochTime
. However, it may measure the real time instead (depending on the OS). The value of the result has no meaning. To generate useful timing values, take the difference between the results of two cpuTime
calls:var t0 = cpuTime() doWork() echo "CPU time [s] ", cpuTime() - t0
© 2006–2017 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/times.html