Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for an issue that caused WeekStartDay to fail while initializing Region #786

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/SwiftDate/DateInRegion/Region.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDate/Foundation+Extras/String+Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 7 additions & 7 deletions Sources/SwiftDate/Supports/Commons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal struct Atomic<Value> {
init(wrappedValue: Value) {
self.value = wrappedValue
}

var wrappedValue: Value {
get {
return queue.sync { value }
Expand All @@ -31,7 +31,7 @@ internal struct Atomic<Value> {
queue.sync { value = newValue }
}
}

}

// MARK: - DateFormatter
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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? = {
Expand All @@ -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 {
Expand All @@ -336,8 +336,8 @@ extension Foundation.Bundle {
return bundle
}
}

return nil
}()

}
4 changes: 2 additions & 2 deletions Tests/SwiftDateTests/TestDateInRegion+Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,13 @@ 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

let now = Date()
XCTAssert((now.addingTimeInterval(3600) - now).in(.hour) == 1, "Failed to compare date")
XCTAssert((now.addingTimeInterval(3600) - now) == 1.hours, "Failed to compare date")
}

}
12 changes: 12 additions & 0 deletions Tests/SwiftDateTests/TestRegion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions Tests/SwiftDateTests/TestSwiftDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down