getDateStr and getClockStr takes a default parameter (#11108)

This commit is contained in:
liuxiaodong 2019-05-21 15:54:09 +08:00 • committed by Andreas Rumpf
commit 5e552ad3a5

View file

@ -1280,15 +1280,17 @@ proc `-`*(ti1, ti2: TimeInterval): TimeInterval =
result = ti1 + (-ti2) result = ti1 + (-ti2)
proc getDateStr*(): string {.rtl, extern: "nt$1", tags: [TimeEffect].} = proc getDateStr*(dt = now()): string {.rtl, extern: "nt$1", tags: [TimeEffect].} =
## Gets the current local date as a string of the format ``YYYY-MM-DD``. ## Gets the current local date as a string of the format ``YYYY-MM-DD``.
var dt = now() runnableExamples:
echo getDateStr(now() - 1.months)
result = $dt.year & '-' & intToStr(ord(dt.month), 2) & result = $dt.year & '-' & intToStr(ord(dt.month), 2) &
'-' & intToStr(dt.monthday, 2) '-' & intToStr(dt.monthday, 2)
proc getClockStr*(): string {.rtl, extern: "nt$1", tags: [TimeEffect].} = proc getClockStr*(dt = now()): string {.rtl, extern: "nt$1", tags: [TimeEffect].} =
## Gets the current local clock time as a string of the format ``HH:MM:SS``. ## Gets the current local clock time as a string of the format ``HH:MM:SS``.
var dt = now() runnableExamples:
echo getClockStr(now() - 1.hours)
result = intToStr(dt.hour, 2) & ':' & intToStr(dt.minute, 2) & result = intToStr(dt.hour, 2) & ':' & intToStr(dt.minute, 2) &
':' & intToStr(dt.second, 2) ':' & intToStr(dt.second, 2)