-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure log time, if not parsed from log, use collection time (#54)
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function ensure_time(_, timestamp, record) | ||
local timeField = "time" | ||
|
||
-- 如果 record 中已经有了 time 字段,则不做处理 | ||
if record[timeField] then | ||
return 0, timestamp, record | ||
end | ||
|
||
-- 将秒级时间戳转换为可用于 os.date 函数的格式 | ||
local sec = math.floor(timestamp) | ||
local nsec = math.floor((timestamp - sec) * 1e9) | ||
|
||
-- 将时间戳转换为指定格式的日期字符串 | ||
local zone = os.date("%z", timestamp) | ||
zone = tostring(zone) | ||
if string.len(zone) > 0 then | ||
-- add `:` to zone, change +0800 to +08:00 | ||
zone = tostring(zone):gsub("^(%+)(%d%d)(%d%d)$", "%1%2:%3") | ||
else | ||
zone = "Z" | ||
end | ||
|
||
-- 生成日期字符串 | ||
local dateStr = os.date("%Y-%m-%dT%H:%M:%S", sec) .. string.format(".%09d", nsec) .. zone | ||
record[timeField] = dateStr | ||
|
||
return 2, timestamp, record | ||
end |