Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import strutils, times, re
- #------------------------------------------------------------------------------
- # nowDateTime gets current time/date as tuple with following fields:
- # .year .year2digit .month .mday .hour .min .sec .milli .micro .nano
- #------------------------------------------------------------------------------
- proc nowDateTime(): tuple =
- let
- startFloat = epochTime()
- startStr = startFloat.formatFloat(precision = 32)
- subSecStr = startStr.split(".")[^1]
- ms = subSecStr[0 .. 2]
- mics = subSecStr[0 .. 5]
- ns = subSecStr[0 .. 8]
- datetime = $now()
- var aDt: array[7, string]
- if datetime.find(re"(\d\d)(\d\d)-(\d+)-(\d+)T(\d+):(\d+):(\d+).+", aDt) != -1:
- let dt: tuple[year, year2digit, month, mday, hour, min, sec, milli, micro, nano: string] =
- (aDt[0] & aDt[1], aDt[1], aDt[2], aDt[3], aDt[4], aDt[5], aDt[6], ms, mics, ns)
- return dt
- else:
- return ("", "", "", "", "", "", "", "", "", "")
- #------------------------------------------------------------------------------
- #------------------------------------------------------------------------------
- # timeStamp Gets the time stamp for the current date and/or time.
- # The date can be userformatted by given format string, default: YYYY-MM-dd. Usable shorts:
- # "YYYY"-4 digit year, "YY"-2 digit year, "MM"- month, "dd"- day of month
- # The time can get more detailed with "partSeconds", by default none parts: hh:mm:ss.
- # Parts are: "milli", "micro", "nano"
- # Note: You can use backslashes in "formDate" as raw string, it will output in the same way (i.e. r"MM\YY" --> "03\19")
- #------------------------------------------------------------------------------
- proc timeStamp(partSeconds: string = "", onlyTime: bool = false, formDate: string = "YYYY-MM-dd"): string =
- let dt = nowDateTime()
- var aForm: array[7, string] = [dt.year, dt.month, dt.mday, dt.hour, dt.min, dt.sec, ""]
- var aSep: array[1, string]
- var frmDate: string = formDate
- if frmDate.find(re"\\[^\\]") != -1: frmDate = frmDate.replace(re"\\", r"\\")
- if frmDate.find(re"(\W)", aSep) != -1:
- let sPart = frmDate.split(aSep[0])
- for part in sPart:
- case part:
- of "YY":
- aForm[0] = dt.year2digit
- else:
- discard
- frmDate = frmDate.replace(re"YYYY", "$1").replace(re"YY", "$1").replace(re"MM", "$2").replace(re"dd", "$3")
- if frmDate.find(re"\\\\") != -1: frmDate = frmDate.replace(re"\\\\", r"\")
- var formStr = frmDate & " $4:$5:$6"
- if onlyTime: formStr = "$4:$5:$6"
- if partSeconds != "":
- formStr.add(".$7")
- case partSeconds:
- of "milli":
- aForm[6] = dt.milli
- of "micro":
- aForm[6] = dt.micro
- of "nano":
- aForm[6] = dt.nano
- else:
- discard
- return formStr % aForm
- else:
- return ""
- #------------------------------------------------------------------------------
- echo "Date and Time (default): ", timeStamp()
- echo r"Date formatted dd\M): ", timeStamp(formDate = r"dd\MM")
- echo "Date formatted [dd.MM.YYYY]: ", timeStamp(formDate = "[dd.MM.YYYY]")
- echo "Time only, part: miliseconds ", timeStamp("milli", true)
- echo "Time only, part: microseconds ", timeStamp("micro", true)
- echo "Time only, part: nanoseconds ", timeStamp("nano", true)
- #[ output:
- Date and Time (default): 2019-03-19 17:46:01
- Date formatted dd\M): 19\03 17:46:01
- Date formatted [dd.MM.YYYY]: [19.03.2019] 17:46:01
- Time only, part: miliseconds 17:46:01.827
- Time only, part: microseconds 17:46:01.828702
- Time only, part: nanoseconds 17:46:01.828702700
- ]#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement