标准snmp DateTime格式如下所示。
http://net-snmp.sourceforge.net/docs/mibs/host.html#DateAndTime
"2016-10-3,2:15:27.0,-4:0"现在,我试图使用tcl的epoch将此值转换为clock scan。
我认为,这里中用于扫描的格式选项不支持小数秒和时区。
% clock scan $value2 -format {%Y-%m-%d %H:%M:%S}
input string does not match supplied format我已经成功地将值分为日期、时间和timeZone。
% set value "2016-10-3,2:15:27.0,-4:0"
2016-10-3,2:15:27.0,-4:0
% set value [split $value ,]
2016-10-3 2:15:27.0 -4:0
% lassign $value date time timeZone
%在这之后我该怎么做?
发布于 2016-10-04 09:15:59
问题的第一部分是分数秒。此外,时区不是我们可以支持的形式(我们可以做的事情只有这么多;我们专注于使解析器能够处理ISO时间戳格式的公共部分)。
然而,这确实意味着我们可以相当容易地清理东西。这里有几个步骤,我们将使用regexp、scan和format来帮助:
# Your example, in a variable for my convenience
set instant "2016-10-3,2:15:27.0,-4:0"
# Take apart the problem piece; REs are *great* for string parsing!
regexp {^(.+)\.(\d+),(.+)$} $instant -> timepart fraction timezone
# Fix the timezone format; we use [scan] for semantic parsing...
set timezone [format "%+03d%02d" {*}[scan $timezone "%d:%d"]]
# Parse the time properly now that we can understand all the pieces
set timestamp [clock scan "$timepart $timezone" -format "%Y-%m-%d,%k:%M:%S %z"]让我们检查一下,这是否产生了正确的输出类型(这是在一个交互式会话中):
% clock format $timestamp
Mon Oct 03 07:15:27 BST 2016在我看来不错。我想您可以在最后添加原始瞬间的小数部分,但是clock format不喜欢它。
发布于 2016-10-04 08:06:47
您可以这样进行(检查每个步骤的扫描结果:当然,这两个步骤都不是最终结果):
clock scan $date -format %Y-%N-%e
lassign [split $time .] t d
clock scan $t -format %k:%M:%S您将不得不决定如何处理十秒部分(在d中)。
lassign [split $timeZone :] h m ; # or:
scan $timeZone %d:%d h m
clock scan [format {%+03d%02d} $h $m] -format %z确切地说,要使用什么clock字段说明符取决于基本格式:根据需要调整。这些说明符与格式匹配。
要获得最后的时间值:
clock scan "$date $t [format {%+03d%02d} $h $m]" -format "%Y-%N-%e %k:%M:%S %z"文档:钟,格式化,拉什,扫描,拆分
https://stackoverflow.com/questions/39846713
复制相似问题