Skip to content

Commit

Permalink
Merge branch 'release/5.0.12'
Browse files Browse the repository at this point in the history
  • Loading branch information
malcommac committed Oct 26, 2018
2 parents b2a1c8e + 02ad67e commit 9060408
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SwiftDate is the **definitive toolchain to manipulate and display dates and time

### Help SwiftDate Development

<img src="Documentation/support_patreon_button.png" width=160px alt="SwiftDate" title="SwiftDate" ahref="https://www.patreon.com/danielemargutti">
<a href="https://www.patreon.com/danielemargutti"><img src="Documentation/support_patreon_button.png" width=160px alt="SwiftDate" title="SwiftDate"></a>
</p>

If you are using SwiftDate or any other my libraries please [consider support further development](https://www.patreon.com/danielemargutti) and mainteniance becoming a patreon.
Expand Down
5 changes: 0 additions & 5 deletions Sources/SwiftDate/DateInRegion/DateInRegion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ public struct DateInRegion: DateRepresentable, Decodable, Encodable, CustomStrin
/// Associated region which define where the date is represented into the world.
public let region: Region

/// Allows to generate an unique hash vaalue for an instance of `DateInRegion`
public var hashValue: Int {
return combineHashes([self.date.hashValue, self.region.hashValue])
}

/// Formatter used to transform this object in a string. By default is `nil` because SwiftDate
/// uses the thread shared formatter in order to avoid expensive init of the `DateFormatter` object.
/// However, if you need of a custom behaviour you can set a valid value.
Expand Down
29 changes: 19 additions & 10 deletions Sources/SwiftDate/DateRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public protocol DateRepresentable {
/// Name of the weekday expressed in given format style.
///
/// - Parameter style: style to express the value.
/// - Parameter locale: locale to use; ignore it to use default's region locale.
/// - Returns: weekday name
func weekdayName(_ style: SymbolFormatStyle) -> String
func weekdayName(_ style: SymbolFormatStyle, locale: LocaleConvertible?) -> String

/// Week of a year of the receiver.
var weekOfYear: Int { get }
Expand Down Expand Up @@ -95,17 +96,19 @@ public protocol DateRepresentable {
/// Quarter name expressed in given format style.
///
/// - Parameter style: style to express the value.
/// - Parameter locale: locale to use; ignore it to use default's region locale.
/// - Returns: quarter name
func quarterName(_ style: SymbolFormatStyle) -> String
func quarterName(_ style: SymbolFormatStyle, locale: LocaleConvertible?) -> String

/// Era value of the receiver.
var era: Int { get }

/// Name of the era expressed in given format style.
///
/// - Parameter style: style to express the value.
/// - Parameter locale: locale to use; ignore it to use default's region locale.
/// - Returns: era
func eraName(_ style: SymbolFormatStyle) -> String
func eraName(_ style: SymbolFormatStyle, locale: LocaleConvertible?) -> String

/// The current daylight saving time offset of the represented date.
var DSTOffset: TimeInterval { get }
Expand Down Expand Up @@ -376,8 +379,10 @@ public extension DateRepresentable {
return self.dateComponents.weekday!
}

func weekdayName(_ style: SymbolFormatStyle) -> String {
let formatter = self.formatter(format: nil)
func weekdayName(_ style: SymbolFormatStyle, locale: LocaleConvertible? = nil) -> String {
let formatter = self.formatter(format: nil) {
$0.locale = (locale ?? self.region.locale).toLocale()
}
let idx = (self.weekday - 1)
switch style {
case .default: return formatter.weekdaySymbols[idx]
Expand Down Expand Up @@ -442,9 +447,11 @@ public extension DateRepresentable {
return self.date > Date()
}

func quarterName(_ style: SymbolFormatStyle) -> String {
let formatter = self.formatter(format: nil)
let idx = self.quarter
func quarterName(_ style: SymbolFormatStyle, locale: LocaleConvertible? = nil) -> String {
let formatter = self.formatter(format: nil) {
$0.locale = (locale ?? self.region.locale).toLocale()
}
let idx = (self.quarter - 1)
switch style {
case .default: return formatter.quarterSymbols[idx]
case .defaultStandalone: return formatter.standaloneQuarterSymbols[idx]
Expand All @@ -457,8 +464,10 @@ public extension DateRepresentable {
return self.dateComponents.era!
}

func eraName(_ style: SymbolFormatStyle) -> String {
let formatter = self.formatter(format: nil)
func eraName(_ style: SymbolFormatStyle, locale: LocaleConvertible? = nil) -> String {
let formatter = self.formatter(format: nil) {
$0.locale = (locale ?? self.region.locale).toLocale()
}
let idx = (self.era - 1)
switch style {
case .default, .defaultStandalone: return formatter.longEraSymbols[idx]
Expand Down
20 changes: 0 additions & 20 deletions Sources/SwiftDate/Supports/Commons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,26 +331,6 @@ public struct TimeCalculationOptions {
}
}

// MARK: - Hash Generation

// Note: we can remove it for Swift 4.2
// https://github.com/krzysztofzablocki/Sourcery/blob/a092c4b2a47daee26fa11b8be92e0484a5bc9633/Templates/Tests/Expected/AutoHashable.expected#L7
internal func combineHashes(_ hashes: [Int]) -> Int {
return hashes.reduce(0, combineHashValues)
}

private func combineHashValues(_ initial: Int, _ other: Int) -> Int {
#if arch(x86_64) || arch(arm64)
let magic: UInt = 0x9e3779b97f4a7c15
#elseif arch(i386) || arch(arm) || arch(arm64_32)
let magic: UInt = 0x9e3779b9
#endif
var lhs = UInt(bitPattern: initial)
let rhs = UInt(bitPattern: other)
lhs ^= rhs &+ magic &+ (lhs << 6) &+ (lhs >> 2)
return Int(bitPattern: lhs)
}

//MARK: - compactMap for Swift 4.0 (not necessary > 4.0)

#if swift(>=4.1)
Expand Down
4 changes: 2 additions & 2 deletions SwiftDate.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "SwiftDate"
s.version = "5.0.11"
s.version = "5.0.12"
s.summary = "The best way to deal with Dates & Time Zones in Swift"
s.homepage = "https://github.com/malcommac/SwiftDate.git"
s.license = { :type => "MIT", :file => "LICENSE" }
Expand All @@ -13,5 +13,5 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/malcommac/SwiftDate.git", :tag => s.version.to_s }
s.source_files = "Sources/**/*"
s.frameworks = "Foundation"
s.swift_version = "4.0"
s.swift_version = "4.2"
end
13 changes: 11 additions & 2 deletions Tests/SwiftDateTests/TestDateInRegion+Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ class TestDateInRegion_Components: XCTestCase {
// TEST #3: Other components
XCTAssert( (dateB.region == regionParis), "Failed to assign correct region to date")
XCTAssert( (dateB.calendar.identifier == regionParis.calendar.identifier), "Failed to assign correct region's calendar to date")
XCTAssert( (dateB.quarterName(.default) == "2e trimestre"), "Failed to get quarterName in default")
XCTAssert( (dateB.quarterName(.short) == "T2"), "Failed to get quarterName in short")
XCTAssert( (dateB.quarterName(.default) == "1er trimestre"), "Failed to get quarterName in default")
XCTAssert( (dateB.quarterName(.short) == "T1"), "Failed to get quarterName in short")
XCTAssert( (dateB.quarterName(.default, locale: Locales.italian) == "1º trimestre"), "Failed to get quarterName with overwrite of locale")
}

func testDateInRegion_isLeapMonth() {
Expand Down Expand Up @@ -253,4 +254,12 @@ class TestDateInRegion_Components: XCTestCase {
XCTAssert( dateB.quarter == 3, "Failed to evaluate quarter property")
XCTAssert( dateC.quarter == 4, "Failed to evaluate quarter property")
}

func testAbsoluteDateISOFormatting() {
let now = DateInRegion()
let iso8601_string = now.toISO([.withInternetDateTime])
let absoluteDate = now.date
let absoluteDate_iso8601_string = absoluteDate.toISO([.withInternetDateTime])
XCTAssert( absoluteDate_iso8601_string == iso8601_string, "Failed respect the absolute ISO date")
}
}

0 comments on commit 9060408

Please sign in to comment.