RegEx在VBscript中格式化日期时间

时间:2015-01-28 13:08:41

标签: vbscript amazon-ec2

我为此寻找了无数个小时,但我找不到任何可以帮助我的事情。

我正在编写一个脚本来计算EC2中特定实例的启动时间,并以秒为单位返回总运行时间。当我运行这个不完整的脚本时,我得到' gettimeoutput'就像' 2015-01-28T01:56:11.000Z' (见参考资料)。我只想以适当的格式使用它,以便我可以使用dateDiff()函数来计算到当前时刻所经过的时间。

' getltimef'将是dateDiff可读格式的开始时间和' timeinsec'将是最终输出(经过的时间)。 Starttimer()函数会启动一个静音计时器,它也会不断更新“timeinsec”的值。变量

有人可以帮帮我吗?

参考文献:

1] PHP Parsing Amazon EC2 instance 'launchTime'

2] http://giantdorks.org/alain/how-to-test-age-of-an-aws-instance-in-a-shell-script-2

Function getinfo()

Set instanceid = CreateObject("MSXML2.XMLHTTP.3.0")
Set regiondata = CreateObject("MSXML2.XMLHTTP.3.0")

instanceid.Open "GET", "http://169.254.169.254/latest/meta-data/instance-id", False
instanceid.Send
regiondata.Open "GET", "http://169.254.169.254/latest/meta-data/placement/availability-zone", False
regiondata.Send

Const getltimeFinished = 1
Const getltimeFailed = 2
getltimeCommand = "aws ec2 --region " regiondata.responseText " describe-instances " instanceid.responseText "--filter=""launch-time"""

Set getltimeShell = CreateObject("WScript.Shell")
Set getltimeShellExec = getltimeShell.Exec(getltimeCommand)

Select Case getltimeShellExec.Status

Case getltimeFinished
   launchtimedetermined = true
   getltimeOutput = getltimeShellExec.StdOut.ReadAll

Case getltimeFailed
   launchtimedetermined = false

End Select

if (launchtimedetermined) = false Then
timeinsec = 240
Else

'getltimef script'
'getltimef = maybe regexp can work here?'
'timeinsec = datediff("s", getltimef, now())'


Call starttimer()

End Function    

谢谢!

1 个答案:

答案 0 :(得分:0)

使用RegExp查找以垃圾和Date / TimeSerial()分隔的数字;如:

s = "2015-01-28T01:56:11.000Z"
Set r = New RegExp
r.Pattern = "(\d+).(\d+).(\d+).(\d+).(\d+).(\d+)"
Set m = r.Execute(s)(0)
d = DateSerial(CInt(m.Submatches(0)), CInt(m.Submatches(1)), CInt(m.Submatches(2))) + TimeSerial(CInt(m.Submatches(3)), CInt(m.Submatches(4)), CInt(m.Submatches(5)))
WScript.Echo s, TypeName(d), d, "(german locale)"

输出:

cscript 28164812.vbs
2015-01-28T01:56:11.000Z Date 28.01.2015 01:56:11 (german locale)