2016-10-04 69 views
1

標準snmpDateTime格式如下。tcl時鐘掃描snmp日期時間格式

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

"2016-10-3,2:15:27.0,-4:0" 

現在,我嘗試使用TCL的clock scan

的格式選項here掃描不支持小數秒和時區,我認爲這個值轉換爲epoch

% 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 
% 

如何在此之後繼續?

+1

時區可能被支持('%z'和'%Z),但只能使用不同的格式。有多少奇數格式可以支持的限制... –

回答

2

這是一個問題是分數第二的第一部分。此外,時區並非我們可以支持的形式(我們只能做很多事情;我們專注於使解析器能夠處理ISO時間戳格式的常見部分)。

但是,這確實意味着我們可以相當容易地清理事情。那裏有幾個步驟,這一點,我們將使用regexpscanformat的幫助:

# 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不會喜歡它。

2

你可以繼續像這樣(檢查每個步驟的掃描結果:這些都不是,當然最後的結果):

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字段說明使用取決於基本格式:根據需要進行調整。 AFAICT這些說明符符合格式。

爲了得到最終的時間值:

clock scan "$date $t [format {%+03d%02d} $h $m]" -format "%Y-%N-%e %k:%M:%S %z" 

文檔: clockformatlassignscansplit

+0

感謝你們倆。現在我很困惑接受哪一個。 –

+1

請注意,如果時區的小時/分鐘部分可以具有前導零,則應使用掃描來提取它們,如Donal的答案中所述。如果沒有,一個簡單的分裂將起作用。原因是08和09被讀爲(非法)八進制數,但掃描%d指定了十進制。 –

+1

@thiru:好的,我的也是:) –