Skip to content

Commit

Permalink
-fix date formatting and add tests (#434)
Browse files Browse the repository at this point in the history
* -fix date formatting and add tests

* -fix broken test

* -update names of timestamps to be more appropriate

* -fix timezone representation for GMT timezone

* -cleaner implementation of one of the date formatters

* -update test

* -try to fix test again

* -fix tests for real

* -trying to fix tests again

* -last fix

* -Z

* -done
  • Loading branch information
sbenedicadb authored Nov 2, 2020
1 parent 15a6cd4 commit 2dfd929
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 19 deletions.
6 changes: 5 additions & 1 deletion AEPCore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
24543A1624E1DC95002D8D9A /* MockUnzipper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2467E43C24CB54B70022F6BE /* MockUnzipper.swift */; };
2467E43A24CA4DE20022F6BE /* Unzipping.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2467E43924CA4DE20022F6BE /* Unzipping.swift */; };
247FBD7D24E331A600FA6505 /* Event+SignalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 247FBD7C24E331A600FA6505 /* Event+SignalTests.swift */; };
249498E2254A0C920045E392 /* Date+FormatTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 249498E0254A0C910045E392 /* Date+FormatTests.swift */; };
24B4935824D4C31100AA38D9 /* AEPSignal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 24B4934F24D4C31100AA38D9 /* AEPSignal.framework */; };
24B4935D24D4C31100AA38D9 /* SignalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24B4935C24D4C31100AA38D9 /* SignalTests.swift */; };
24B4935F24D4C31100AA38D9 /* AEPSignal.h in Headers */ = {isa = PBXBuildFile; fileRef = 24B4935124D4C31100AA38D9 /* AEPSignal.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -591,6 +592,7 @@
2467E43B24CB54B70022F6BE /* MockDiskCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MockDiskCache.swift; path = AEPServices/Mocks/MockDiskCache.swift; sourceTree = SOURCE_ROOT; };
2467E43C24CB54B70022F6BE /* MockUnzipper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MockUnzipper.swift; path = AEPServices/Mocks/MockUnzipper.swift; sourceTree = SOURCE_ROOT; };
247FBD7C24E331A600FA6505 /* Event+SignalTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Event+SignalTests.swift"; sourceTree = "<group>"; };
249498E0254A0C910045E392 /* Date+FormatTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+FormatTests.swift"; sourceTree = "<group>"; };
2499461B24E5E67700D3F7B2 /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; };
24B4934F24D4C31100AA38D9 /* AEPSignal.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AEPSignal.framework; sourceTree = BUILT_PRODUCTS_DIR; };
24B4935124D4C31100AA38D9 /* AEPSignal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AEPSignal.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1349,12 +1351,12 @@
3F0397E724BE60910019F095 /* unzip */,
3F0397F124BE60910019F095 /* AnyCodable.swift */,
3F0397E624BE60910019F095 /* AtomicCounter.swift */,
BB00E26A24D8C9A600C578C1 /* Date+Format.swift */,
3F0397EE24BE60910019F095 /* OperationOrderer.swift */,
2107F02D24C9FF88002935CF /* SHA256.swift */,
3F0397ED24BE60910019F095 /* ThreadSafeArray.swift */,
3F0397EF24BE60910019F095 /* ThreadSafeDictionary.swift */,
3F0397F024BE60910019F095 /* URLEncoder.swift */,
BB00E26A24D8C9A600C578C1 /* Date+Format.swift */,
);
path = utility;
sourceTree = "<group>";
Expand Down Expand Up @@ -1408,6 +1410,7 @@
isa = PBXGroup;
children = (
3F03981124BE61520019F095 /* AnyCodableTests.swift */,
249498E0254A0C910045E392 /* Date+FormatTests.swift */,
3F03980F24BE61520019F095 /* OperationOrdererTests.swift */,
3F03981024BE61520019F095 /* PersistentHitQueueTests.swift */,
2107F02F24C9FFB2002935CF /* SHA256Tests.swift */,
Expand Down Expand Up @@ -2585,6 +2588,7 @@
buildActionMask = 2147483647;
files = (
3F03981624BE61520019F095 /* SystemInfoServiceTest.swift in Sources */,
249498E2254A0C920045E392 /* Date+FormatTests.swift in Sources */,
3F03981E24BE61520019F095 /* DataQueueTests.swift in Sources */,
3F03981524BE61520019F095 /* UnzipperTest.swift in Sources */,
3F03982424BE61520019F095 /* AnyCodableTests.swift in Sources */,
Expand Down
8 changes: 4 additions & 4 deletions AEPCore/Sources/rules/TokenFinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class TokenFinder: Traversable {
private let TOKEN_KEY_EVENT_TYPE = "~type"
private let TOKEN_KEY_EVENT_SOURCE = "~source"
private let TOKEN_KEY_TIMESTAMP_UNIX = "~timestampu"
private let TOKEN_KEY_TIMESTAMP_ISO8601 = "~timestampz"
private let TOKEN_KEY_TIMESTAMP_PLATFORM = "~timestampp"
private let TOKEN_KEY_TIMESTAMP_ISO8601_NO_COLON = "~timestampz"
private let TOKEN_KEY_TIMESTAMP_ISO8601 = "~timestampp"
private let TOKEN_KEY_SDK_VERSION = "~sdkver"
private let TOKEN_KEY_CACHEBUST = "~cachebust"
private let TOKEN_KEY_ALL_URL = "~all_url"
Expand Down Expand Up @@ -65,9 +65,9 @@ class TokenFinder: Traversable {
return event.source
case TOKEN_KEY_TIMESTAMP_UNIX:
return now.getUnixTimeInSeconds()
case TOKEN_KEY_TIMESTAMP_ISO8601_NO_COLON:
return now.getISO8601DateNoColon()
case TOKEN_KEY_TIMESTAMP_ISO8601:
return now.getRFC822Date()
case TOKEN_KEY_TIMESTAMP_PLATFORM:
return now.getISO8601Date()
case TOKEN_KEY_SDK_VERSION:
return MobileCore.extensionVersion
Expand Down
17 changes: 9 additions & 8 deletions AEPCore/Tests/RulesTests/TokenFinderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,21 @@ class TokenFinderTests: XCTestCase {
/// Given: initialize `TokenFinder` with mocked extension runtime & dummy event
let runtime = TestableExtensionRuntime()
let tokenFinder = TokenFinder(event: Event(name: "eventName", type: "eventType", source: "eventSource", data: nil), extensionRuntime: runtime)
let formatter_ISO8601 = DateFormatter()
formatter_ISO8601.locale = Locale(identifier: "en_US_POSIX")
formatter_ISO8601.setLocalizedDateFormatFromTemplate("yyyy-MM-dd'T'HH:mm:ssZZZ")
let formatter_PLATFORM = DateFormatter()
formatter_PLATFORM.locale = Locale(identifier: "en_US_POSIX")
formatter_PLATFORM.setLocalizedDateFormatFromTemplate("yyyy-MM-dd'T'HH:mm:ssXXX")
let formatter_ISO8601 = ISO8601DateFormatter()
formatter_ISO8601.timeZone = TimeZone.current
formatter_ISO8601.formatOptions.insert(.withInternetDateTime)
let formatter_ISO8601NoColon = DateFormatter()
formatter_ISO8601NoColon.locale = Locale(identifier: "en_US_POSIX")
formatter_ISO8601NoColon.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZ"

/// When: retrieve token `~timestampz`, `~timestampp` & `~timestampu`
guard let date_ISO8601_string = tokenFinder.get(key: "~timestampz") as? String, let date_ISO8601 = formatter_ISO8601.date(from: date_ISO8601_string), let date_PLATFORM_string = tokenFinder.get(key: "~timestampp") as? String, let date_PLATFORM = formatter_PLATFORM.date(from: date_PLATFORM_string), let date_UNIX_Int64 = tokenFinder.get(key: "~timestampu") as? Int64 else {
guard let date_ISO8601_string = tokenFinder.get(key: "~timestampp") as? String, let date_ISO8601 = formatter_ISO8601.date(from: date_ISO8601_string), let date_ISO8601NoColon_string = tokenFinder.get(key: "~timestampz") as? String, let date_ISO8601NoColon = formatter_ISO8601NoColon.date(from: date_ISO8601NoColon_string), let date_UNIX_Int64 = tokenFinder.get(key: "~timestampu") as? Int64 else {
XCTFail("Expected no-nil timestamp")
return
}
let date_UNIX = Date(timeIntervalSince1970: TimeInterval(date_UNIX_Int64))
/// Then: return same timestamp with different format
XCTAssertEqual(date_ISO8601, date_PLATFORM)
XCTAssertEqual(date_ISO8601, date_ISO8601NoColon)
XCTAssertEqual(date_ISO8601, date_UNIX)
}

Expand Down
14 changes: 8 additions & 6 deletions AEPServices/Sources/utility/Date+Format.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ public extension Date {
return Int64(timeIntervalSince1970)
}

func getRFC822Date() -> String {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.setLocalizedDateFormatFromTemplate("yyyy-MM-dd'T'HH:mm:ssZZZ")
// e.g. - 2020-10-28T15:08:32-06:00
func getISO8601Date() -> String {
let formatter = ISO8601DateFormatter()
formatter.timeZone = TimeZone.current
formatter.formatOptions.insert(.withInternetDateTime)
return formatter.string(from: self)
}

func getISO8601Date() -> String {
// e.g. - 2020-10-28T15:08:32-0600
func getISO8601DateNoColon() -> String {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.setLocalizedDateFormatFromTemplate("yyyy-MM-dd'T'HH:mm:ssXXX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXX"
return formatter.string(from: self)
}
}
152 changes: 152 additions & 0 deletions AEPServices/Tests/utility/Date+FormatTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import XCTest

@testable import AEPServices

class DateFormatTests: XCTestCase {

func testGetUnixTimeInSeconds() {
// setup
let victory: Int64 = 1391373045000 // Feb 2, 2014 8:30:45 pm MST
let date = Date(milliseconds: victory)

// test
let result = date.getUnixTimeInSeconds()

// verify
XCTAssertEqual(victory, result * 1000)
}

func testGetISO8601Date() {
// setup
let tzOffset = TimeZone.current.secondsFromGMT()
let victory: Int64 = Int64(tzOffset * 1000) + 1391398245000 // Feb 2, 2014 8:30:45 pm MST
let date = Date(milliseconds: victory)
let expectedDateString = getLocalExpectedDateStringFrom(date) + timezoneStringWithColon

// test
let result = date.getISO8601Date()

// verify
XCTAssertEqual(expectedDateString, result)
}

func testGetISO8601DateNoColon() {
// setup
let tzOffset = TimeZone.current.secondsFromGMT()
let victory: Int64 = Int64(tzOffset * 1000) + 1391398245000 // Feb 2, 2014 8:30:45 pm MST
let date = Date(milliseconds: victory)
let expectedDateString = getLocalExpectedDateStringFrom(date) + timezoneString

// test
let result = date.getISO8601DateNoColon()

// verify
XCTAssertEqual(expectedDateString, result)
}

// MARK: - Helpers

/// [milliseconds offset from GMT : hours offset from GMT]
let timezoneMapper: [Int:String] = [
43200: "+1200",
39600: "+1100",
36000: "+1000",
32400: "+0900",
28800: "+0800",
25200: "+0700",
21600: "+0600",
18000: "+0500",
14400: "+0400",
10800: "+0300",
7200: "+0200",
3600: "+0100",
0: "Z",
-3600: "-0100",
-7200: "-0200",
-10800: "-0300",
-14400: "-0400",
-18000: "-0500",
-21600: "-0600", // US Mountain Standard
-25200: "-0700", // US Mountain Daylight, US Pacific Standard
-28800: "-0800", // US Pacific Daylight
-32400: "-0900",
-36000: "-1000",
-39600: "-1100",
-43200: "-1200",
]

let timezoneMapperWithColon: [Int:String] = [
43200: "+12:00",
39600: "+11:00",
36000: "+10:00",
32400: "+09:00",
28800: "+08:00",
25200: "+07:00",
21600: "+06:00",
18000: "+05:00",
14400: "+04:00",
10800: "+03:00",
7200: "+02:00",
3600: "+01:00",
0: "Z",
-3600: "-01:00",
-7200: "-02:00",
-10800: "-03:00",
-14400: "-04:00",
-18000: "-05:00",
-21600: "-06:00", // US Mountain Standard
-25200: "-07:00", // US Mountain Daylight, US Pacific Standard
-28800: "-08:00", // US Pacific Daylight
-32400: "-09:00",
-36000: "-10:00",
-39600: "-11:00",
-43200: "-12:00",
]

var timezoneString: String {
if TimeZone.current.isDaylightSavingTime() {
return timezoneMapper[TimeZone.current.secondsFromGMT() - 3600] ?? ""
} else {
return timezoneMapper[TimeZone.current.secondsFromGMT()] ?? ""
}
}

var timezoneStringWithColon: String {
if TimeZone.current.isDaylightSavingTime() {
return timezoneMapperWithColon[TimeZone.current.secondsFromGMT() - 3600] ?? ""
} else {
return timezoneMapperWithColon[TimeZone.current.secondsFromGMT()] ?? ""
}
}

func getLocalExpectedDateStringFrom(_ date: Date) -> String {
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateFormat = "YYYY"
let year = formatter.string(from: date)
formatter.dateFormat = "MM"
let month = formatter.string(from: date)
formatter.dateFormat = "dd"
let day = formatter.string(from: date)
formatter.dateFormat = "HH"
let hours = formatter.string(from: date)
formatter.dateFormat = "mm"
let minutes = formatter.string(from: date)
formatter.dateFormat = "ss"
let seconds = formatter.string(from: date)

return "\(year)-\(month)-\(day)T\(hours):\(minutes):\(seconds)"
}
}

0 comments on commit 2dfd929

Please sign in to comment.