diff --git a/Sources/SwiftDate/DateInRegion/Region.swift b/Sources/SwiftDate/DateInRegion/Region.swift index 23707f86..adc8da60 100644 --- a/Sources/SwiftDate/DateInRegion/Region.swift +++ b/Sources/SwiftDate/DateInRegion/Region.swift @@ -50,6 +50,8 @@ public struct Region: Decodable, Encodable, Equatable, Hashable, CustomStringCon self.calendar = Calendar.newCalendar(calendar, configure: { $0.timeZone = zone.toTimezone() $0.locale = locale.toLocale() + $0.firstWeekday = calendar.toCalendar().firstWeekday + $0.minimumDaysInFirstWeek = calendar.toCalendar().minimumDaysInFirstWeek }) } diff --git a/Sources/SwiftDate/Foundation+Extras/Int+DateComponents.swift b/Sources/SwiftDate/Foundation+Extras/Int+DateComponents.swift index e9e28ca4..e8c8970b 100644 --- a/Sources/SwiftDate/Foundation+Extras/Int+DateComponents.swift +++ b/Sources/SwiftDate/Foundation+Extras/Int+DateComponents.swift @@ -26,7 +26,7 @@ public extension Int { /// - returns: return self value in form of `DateComponents` where given `Calendar.Component` has `self` as value internal func toDateComponents(type: Calendar.Component) -> DateComponents { var dateComponents = DateComponents() - DateComponents.allComponents.forEach( { dateComponents.setValue(0, for: $0 )}) + DateComponents.allComponents.forEach({ dateComponents.setValue(0, for: $0 ) }) dateComponents.setValue(self, for: type) dateComponents.setValue(0, for: .era) return dateComponents diff --git a/Sources/SwiftDate/Foundation+Extras/String+Parser.swift b/Sources/SwiftDate/Foundation+Extras/String+Parser.swift index bb2273d0..8ce7655f 100644 --- a/Sources/SwiftDate/Foundation+Extras/String+Parser.swift +++ b/Sources/SwiftDate/Foundation+Extras/String+Parser.swift @@ -116,7 +116,7 @@ extension String: DateParsable { public func toSQLDate(region: Region = Region.ISO) -> DateInRegion? { StringToDateStyles.sql.toDate(self, region: region) } - + public func asLocale() -> Locale { Locale(identifier: self) } diff --git a/Sources/SwiftDate/Supports/Commons.swift b/Sources/SwiftDate/Supports/Commons.swift index e3de49c2..386515ea 100644 --- a/Sources/SwiftDate/Supports/Commons.swift +++ b/Sources/SwiftDate/Supports/Commons.swift @@ -22,7 +22,7 @@ internal struct Atomic { init(wrappedValue: Value) { self.value = wrappedValue } - + var wrappedValue: Value { get { return queue.sync { value } @@ -31,7 +31,7 @@ internal struct Atomic { queue.sync { value = newValue } } } - + } // MARK: - DateFormatter @@ -263,7 +263,7 @@ public enum DateRelatedType { case yesterday case yesterdayAtStart case nearestMinute(minute: Int) - case nearestHour(hour :Int) + case nearestHour(hour: Int) case nextWeekday(_: WeekDay) case nextDSTDate case prevMonth @@ -313,7 +313,7 @@ public struct TimeCalculationOptions { private class BundleFinder {} extension Foundation.Bundle { - + /// Returns the resource bundle associated with the current Swift module. /// This is used instead of `module` to allows compatibility outside the SwiftPM environment (ie. CocoaPods). static var appModule: Bundle? = { @@ -327,7 +327,7 @@ extension Foundation.Bundle { Bundle(for: BundleFinder.self).resourceURL, // For command-line tools. - Bundle.main.bundleURL, + Bundle.main.bundleURL ] for candidate in candidates { @@ -336,8 +336,8 @@ extension Foundation.Bundle { return bundle } } - + return nil }() - + } diff --git a/Tests/SwiftDateTests/TestDateInRegion+Components.swift b/Tests/SwiftDateTests/TestDateInRegion+Components.swift index ae1f0ccb..ddd3e18b 100644 --- a/Tests/SwiftDateTests/TestDateInRegion+Components.swift +++ b/Tests/SwiftDateTests/TestDateInRegion+Components.swift @@ -264,7 +264,7 @@ class TestDateInRegion_Components: XCTestCase { let absoluteDate_iso8601_string = absoluteDate.toISO([.withInternetDateTime]) XCTAssert( absoluteDate_iso8601_string == iso8601_string, "Failed respect the absolute ISO date") } - + func testComparingTimeUnitsWithDateComponents() { SwiftDate.defaultRegion = .local @@ -272,5 +272,5 @@ class TestDateInRegion_Components: XCTestCase { XCTAssert((now.addingTimeInterval(3600) - now).in(.hour) == 1, "Failed to compare date") XCTAssert((now.addingTimeInterval(3600) - now) == 1.hours, "Failed to compare date") } - + } diff --git a/Tests/SwiftDateTests/TestRegion.swift b/Tests/SwiftDateTests/TestRegion.swift index 7095f3b0..2d0434e0 100644 --- a/Tests/SwiftDateTests/TestRegion.swift +++ b/Tests/SwiftDateTests/TestRegion.swift @@ -124,6 +124,18 @@ class TestRegion: XCTestCase { } + func testRegionInit_SettingFirstWeekday() { + + var calendar = Calendars.gregorian.toCalendar() + calendar.firstWeekday = 2 + let regionStartingFromMonday = Region(calendar: calendar, zone: Zones.gmt, locale: Locales.englishUnitedStates) + XCTAssert(regionStartingFromMonday.calendar.firstWeekday == 2, "Failed to set firstWeekDay for Region") + + calendar.firstWeekday = 4 + let regionStartingFromWednesday = Region(calendar: calendar, zone: Zones.gmt, locale: Locales.englishUnitedStates) + XCTAssert(regionStartingFromWednesday.calendar.firstWeekday == 4, "Failed to set firstWeekDay for Region") + } + } func XCTAssertInTimeIntervalRange(value: Double, range: TimeInterval, _ error: String) { diff --git a/Tests/SwiftDateTests/TestSwiftDate.swift b/Tests/SwiftDateTests/TestSwiftDate.swift index 49ddc057..4cd4d7c4 100644 --- a/Tests/SwiftDateTests/TestSwiftDate.swift +++ b/Tests/SwiftDateTests/TestSwiftDate.swift @@ -27,16 +27,16 @@ class TestSwiftDate: XCTestCase { func testUTCZone() { SwiftDate.defaultRegion = Region(calendar: Calendars.gregorian, zone: Zones.asiaShanghai, locale: Locales.current) - + // DO NOT recognized the right timezone // The timezone should be UTC let wrongZone = "2020-03-13T05:40:48.000Z" - let wrongZoneDate = Date.init(wrongZone) + let wrongZoneDate = Date(wrongZone) print(wrongZoneDate!.description) XCTAssert("2020-03-13 05:40:48 +0000" == wrongZoneDate!.description) - + let iso8601Time = "2020-03-13T05:40:48+00:00" - let iso8601Date = Date.init(iso8601Time) + let iso8601Date = Date(iso8601Time) print(iso8601Date!.description) XCTAssert("2020-03-13 05:40:48 +0000" == iso8601Date!.description) }