Last active
April 3, 2019 08:09
-
-
Save sean-m/12e225436215c3358afb to your computer and use it in GitHub Desktop.
vbscript function for calculating Unix epoch, the universal machine readable timestamp.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
FYI - there is one place that use an offset that is not in whole hours ... from https://www.timeanddate.com/time/dst/
"Not Always One Hour
Today clocks are almost always set one hour back or ahead.
However, on Lord Howe Island, Australia, clocks are set only 30 minutes forward from LHST (UTC+10:30) to LHDT (UTC+11) during DST.
30 and 45 minute time zones
Throughout history, there have been several variations, like half adjustments (30 minutes) or double adjustment (2 hours). Adjustments of 20 and 40 minutes have also been used."
So ... probably better to use "s" in lines 17 and 20