::
Library : nimcx.nim Module : cxtime.nim Status : stable License : MIT opensource Latest : 2022-12-17 Compiler : latest stable or devel branch OS : Linux Description : provides most time related functions for the libraryiso week number borrowed from https://gist.github.com/pietroppeter/e6afa43318b202ef2a2a32e0fd3844bf see https://en.wikipedia.org/wiki/ISO_week_date#Calculating_the_week_number_of_a_given_date
Types
Cxcounter = object value*: int
CxTimer {.packed.} = object name*: string start*: float lap*: seq[float]
Cxtimerres = tuple[tname: string, start: float, stop: float, lap: seq[float]]
cxTz = enum long, short
Vars
cxtimerresults = newSeq(Natural(0))
Procs
proc clearAllTimerResults(quiet: bool = true; xpos: int = 3) {. ...raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
-
clearAllTimerResults
clears cxtimerresults for all timers , set quiet to false to show feedback
proc clearTimerResults(aname: string = ""; quiet: bool = true; xpos: int = 3) {. ...raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
-
clearTimerResults
clears cxtimerresults of one named timer or if aname == "" of timer cxtimer
proc compareDates(startDate, endDate: string): int {....raises: [ValueError], tags: [], forbids: [].}
proc createSeqDate(fromDate: string; days: int = 1): seq[string] {. ...raises: [ValueError, TimeParseError, IOError], tags: [TimeEffect, WriteIOEffect], forbids: [].}
-
createSeqDate
creates a seq of continous dates in format yyyy-MM-dd
from fromDate to fromDate + days
showSeq(createseqdate(cxToday, 60),maxitemwidth=12)
proc createSeqDate(fromDate: string; toDate: string): seq[string] {. ...raises: [ValueError, TimeParseError, IOError], tags: [TimeEffect, WriteIOEffect], forbids: [].}
-
createSeqDate
creates a seq of dates in format yyyy-MM-dd
fromDate and toDate must be a date in form yyyy-MM-dd eg. 2018-06-18
showSeq(createseqdate(cxtoday(), "2022-05-25"),maxitemwidth=12)
proc cxDayOfWeek(datestr: string): string {....raises: [ValueError, IOError], tags: [WriteIOEffect], forbids: [].}
-
cxDayOfWeek
returns day of week from a date in format yyyy-MM-dd
echo cxDayOfWeek(cxtoday()) echo getNextMonday("2017-07-15")," ",cxDayOfWeek(getNextMonday("2017-07-15"))
proc cxDuration(tstart: int | float; tend: int | float): string
-
cxDuration
expects tstart and tend to be seconds
echo "Duration: " ,cxDuration(epochTime() - 201235.32,epochTime()) echo "Duration: " ,cxDuration(0,567887) # output: # Duration: 2 days, 7 hours, 53 minutes, and 55 seconds # Duration: 6 days, 13 hours, 44 minutes, and 47 seconds
proc cxHRTimer(tstart: Time = getTime(); tend: Time = getTime()): auto {. ...raises: [], tags: [], forbids: [].}
-
cxHRTimer
Example
let currenttime = getTime() sleep(2000) let later = gettime() echo cxHRTimer(currentime,later)
proc cxSystemUptime(): string {....raises: [OSError, IOError], tags: [ExecIOEffect, ReadIOEffect, RootEffect], forbids: [].}
- Returns system uptime
proc cxTimeZone(amode: cxTz = long): string {....raises: [], tags: [TimeEffect], forbids: [].}
-
cxTimeZone
returns a string with the actual timezone offset in hours as seen from UTC
default long gives results parsed from getLocalTime like : UTC +08:00
proc datetimeunpack[T](d: T): string
proc day(aDate: string): string {....raises: [], tags: [], forbids: [].}
-
day,month year extracts the relevant part from
a date string of format yyyy-MM-dd
proc getAmzDateString(): string {....raises: [], tags: [TimeEffect], forbids: [].}
-
getAmzDateString
get current GMT date time in amazon format
proc getMsecs(d: Duration): float64 {....raises: [], tags: [], forbids: [].}
-
getMsecs
returns duration in milliseconds duration d can be obtained like so let start = getMonoTime() sleep(5) let end = getMonoTime() let d = end - start printLnBiCol "Timing : " & ff(getMsecs(d),6) & " msn"
proc getNextMonday(adate: string): string {. ...raises: [IOError, ValueError, TimeParseError], tags: [WriteIOEffect, TimeEffect], forbids: [].}
-
getNextMonday
echo getNextMonday(getDateStr())
import nimcx # get next 10 mondays var dw = getdatestr() echo dw for x in 1..10: dw = getnextmonday(dw) echo dw dw = plusDays(dw,1)
proc getRndDate(minyear: int = parseInt(year(getDateStr(now()))) - 50; maxyear: int = parseInt(year(getDateStr(now()))) + 50): string {. ...raises: [ValueError], tags: [], forbids: [].}
-
getRndDate
returns a valid rand date between 1900 and 3001 in format 2017-12-31
default currently set to between +/- 50 years of today
import nimcx loopy2(0,100): echo getRndDate(2016,2025)
proc isoweek(dt: DateTime): WeekRange {....raises: [], tags: [], forbids: [].}
- returns isoweek for a given date
proc minusDays(aDate: string; days: int): string {. ...raises: [ValueError, TimeParseError, IOError], tags: [TimeEffect, WriteIOEffect], forbids: [].}
-
minusDays
subtracts days from a date string of format yyyy-MM-dd or result of getDateStr() or today()
and returns a string of format yyyy-MM-dd
the passed in date string must be a valid date or an error message will be returned
proc month(aDate: string): string {....raises: [ValueError], tags: [], forbids: [].}
proc newCxtimer(aname: string = "cxtimer"): ref (CxTimer) {....raises: [], tags: [], forbids: [].}
-
newCxtimer
set up a new cxtimer
simple timer with starttimer,stoptimer,laptimer,resettimer functionality
Example
var ct = newCxtimer("TestTimer1") # create a cxtimer with name TestTimer1 var ct2 = newCxtimer() # create a cxtimer which will have default name cxtimer ct.startTimer # start a timer ct2.startTimer loopy2(0,2): sleepy(1) ct2.laptimer # take a laptime for a timer ct.stopTimer # stop a timer ct2.stopTimer saveTimerResults(ct) # save current state of a timer saveTimerResults(ct2) echo() showTimerResults() # display status of all timers ct2.resetTimer # reset a particular timer clearTimerResults() # clear timer result of default timer clearTimerResults("TestTimer1") # clear timer results of a particular timer clearAllTimerResults() # clear all timer results showTimerResults() dprint cxtimerresults # dprint is a simple repr utility
proc plusDays(aDate: string; days: int): string {. ...raises: [ValueError, TimeParseError, IOError], tags: [TimeEffect, WriteIOEffect], forbids: [].}
-
plusDays
adds days to date string of format yyyy-MM-dd or result of getDateStr() or today()
and returns a string of format yyyy-MM-dd
the passed in date string must be a valid date or an error message will be returned
echo plusDays(cxtoday(),343)
proc printDateMsg(atext: string = getDateStr(); xpos: int = 1): string {. discardable, ...raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
proc printDTimeMsg(atext: string = $toTime(now()); xpos: int = 1): string {. discardable, ...raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
proc printLnDateMsg(atext: string = getDateStr(); xpos: int = 1): string {. discardable, ...raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
proc printLnDTimeMsg(atext: string = $toTime(now()); xpos: int = 1): string {. discardable, ...raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
proc printLnTimeMsg(atext: string = cxTime; xpos: int = 1): string {. discardable, ...raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
proc printTimeMsg(atext: string = cxTime; xpos: int = 1): string {.discardable, ...raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
proc resetTimer(co: ref (CxTimer)) {....raises: [], tags: [], forbids: [].}
proc saveTimerResults(b: ref (CxTimer)) {....raises: [], tags: [], forbids: [].}
proc showTimerResults() {....raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
-
showTimerResults
shows results for all timers
proc showTimerResults(aname: string) {....raises: [IOError, ValueError], tags: [WriteIOEffect], forbids: [].}
-
showTimerResults
shows results for a particular timer name
proc sleepy[T: float | int](secs: T)
-
sleepy
imitates sleep but in seconds suitable for shorter sleeps
proc startTimer(co: ref (CxTimer)) {....raises: [], tags: [TimeEffect], forbids: [].}
proc validDate(adate: string): bool {....raises: [ValueError], tags: [], forbids: [].}
-
validdate
try to ensure correct dates of form yyyy-MM-dd
correct : 2015-08-15
wrong : 2015-08-32 , 201508-15, 2015-13-10 etc.
proc year(aDate: string): string {....raises: [], tags: [], forbids: [].}
- Format yyyy
Templates
template cxDateTime(): untyped
-
cxDateTime
returns a date time string
eg : 2018-08-23 16:48:50
template cxLocal(): untyped
-
cxLocal
returns local datetime in a string same as now()
Examples for var. cx time,date functions
printLnBiCol(["cxLocal : ",cxLocal()]) printLnBiCol(["cxNow : ",cxNow()]) printLnBiCol(["cxTime : ",cxTime()]) printLnBiCol(["cxToday : ",cxToday()]) printLnBiCol(["cxTimeZone(long) : ",cxTimezone(long)]) printLnBiCol(["cxTimeZone(short) : ",cxTimezone(short)])
template cxNow(): untyped
-
cxnow
formated datetime without the T and zone details
eg: 2018-08-23 16:48:50+08.00
template cxTime(): untyped
-
cxTime
eg : 16:48:50
template cxToday(): untyped
-
today
returns date string
eg : 2018-08-23