Skip to content

Instantly share code, notes, and snippets.

@sean-m
Last active April 3, 2019 08:09
Show Gist options
  • Select an option

  • Save sean-m/12e225436215c3358afb to your computer and use it in GitHub Desktop.

Select an option

Save sean-m/12e225436215c3358afb to your computer and use it in GitHub Desktop.
vbscript function for calculating Unix epoch, the universal machine readable timestamp.
Function UnixEpoch
Dim utc_now, t_diff, objWMIService, colItems, item
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
'Get UTC time string
Set colItems = objWMIService.ExecQuery("Select * from Win32_UTCTime")
For Each item In colItems
If Not IsNull(item) Then
utc_now = item.Month & "/" & item.Day & "/" & item.Year & " " _
& item.Hour & ":" & item.Minute & ":" & item.Second
End If
Next
'Get UTC offset, not constant due to daylight savings
t_diff = Abs(DateDiff("h", utc_now, Now()))
'Calculate seconds since start of epoch
UnixEpoch = DateDiff("s", "01/01/1970 00:00:00", DateAdd("h",t_diff,Now()))
End Function
@PaulWebster
Copy link
Copy Markdown

Also ... I just had an odd result on 3rd April 2019 ... in Europe that is 03/04/2019 ... which would be presented in US format as 04/03/2019.
So - I suspect that it would be safer to change line 11 to put in y/m/d format with the hope that VBScript would spot the 4 digit year and the take the next number as month regardless of US/EU date format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment