Skip to content

Commit

Permalink
Merge branch 'release/6.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
malcommac committed Sep 16, 2020
2 parents 700dde1 + 2938e5f commit a25913b
Show file tree
Hide file tree
Showing 24 changed files with 529 additions and 91 deletions.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Documentation/1.Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

## 1.0 - Dates & Cocoa

Generally when you talk about dates you are brought to think about a particular instance of time in a particular location of the world. However, in order to get a fully generic implementationn Apple made `Date` fully indipendent from any particular geographic location, calendar or locale.
Generally when you talk about dates you are brought to think about a particular instance of time in a particular location of the world. However, in order to get a fully generic implementation Apple made `Date` fully independent from any particular geographic location, calendar or locale.

A plain `Date` object just represent an absolute value: in fact it count the number of seconds elapsed since January 1, 2001.

Expand Down
20 changes: 9 additions & 11 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

// swift-tools-version:5.3
import PackageDescription

let package = Package(
name: "SwiftDate",
platforms: [
.macOS(.v10_10), .iOS(.v8), .watchOS(.v2), .tvOS(.v9)
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "SwiftDate",
targets: ["SwiftDate"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
dependencies: [],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "SwiftDate",
dependencies: []),
dependencies: [],
resources: [
.copy("Formatters/RelativeFormatter/langs")
]),
.testTarget(
name: "SwiftDateTests",
dependencies: ["SwiftDate"])
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[![Build Status](https://travis-ci.org/malcommac/SwiftDate.svg?branch=master)](https://travis-ci.org/malcommac/SwiftDate)

<p align="center" >★★ <b>Star me to follow the project! </b> ★★<br>
Created and maintaned by <b>Daniele Margutti</b> - <a href="http://www.danielemargutti.com">www.danielemargutti.com</a>
Created and maintained by <b>Daniele Margutti</b> - <a href="http://www.danielemargutti.com">www.danielemargutti.com</a>
</p>

SwiftDate is the **definitive toolchain to manipulate and display dates and time zones** on all Apple platform and even on Linux and Swift Server Side frameworks like Vapor or Kitura.
Expand All @@ -36,7 +36,7 @@ and of course...

- **IT'S TESTED!**. As 5.x the project has 90% of code coverage (want help us? write some unit tests and make a PR)
- **IT'S FULLY DOCUMENTED!**, [both with a complete guide](/Documentation/Index.md) and with Jazzy!
- **WE LOVE PLAYGROUND!** [Check out](/Playgrounds/SwiftDate.playground) our interative playground!
- **WE LOVE PLAYGROUND!** [Check out](/Playgrounds/SwiftDate.playground) our interactive playground!


## Start with SwiftDate
Expand Down Expand Up @@ -102,7 +102,7 @@ let over1Year = (date3 - date2).year > 1
<a name="3"/>

### 3. Date Comparison
SwiftDate include an extensive set of comparison functions; you can compare two dates by granularity, check if a date is an particular day, range and pratically any other comparison you ever need.
SwiftDate include an extensive set of comparison functions; you can compare two dates by granularity, check if a date is an particular day, range and practically any other comparison you ever need.

Comparison is also available via standard math operators like (`>, >=, <, <=`).

Expand Down
12 changes: 11 additions & 1 deletion Sources/SwiftDate/Date/Date+Compare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public extension Date {
return inDefaultRegion().isAfterDate(refDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity)
}

/// Returns a value between 0.0 and 1.0 or nil, that is the position of current date between 2 other dates.
///
/// - Parameters:
/// - startDate: range upper bound date
/// - endDate: range lower bound date
/// - Returns: `nil` if current date is not between `startDate` and `endDate`. Otherwise returns position between `startDate` and `endDate`.
func positionInRange(date startDate: Date, and endDate: Date) -> Double? {
return inDefaultRegion().positionInRange(date: startDate.inDefaultRegion(), and: endDate.inDefaultRegion())
}

/// Return true if receiver date is contained in the range specified by two dates.
///
/// - Parameters:
Expand All @@ -79,7 +89,7 @@ public extension Date {
/// - granularity: smallest unit that must, along with all larger units, be greater for the given dates.
/// - Returns: Boolean
func isInRange(date startDate: Date, and endDate: Date, orEqual: Bool = false, granularity: Calendar.Component = .nanosecond) -> Bool {
return self.inDefaultRegion().isInRange(date: startDate.inDefaultRegion(), and: endDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity)
return inDefaultRegion().isInRange(date: startDate.inDefaultRegion(), and: endDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity)
}

/// Compares equality of two given dates based on their components down to a given unit
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftDate/Date/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ extension Date: DateRepresentable {
/// optimize the formatting process. By default is `nil`.
public var customFormatter: DateFormatter? {
get {
let fomatter: DateFormatter? = getAssociatedValue(key: AssociatedKeys.customDateFormatter.rawValue, object: self as AnyObject)
return fomatter
let formatter: DateFormatter? = getAssociatedValue(key: AssociatedKeys.customDateFormatter.rawValue, object: self as AnyObject)
return formatter
}
set {
set(associatedValue: newValue, key: AssociatedKeys.customDateFormatter.rawValue, object: self as AnyObject)
Expand Down
22 changes: 20 additions & 2 deletions Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public extension DateInRegion {
return compare(.isSameMonth(lastMonth))

case .isSameMonth(let refDate):
return (date.year == refDate.date.year) && (date.month == refDate.date.month)
return (year == refDate.year) && (month == refDate.month)

case .isThisYear:
return compare(.isSameYear(region.nowInThisRegion()))
Expand All @@ -187,7 +187,7 @@ public extension DateInRegion {
return compare(.isSameYear(lastYear))

case .isSameYear(let refDate):
return (date.year == refDate.date.year)
return (year == refDate.year)

case .isInTheFuture:
return compare(.isLater(than: region.nowInThisRegion()))
Expand Down Expand Up @@ -280,6 +280,24 @@ public extension DateInRegion {
return (compare(toDate: date, granularity: granularity) == .orderedSame)
}

/// Returns a value between 0.0 and 1.0 or nil, that is the position of current date between 2 other dates.
///
/// - Parameters:
/// - startDate: range upper bound date
/// - endDate: range lower bound date
/// - Returns: `nil` if current date is not between `startDate` and `endDate`. Otherwise returns position between `startDate` and `endDate`.
func positionInRange(date startDate: DateInRegion, and endDate: DateInRegion) -> Double? {
let diffCurrentDateAndStartDate = self - startDate
guard diffCurrentDateAndStartDate >= 0 else {
return nil
}
let diffEndDateAndStartDate = endDate - startDate
guard diffEndDateAndStartDate > 0, diffCurrentDateAndStartDate <= diffEndDateAndStartDate else {
return nil
}
return diffCurrentDateAndStartDate / diffEndDateAndStartDate
}

/// Return `true` if receiver data is contained in the range specified by two dates.
///
/// - Parameters:
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public extension DateInRegion {
case .endOfWeek:
return dateAt(.startOfWeek).dateByAdding(7, .day).dateByAdding(-1, .second)
case .startOfMonth:
return dateBySet([.day: 1, .hour: 1, .minute: 1, .second: 1, .nanosecond: 1])!
return dateBySet([.day: 1, .hour: 0, .minute: 0, .second: 0, .nanosecond: 0])!
case .endOfMonth:
return dateByAdding((monthDays - day), .day).dateAtEndOf(.day)
case .tomorrow:
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDate/Formatters/ISOParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ public class ISOParser: StringToDateTransformable {
return char.isDigit
}

/// MARK: - Scanner internal functions
// MARK: - Scanner internal functions

/// Get the value at specified offset from current scanner position without
/// moving the current scanner's index.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,32 @@ internal class RelativeFormatterLanguagesCache {

static let shared = RelativeFormatterLanguagesCache()

@Atomic
private(set) var cachedValues = [String: [String: Any]]()

func flavoursForLocaleID(_ langID: String) -> [String: Any]? {
do {
guard let fullURL = Bundle(for: RelativeFormatter.self).resourceURL?.appendingPathComponent("langs/\(langID).json") else {
return nil
}
let data = try Data(contentsOf: fullURL)
let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)

guard let cachedValue = cachedValues[langID] else {
var fileURL = Bundle.appModule?.url(forResource: langID, withExtension: "json", subdirectory: "langs")
if fileURL == nil {
fileURL = Bundle(for: RelativeFormatter.self).resourceURL?.appendingPathComponent("langs/\(langID).json")
}

guard let fullURL = fileURL else {
return nil
}
let data = try Data(contentsOf: fullURL)
let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)

if let value = json as? [String: Any] {
cachedValues[langID] = value
return value
} else {
if let value = json as? [String: Any] {
cachedValues[langID] = value
return value
}
return nil
}

return cachedValue

} catch {
debugPrint("Failed to read data for language id: \(langID)")
Expand Down
26 changes: 6 additions & 20 deletions Sources/SwiftDate/Formatters/RelativeFormatter/langs/sk.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@
"current" : "tento týždeň",
"past" : {
"other" : "pred {0} týždňami",
"many" : "pred {0} týždňa",
"one" : "pred {0} týždňom"
},
"previous" : "minulý týždeň",
"future" : {
"many" : "o {0} týždňa",
"one" : "o {0} týždeň",
"few" : "o {0} týždne",
"other" : "o {0} týždňov"
Expand All @@ -72,12 +70,10 @@
},
"hour" : {
"past" : {
"many" : "pred {0} hodinou",
"one" : "pred {0} hodinou",
"other" : "pred {0} hodinami"
},
"future" : {
"many" : "o {0} hodiny",
"one" : "o {0} hodinu",
"few" : "o {0} hodiny",
"other" : "o {0} hodín"
Expand All @@ -87,13 +83,11 @@
"quarter" : {
"previous" : "minulý štvrťrok",
"future" : {
"many" : "o {0} štvrťroka",
"other" : "o {0} štvrťrokov",
"few" : "o {0} štvrťroky",
"one" : "o {0} štvrťrok"
},
"past" : {
"many" : "pred {0} štvrťroka",
"one" : "pred {0} štvrťrokom",
"other" : "pred {0} štvrťrokmi"
},
Expand All @@ -103,27 +97,23 @@
"minute" : {
"current" : "v tejto minúte",
"future" : {
"many" : "o {0} minúty",
"one" : "o {0} minútu",
"other" : "o {0} minút",
"few" : "o {0} minúty"
},
"past" : {
"other" : "pred {0} minútami",
"one" : "pred {0} minútou",
"many" : "pred {0} minúty"
"one" : "pred {0} minútou"
}
},
"year" : {
"past" : {
"many" : "pred {0} roka",
"other" : "pred {0} rokmi",
"one" : "pred {0} rokom"
},
"future" : {
"few" : "o {0} roky",
"one" : "o {0} rok",
"many" : "o {0} roka",
"other" : "o {0} rokov"
},
"current" : "tento rok",
Expand All @@ -132,7 +122,7 @@
},
"month" : {
"future" : {
"many" : "o {0} mesiaca",
"many" : "o {0} mesiacov",
"one" : "o {0} mesiac",
"few" : "o {0} mesiace",
"other" : "o {0} mesiacov"
Expand All @@ -141,7 +131,7 @@
"previous" : "minulý mesiac",
"next" : "budúci mesiac",
"past" : {
"many" : "pred {0} mesiaca",
"many" : "pred {0} mesiacmi",
"other" : "pred {0} mesiacmi",
"one" : "pred {0} mesiacom"
}
Expand All @@ -150,11 +140,9 @@
"current" : "teraz",
"past" : {
"one" : "pred {0} sekundou",
"many" : "pred {0} sekundy",
"other" : "pred {0} sekundami"
},
"future" : {
"many" : "o {0} sekundy",
"few" : "o {0} sekundy",
"one" : "o {0} sekundu",
"other" : "o {0} sekúnd"
Expand All @@ -164,16 +152,14 @@
"previous" : "včera",
"next" : "zajtra",
"past" : {
"many" : "pred {0} dňa",
"one" : "pred {0} dňom",
"other" : "pred {0} dňami"
},
"current" : "dnes",
"future" : {
"many" : "o {0} dňa",
"one" : "o {0} deň",
"other" : "o {0} dní",
"few" : "o {0} dni"
"few" : "o {0} dni",
"other" : "o {0} dní"
}
}
},
Expand Down Expand Up @@ -230,4 +216,4 @@
},
"now" : "teraz"
}
}
}
Loading

0 comments on commit a25913b

Please sign in to comment.