-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTimeZoneTests.swift
99 lines (76 loc) · 3.38 KB
/
TimeZoneTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// TimeZoneTests.swift
// PotentCodables
//
// Copyright © 2021 Outfox, inc.
//
//
// Distributed under the MIT License, See LICENSE for details.
//
import Foundation
import XCTest
class TimeZoneTests: XCTestCase {
func testParsingOffsets() throws {
let noOff = TimeZone.offset(from: "20201212111111.000")
XCTAssertNil(noOff)
let utcOff = TimeZone.offset(from: "20201212111111.000Z")
XCTAssertEqual(utcOff, 0)
let aheadSecsOff1 = TimeZone.offset(from: "20201212111111.000+123456")
XCTAssertEqual(aheadSecsOff1, 45296)
let aheadSecsOff2 = TimeZone.offset(from: "20201212111111.000+12:34:56")
XCTAssertEqual(aheadSecsOff2, 45296)
let behindSecsOff1 = TimeZone.offset(from: "20201212111111.000-123456")
XCTAssertEqual(behindSecsOff1, -45296)
let behindSecsOff2 = TimeZone.offset(from: "20201212111111.000-12:34:56")
XCTAssertEqual(behindSecsOff2, -45296)
let aheadMinsOff1 = TimeZone.offset(from: "20201212111111.000+1234")
XCTAssertEqual(aheadMinsOff1, 45240)
let aheadMinsOff2 = TimeZone.offset(from: "20201212111111.000+12:34")
XCTAssertEqual(aheadMinsOff2, 45240)
let behindMinsOff1 = TimeZone.offset(from: "20201212111111.000-1234")
XCTAssertEqual(behindMinsOff1, -45240)
let behindMinsOff2 = TimeZone.offset(from: "20201212111111.000-12:34")
XCTAssertEqual(behindMinsOff2, -45240)
let aheadHrsOff = TimeZone.offset(from: "20201212111111.000+12")
XCTAssertEqual(aheadHrsOff, 43200)
let behindHrsOff = TimeZone.offset(from: "20201212111111.000-12")
XCTAssertEqual(behindHrsOff, -43200)
}
func testParsingTimeZones() throws {
let noTZ = TimeZone.timeZone(from: "20201212111111.000")
XCTAssertNil(noTZ)
let utcTZ = TimeZone.timeZone(from: "20201212111111.000Z")
XCTAssertEqual(utcTZ?.secondsFromGMT(), 0)
let secondsOffset: Int
#if !os(Linux)
if #available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) {
secondsOffset = 45296
}
else {
secondsOffset = 45300
}
#else
secondsOffset = 45300
#endif
let aheadSecsTZ1 = TimeZone.timeZone(from: "20201212111111.000+123456")
XCTAssertEqual(aheadSecsTZ1?.secondsFromGMT(), secondsOffset)
let aheadSecsTZ2 = TimeZone.timeZone(from: "20201212111111.000+12:34:56")
XCTAssertEqual(aheadSecsTZ2?.secondsFromGMT(), secondsOffset)
let behindSecsTZ1 = TimeZone.timeZone(from: "20201212111111.000-123456")
XCTAssertEqual(behindSecsTZ1?.secondsFromGMT(), -secondsOffset)
let behindSecsTZ2 = TimeZone.timeZone(from: "20201212111111.000-12:34:56")
XCTAssertEqual(behindSecsTZ2?.secondsFromGMT(), -secondsOffset)
let aheadMinsTZ1 = TimeZone.timeZone(from: "20201212111111.000+1234")
XCTAssertEqual(aheadMinsTZ1?.secondsFromGMT(), 45240)
let aheadMinsTZ2 = TimeZone.timeZone(from: "20201212111111.000+12:34")
XCTAssertEqual(aheadMinsTZ2?.secondsFromGMT(), 45240)
let behindMinsTZ1 = TimeZone.timeZone(from: "20201212111111.000-1234")
XCTAssertEqual(behindMinsTZ1?.secondsFromGMT(), -45240)
let behindMinsTZ2 = TimeZone.timeZone(from: "20201212111111.000-12:34")
XCTAssertEqual(behindMinsTZ2?.secondsFromGMT(), -45240)
let aheadHrsTZ = TimeZone.timeZone(from: "20201212111111.000+12")
XCTAssertEqual(aheadHrsTZ?.secondsFromGMT(), 43200)
let behindHrsTZ = TimeZone.timeZone(from: "20201212111111.000-12")
XCTAssertEqual(behindHrsTZ?.secondsFromGMT(), -43200)
}
}