Skip to content

Commit a99e618

Browse files
committed
Remove override and update specs
1 parent c48937a commit a99e618

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

UtcConverter.bas

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Attribute VB_Name = "UtcConverter"
22
''
3-
' VBA-UTC v0.0.0
3+
' VBA-UTC v0.5.0
44
' (c) Tim Hall - https://github.com/VBA-tools/VBA-UtcConverter
55
'
66
' UTC/ISO 8601 Converter for VBA
@@ -46,9 +46,6 @@ Private Type utc_TIME_ZONE_INFORMATION
4646
End Type
4747
#End If
4848

49-
Public utc_UtcOffsetMinutes As Long
50-
Public utc_Override As Boolean
51-
5249
' ============================================= '
5350
' Public Methods
5451
' ============================================= '
@@ -60,9 +57,6 @@ Public utc_Override As Boolean
6057
' @return {Date} Local date
6158
' -------------------------------------- '
6259
Public Function ParseUtc(utc_UtcDate As Date) As Date
63-
If utc_Override Then
64-
ParseUtc = utc_UtcDate + VBA.TimeSerial(0, utc_UtcOffsetMinutes, 0)
65-
Else
6660
#If Mac Then
6761
' TODO
6862
#Else
@@ -74,7 +68,6 @@ Public Function ParseUtc(utc_UtcDate As Date) As Date
7468

7569
ParseUtc = SystemTimeToDate(utc_LocalDate)
7670
#End If
77-
End If
7871
End Function
7972

8073
''
@@ -84,9 +77,6 @@ End Function
8477
' @return {Date} UTC date
8578
' -------------------------------------- '
8679
Public Function ConvertToUtc(utc_LocalDate As Date) As Date
87-
If utc_Override Then
88-
ConvertToUtc = utc_LocalDate - VBA.TimeSerial(0, utc_UtcOffsetMinutes, 0)
89-
Else
9080
#If Mac Then
9181
' TODO
9282
#Else
@@ -98,7 +88,6 @@ Public Function ConvertToUtc(utc_LocalDate As Date) As Date
9888

9989
ConvertToUtc = SystemTimeToDate(utc_UtcDate)
10090
#End If
101-
End If
10291
End Function
10392

10493
''

specs/Specs.bas

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
Attribute VB_Name = "Specs"
2+
Private pOffsetMinutes As Long
3+
Private pOffsetLoaded As Boolean
4+
Public Property Get OffSetMinutes() As Long
5+
If Not pOffsetLoaded Then
6+
Dim InputValue As String
7+
InputValue = VBA.InputBox("Enter UTC Offset (in minutes)" & vbNewLine & vbNewLine & _
8+
"Example:" & vbNewLine & _
9+
"EST (UTC-5:00) and DST (+1:00)" & vbNewLine & _
10+
"= UTC-4:00" & vbNewLine & _
11+
"= -240", "Enter UTC Offset", 0)
12+
13+
If InputValue <> "" Then: pOffsetMinutes = CLng(InputValue)
14+
15+
pOffsetLoaded = True
16+
End If
17+
18+
OffSetMinutes = pOffsetMinutes
19+
End Property
20+
Public Property Let OffSetMinutes(Value As Long)
21+
pOffsetMinutes = Value
22+
pOffsetLoaded = True
23+
End Property
24+
225
Public Function Specs() As SpecSuite
326
Set Specs = New SpecSuite
427
Specs.Description = "VBA-UTC"
528

6-
' Override UTC Offset for specs
7-
UtcConverter.utc_Override = True
8-
UtcConverter.utc_UtcOffsetMinutes = -4 * 60
9-
1029
Dim LocalDate As Date
1130
Dim LocalIso As String
1231
Dim UtcDate As Date
@@ -17,47 +36,49 @@ Public Function Specs() As SpecSuite
1736
LocalIso = "2004-05-06T19:08:09.000Z"
1837

1938
' May 6, 2004 11:08:09 PM
20-
UtcDate = 38113.9639930556
21-
UtcIso = "2004-05-06T23:08:09.000Z"
39+
UtcDate = LocalDate - OffSetMinutes / 60 / 24
40+
UtcIso = VBA.Format$(UtcDate, "yyyy-mm-ddTHH:mm:ss.000Z")
2241

2342
' ============================================= '
2443
' ParseUTC
2544
' ============================================= '
2645
With Specs.It("should parse UTC")
27-
.Expect(VBA.Format$(UtcConverter.ParseUtc(UtcDate), "yyyy-mm-ddTHH:mm:ss.000Z")).ToEqual LocalIso
46+
.Expect(DateToString(UtcConverter.ParseUtc(UtcDate))).ToEqual DateToString(LocalDate)
2847
End With
2948

3049
' ============================================= '
3150
' ConvertToUTC
3251
' ============================================= '
3352
With Specs.It("should convert to UTC")
34-
.Expect(VBA.Format$(UtcConverter.ConvertToUtc(LocalDate), "yyyy-mm-ddTHH:mm:ss.000Z")).ToEqual UtcIso
53+
.Expect(DateToString(UtcConverter.ConvertToUtc(LocalDate))).ToEqual DateToString(UtcDate)
3554
End With
3655

3756
' ============================================= '
3857
' ParseISO
3958
' ============================================= '
4059
With Specs.It("should parse ISO 8601")
41-
.Expect(VBA.Format$(UtcConverter.ParseIso(UtcIso), "yyyy-mm-ddTHH:mm:ss.000Z")).ToEqual LocalIso
60+
.Expect(DateToString(UtcConverter.ParseIso(UtcIso))).ToEqual "2004-05-06T19:08:09"
4261
End With
4362

4463
With Specs.It("should parse ISO 8601 with offset")
45-
.Expect(VBA.Format$(UtcConverter.ParseIso("2004-05-06T12:08:09+04:05:06"), "yyyy-mm-ddTHH:mm:ss")).ToEqual "2004-05-06T16:13:15"
46-
.Expect(VBA.Format$(UtcConverter.ParseIso("2004-05-06T12:08:09-04:05:06"), "yyyy-mm-ddTHH:mm:ss")).ToEqual "2004-05-06T08:03:03"
64+
.Expect(DateToString(UtcConverter.ParseIso("2004-05-06T12:08:09+04:05:06"))).ToEqual "2004-05-06T16:13:15"
65+
.Expect(DateToString(UtcConverter.ParseIso("2004-05-06T12:08:09-04:05:06"))).ToEqual "2004-05-06T08:03:03"
4766
End With
4867

4968
With Specs.It("should parse ISO 8601 with varying time format")
50-
.Expect(VBA.Format$(UtcConverter.ParseIso("2004-05-06T12+04"), "yyyy-mm-ddTHH:mm:ss")).ToEqual "2004-05-06T16:00:00"
51-
.Expect(VBA.Format$(UtcConverter.ParseIso("2004-05-06T12:08+04:05"), "yyyy-mm-ddTHH:mm:ss")).ToEqual "2004-05-06T16:13:00"
52-
.Expect(VBA.Format$(UtcConverter.ParseIso("2004-05-06T12Z"), "yyyy-mm-ddTHH:mm:ss")).ToEqual "2004-05-06T08:00:00"
53-
.Expect(VBA.Format$(UtcConverter.ParseIso("2004-05-06T12:08Z"), "yyyy-mm-ddTHH:mm:ss")).ToEqual "2004-05-06T08:08:00"
69+
.Expect(DateToString(UtcConverter.ParseIso("2004-05-06T12+04"))).ToEqual "2004-05-06T16:00:00"
70+
.Expect(DateToString(UtcConverter.ParseIso("2004-05-06T12:08+04:05"))).ToEqual "2004-05-06T16:13:00"
71+
.Expect(DateToString(UtcConverter.ParseIso("2004-05-06T12Z"))).ToEqual _
72+
DateToString(DateSerial(2004, 5, 6) + TimeSerial(12, 0, 0) + OffSetMinutes / 60 / 24)
73+
.Expect(DateToString(UtcConverter.ParseIso("2004-05-06T12:08Z"))).ToEqual _
74+
DateToString(DateSerial(2004, 5, 6) + TimeSerial(12, 8, 0) + OffSetMinutes / 60 / 24)
5475
End With
5576

5677
' ============================================= '
5778
' ConvertToISO
5879
' ============================================= '
5980
With Specs.It("should convert to ISO 8601")
60-
.Expect(VBA.Format$(UtcConverter.ConvertToIso(LocalDate), "yyyy-mm-ddTHH:mm:ss.000Z")).ToEqual UtcIso
81+
.Expect(UtcConverter.ConvertToIso(LocalDate)).ToEqual UtcIso
6182
End With
6283

6384
' ============================================= '
@@ -77,3 +98,7 @@ Public Sub RunSpecs()
7798

7899
DisplayRunner.RunSuite Specs
79100
End Sub
101+
102+
Private Function DateToString(Value As Date) As String
103+
DateToString = VBA.Format$(Value, "yyyy-mm-ddTHH:mm:ss")
104+
End Function

specs/VBA-UTC - Specs.xlsm

-12.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)