Skip to content

Commit

Permalink
Pass by ref so we don't need to build more memory use. Optimize Offse…
Browse files Browse the repository at this point in the history
…t string building to only do math when it's required and fix whitespace.
  • Loading branch information
hecon5 committed Nov 15, 2023
1 parent 26bf030 commit 1961800
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions Version Control.accda.src/modules/modUtcConverter.bas
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ Private Function utc_ConvertDate(utc_Value As Double _
End If
End Function


Private Function utc_ExecuteInShell(utc_ShellCommand As String) As utc_ShellResult
#If VBA7 Then
' 64bit Mac
Expand Down Expand Up @@ -677,8 +678,10 @@ End Function
#Else
' Windows


' Pass in a date, this will return a Windows SystemTime structure with millisecond accuracy.
Private Function utc_DateToSystemTime(ByRef utc_Value As Date) As utc_SYSTEMTIME ' "Helper Functions

With utc_DateToSystemTime
.utc_wYear = VBA.Year(utc_Value)
.utc_wMonth = VBA.Month(utc_Value)
Expand All @@ -692,17 +695,21 @@ Private Function utc_DateToSystemTime(ByRef utc_Value As Date) As utc_SYSTEMTIME
.utc_wSecond = VBA.Second(utc_Value)
End If
End With

End Function


Private Function utc_SystemTimeToDate(ByRef utc_Value As utc_SYSTEMTIME) As Date ' "Helper Function" for Public Functions (below)
Private Function utc_SystemTimeToDate(ByRef utc_Value As utc_SYSTEMTIME) As Date
' "Helper Function" for Public Functions (below)

utc_SystemTimeToDate = DateSerial(utc_Value.utc_wYear _
, utc_Value.utc_wMonth _
, utc_Value.utc_wDay) + _
TimeSerialDbl(utc_Value.utc_wHour _
, utc_Value.utc_wMinute _
, utc_Value.utc_wSecond _
, utc_Value.utc_wMilliseconds)

End Function


Expand Down Expand Up @@ -872,20 +879,25 @@ Public Function TimeSerialDbl(ByVal HoursIn As Double _
, ByVal MinutesIn As Double _
, ByVal SecondsIn As Double _
, Optional ByVal MillisecondsIn As Double = 0) As Double

Dim tMS As Double
Dim tSec As Double
Dim tSecTemp As Double

tSec = VBA.CDbl(RoundDown(SecondsIn))
tSecTemp = SecondsIn - tSec
tMS = (tSecTemp * (TotalMillisecondsInDay / TotalSecondsInDay)) \ 1
tMS = tMS + MillisecondsIn
If (tSecTemp > 0.5) Then tSec = tSec - 1
If tMS = 500 Then tMS = tMS - 0.001 ' Shave a hair, because otherwise it'll round up too much.
TimeSerialDbl = (HoursIn / TotalHoursInDay) + (MinutesIn / TotalMinutesInDay) + CDbl((tSec / TotalSecondsInDay)) + (tMS / TotalMillisecondsInDay)

End Function


' If given a time double, will return the millisecond portion of the time.
Private Function GetMilliseconds(ByVal TimeIn As Double) As Variant
Private Function GetMilliseconds(ByRef TimeIn As Date) As Variant

Dim IntDatePart As Long
Dim DblTimePart As Double
Dim LngSeconds As Long ' Used to remove whole seconds.
Expand All @@ -907,6 +919,7 @@ Private Function GetMilliseconds(ByVal TimeIn As Double) As Variant
MSCount = ((DblMS * (TotalMillisecondsInDay))) \ 1
If MSCount >= 1000 Then MSCount = 0
GetMilliseconds = MSCount

End Function


Expand Down Expand Up @@ -942,13 +955,15 @@ Public Function CurrentLocalBiasFromUTC(Optional ByVal OutputAsHours As Boolean

End Function


Public Function CurrentISOTimezoneOffset() As String
CurrentISOTimezoneOffset = ISOTimezoneOffset(CurrentLocalBiasFromUTC)
End Function


Public Function GetBiasForGivenLocalDate(ByVal LocalDateIn As Date _
Public Function GetBiasForGivenLocalDate(ByRef LocalDateIn As Date _
, Optional ByVal OutputAsHours As Boolean = False) As Long

Dim DateUTCNow As Date

DateUTCNow = ConvertToUtc(LocalDateIn)
Expand All @@ -961,40 +976,44 @@ Public Function GetBiasForGivenLocalDate(ByVal LocalDateIn As Date _
Else
GetBiasForGivenLocalDate = VBA.DateDiff("h", LocalDateIn, DateUTCNow)
End If

End Function


Public Function ISOTimezoneOffsetOnDate(ByVal LocalDateIn As Date) As String
ISOTimezoneOffsetOnDate = ISOTimezoneOffset(GetBiasForGivenLocalDate(LocalDateIn))
End Function


' Provides the ISO Offset time from an input (or current offset if none is passed in) to build an ISO8601 output String
'
Private Function ISOTimezoneOffset(Optional TimeBias As Long = 0) As String

Dim strOffsetOut As String

Dim tString_Buffer As StringBufferCache

Dim OffsetLong As Long
Dim hourOffset As Long
Dim minOffset As Long

' Counterintuitively, the Bias is postive (time ahead), the offset is the negative value of bias.
OffsetLong = TimeBias * -1

hourOffset = OffsetLong \ 60
minOffset = OffsetLong Mod 60
If TimeBias = 0 Then

If OffsetLong = 0 Then
ISOTimezoneOffset = ISO8601UTCTimeZone
Else
If OffsetLong > 0 Then String_BufferAppend tString_Buffer, "+"
String_BufferAppend tString_Buffer, VBA.CStr(VBA.Format(hourOffset, "00"))
String_BufferAppend tString_Buffer, ISO8601TimeDelimiter
String_BufferAppend tString_Buffer, VBA.CStr(VBA.Format(minOffset, "00"))

ISOTimezoneOffset = String_BufferToString(tString_Buffer)
Else
' Counterintuitively, the Bias is postive (time ahead),
' and the offset is the negative value of bias.
OffsetLong = TimeBias * -1
hourOffset = OffsetLong \ 60
minOffset = OffsetLong Mod 60

With New clsConcat
If OffsetLong > 0 Then .Add "+"
.Add VBA.CStr(VBA.Format(hourOffset, "00"))
.Add ISO8601TimeDelimiter
.Add VBA.CStr(VBA.Format(minOffset, "00"))

ISOTimezoneOffset = .GetStr
End With
End If

End Function


Expand Down

0 comments on commit 1961800

Please sign in to comment.