From 1ac2715bbbb1f2931ce2d32fe05b70f8d47fe413 Mon Sep 17 00:00:00 2001 From: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> Date: Thu, 19 Sep 2024 15:56:36 +0200 Subject: [PATCH] add RfcUtcTime compilation option (#152) * log_output: use of getFastDateTimeString with RfcUtcTime format setting --- README.md | 7 +++++++ chronicles/log_output.nim | 22 ++++++++++++++-------- chronicles/options.nim | 4 +++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 01afe3a..564e5b9 100644 --- a/README.md +++ b/README.md @@ -385,6 +385,13 @@ Possible values are: https://tools.ietf.org/html/rfc3339 +- `RfcUtcTime` + + Chronicles will use the UTC but in human-readable format specified in + RFC 3339: Date and Time on the Internet: Timestamps + + https://tools.ietf.org/html/rfc3339 + - `UnixTime` Chronicles will write a single float value for the number diff --git a/chronicles/log_output.nim b/chronicles/log_output.nim index ae7b6cc..1a9d63c 100644 --- a/chronicles/log_output.nim +++ b/chronicles/log_output.nim @@ -454,29 +454,35 @@ proc getSecondsPart(timestamp: Time): string = res[5] = chr(ord('0') + (tmp mod 10)) res -proc getFastDateTimeString(): string = +proc getFastDateTimeString(useUtc: bool): string = + ## if useUtc is true, utc time is used and Z for the timezone part. + ## if useUtc is false, the local time will be used and the time zone will be obtained each time. let timestamp = getFastTime() minutes = timestamp.toUnix() div 60 if minutes != cachedMinutes: cachedMinutes = minutes - let datetime = timestamp.local() + let datetime = if useUtc: timestamp.utc() else: timestamp.local() block: # Cache string representation of first part (without seconds) let tmp = datetime.format("yyyy-MM-dd HH:mm:") cachedTimeArray = toArray(17, tmp.toOpenArrayByte(0, 16)) block: - # Cache string representation of zone part - let tmp = datetime.format("zzz") - cachedZoneArray = toArray(6, tmp.toOpenArrayByte(0, 5)) + if not useUtc: + # Cache string representation of zone part + let tmp = datetime.format("zzz") + cachedZoneArray = toArray(6, tmp.toOpenArrayByte(0, 5)) - string.fromBytes(cachedTimeArray) & timestamp.getSecondsPart() & - string.fromBytes(cachedZoneArray) + let timeZone = if useUtc: "Z" else: string.fromBytes(cachedZoneArray) + + string.fromBytes(cachedTimeArray) & timestamp.getSecondsPart() & timeZone template timestamp(record): string = when record.timestamps == RfcTime: - getFastDateTimeString() + getFastDateTimeString(useUtc = false) + elif record.timestamps == RfcUtcTime: + getFastDateTimeString(useUtc = true) else: epochTimestamp() diff --git a/chronicles/options.nim b/chronicles/options.nim index f75dad4..a0fcc77 100644 --- a/chronicles/options.nim +++ b/chronicles/options.nim @@ -73,7 +73,8 @@ type TimestampsScheme* = enum NoTimestamps, UnixTime, - RfcTime + RfcTime, + RfcUtcTime ColorScheme* = enum NoColors, @@ -257,6 +258,7 @@ proc sinkSpecsFromNode*(streamNode: NimNode): seq[SinkSpec] = of "notimestamps": setTimestamps(NoTimestamps) of "unixtime": setTimestamps(UnixTime) of "rfctime": setTimestamps(RfcTime) + of "rfcutctime": setTimestamps(RfcUtcTime) else: discard let dst = logDestinationFromNode(dstSpec)