0

I searched endless hours for this but I was unable to find anything that could help me.

I'm writing a script to calculate the launch-time of a particular instance in EC2 and to return the total running time in seconds. When I run this incomplete script I get the value of 'gettimeoutput' as something like '2015-01-28T01:56:11.000Z' (see reference). I just want this in a proper format so that I can use dateDiff() function to calculate time elapsed till the current moment.

'getltimef' would be the starting time in dateDiff readable format and 'timeinsec' would be the final output (time elapsed). Starttimer() function would start a silent timer that will also keep updating the value of 'timeinsec' variable.

Could someone help me with this, please?

References:

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    

Thank you!

Community
  • 1
  • 1
Panny
  • 1
  • 2

1 Answers1

0

Use a RegExp looking for digits separated by junk and Date/TimeSerial(); as in:

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)"

output:

cscript 28164812.vbs
2015-01-28T01:56:11.000Z Date 28.01.2015 01:56:11 (german locale)
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • Thanks Ekkehard but won't you need to convert this date/time value from UTC to your local computer time zone? Can it be scripted to automatically determine your local computer time zone? – Panny Jan 29 '15 at 15:25