Skip to content

Commit

Permalink
add RfcUtcTime compilation option (#152)
Browse files Browse the repository at this point in the history
* log_output: use of getFastDateTimeString with RfcUtcTime format setting
  • Loading branch information
Ivansete-status authored Sep 19, 2024
1 parent a28bb97 commit 1ac2715
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions chronicles/log_output.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 3 additions & 1 deletion chronicles/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ type
TimestampsScheme* = enum
NoTimestamps,
UnixTime,
RfcTime
RfcTime,
RfcUtcTime

ColorScheme* = enum
NoColors,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1ac2715

Please sign in to comment.