Last active
February 26, 2021 20:45
-
-
Save seakintruth/ddcc3d5e400a5083458494ae30d55466 to your computer and use it in GitHub Desktop.
Visual Basic Epoch Time Handling
This file contains 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
'Notes: Visual Basic (VBScript, or VBA) to handle UNIX like Epoch Timestamps with timezone corrections to UTC | |
' TimeStampEpoch, and date2epoch functions return epoch as fracional decimal values in seconds since epoch | |
'Maintained at:https://gist.github.com/seakintruth/ddcc3d5e400a5083458494ae30d55466 | |
'Dependencies: Windows with "WbemScripting.SWbemDateTime", a default 32bit app | |
'Contributing: Jeremy D. Gerdes<seakintruth@gmail.com>, https://stackoverflow.com/a/22842128 | |
'License: CC-BY-SA 3.0 | |
'Version: 1.0.6 | |
Function TimeStampEpoch() | |
'Returns a time stamp in the format of seconds since epoch | |
' Use this when you need epoch values, from testing, calling Timer() is roughly 10 times faster than calling TimeStampEpoch | |
' so the returned time stamp is going to be ~0.0048 seconds slow for a ~3 ghz processor. | |
' We don't fudge the numbers here because we don't know how fast the machine is... | |
' For precise timers in VBA we can use Win API calls like https://bytecomb.com/accurate-performance-timers-in-vba/ | |
Dim dblSecondFractional | |
Dim dblNowTimer: dblNowTimer = Timer | |
dblSecondFractional = dblNowTimer - Int(dblNowTimer) | |
TimeStampEpoch = date2epoch(Format(Now(), "yyyy-mm-dd hh:mm:ss") & Right(dblSecondFractional, Len(dblSecondFractional) - 1)) | |
End Function | |
Function date2epoch(myDate) | |
'date2epoch function accept human readable time and returns machines current timezone from an epoch value | |
'expects a normal string date format, with or without the milliseconds, something like "yyyy-mm-dd hh:mm:ss.ms" | |
Dim objDateTime: Set objDateTime = CreateObject("WbemScripting.SWbemDateTime") | |
Dim arrySecondFraction: arrySecondFraction = Split(myDate, ".") | |
If UBound(arrySecondFraction) Then | |
objDateTime.SetVarDate arrySecondFraction(0) 'date portion | |
date2epoch = CDbl(DateDiff("s", "01/01/1970 00:00:00", CDate(objDateTime.GetVarDate(False))) + CDbl(0 & "." & arrySecondFraction(1))) | |
Else | |
objDateTime.SetVarDate myDate | |
date2epoch = DateDiff("s", "01/01/1970 00:00:00", CDate(objDateTime.GetVarDate(False))) | |
End If | |
'Cleanup | |
Set objDateTime = Nothing | |
End Function | |
Function epoch2date(myEpoch) | |
'Accepts negative values for seconds prior to epoch | |
Dim objDateTime: Set objDateTime = CreateObject("WbemScripting.SWbemDateTime") | |
objDateTime.SetVarDate DateAdd("s", myEpoch, "01/01/1970 00:00:00"), False | |
Dim strTmpReturn | |
strTmpReturn = Format(objDateTime.GetVarDate(True), "yyyy-mm-dd hh:mm:ss") | |
Dim arrySecondFraction: arrySecondFraction = Split(myEpoch, ".") | |
If UBound(arrySecondFraction) Then | |
Dim dblSecondFractional: dblSecondFractional = (myEpoch - Int(myEpoch)) | |
epoch2date = strTmpReturn & Right(dblSecondFractional, Len(dblSecondFractional) - 1) | |
Else | |
epoch2date = strTmpReturn | |
End If | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment