首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tcl时钟扫描snmp日期时间格式

tcl时钟扫描snmp日期时间格式
EN

Stack Overflow用户
提问于 2016-10-04 07:37:15
回答 2查看 1.3K关注 0票数 1

标准snmp DateTime格式如下所示。

http://net-snmp.sourceforge.net/docs/mibs/host.html#DateAndTime

代码语言:javascript
复制
"2016-10-3,2:15:27.0,-4:0"

现在,我试图使用tcl的epoch将此值转换为clock scan

我认为,这里中用于扫描的格式选项不支持小数秒和时区。

代码语言:javascript
复制
% clock scan $value2 -format {%Y-%m-%d %H:%M:%S}
input string does not match supplied format

我已经成功地将值分为日期、时间和timeZone。

代码语言:javascript
复制
% 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
%

在这之后我该怎么做?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-04 09:15:59

问题的第一部分是分数秒。此外,时区不是我们可以支持的形式(我们可以做的事情只有这么多;我们专注于使解析器能够处理ISO时间戳格式的公共部分)。

然而,这确实意味着我们可以相当容易地清理东西。这里有几个步骤,我们将使用regexpscanformat来帮助:

代码语言:javascript
复制
# 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"]

让我们检查一下,这是否产生了正确的输出类型(这是在一个交互式会话中):

代码语言:javascript
复制
% clock format $timestamp
Mon Oct 03 07:15:27 BST 2016

在我看来不错。我想您可以在最后添加原始瞬间的小数部分,但是clock format不喜欢它。

票数 2
EN

Stack Overflow用户

发布于 2016-10-04 08:06:47

您可以这样进行(检查每个步骤的扫描结果:当然,这两个步骤都不是最终结果):

代码语言:javascript
复制
clock scan $date -format %Y-%N-%e

lassign [split $time .] t d
clock scan $t -format %k:%M:%S

您将不得不决定如何处理十秒部分(在d中)。

代码语言:javascript
复制
lassign [split $timeZone :] h m ; # or:
scan $timeZone %d:%d h m
clock scan [format {%+03d%02d} $h $m] -format %z

确切地说,要使用什么clock字段说明符取决于基本格式:根据需要调整。这些说明符与格式匹配。

要获得最后的时间值:

代码语言:javascript
复制
clock scan "$date $t [format {%+03d%02d} $h $m]" -format "%Y-%N-%e %k:%M:%S %z"

文档:格式化拉什扫描拆分

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39846713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档