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

Enable passing a locale to RelativeDateFormatter #812

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
9 changes: 7 additions & 2 deletions Sources/SwiftDate/DateRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ public protocol DateRepresentable {
/// if not passed).
func toRelative(since: DateInRegion?,
dateTimeStyle: RelativeDateTimeFormatter.DateTimeStyle,
unitsStyle: RelativeDateTimeFormatter.UnitsStyle) -> String
unitsStyle: RelativeDateTimeFormatter.UnitsStyle,
locale: LocaleConvertible?) -> String

/// Return ISO8601 representation of the date
///
Expand Down Expand Up @@ -522,10 +523,14 @@ public extension DateRepresentable {

func toRelative(since: DateInRegion?,
dateTimeStyle: RelativeDateTimeFormatter.DateTimeStyle = .named,
unitsStyle: RelativeDateTimeFormatter.UnitsStyle = .short) -> String {
unitsStyle: RelativeDateTimeFormatter.UnitsStyle = .short,
locale: LocaleConvertible? = nil) -> String {
let formatter = RelativeDateTimeFormatter()
formatter.dateTimeStyle = dateTimeStyle
formatter.unitsStyle = unitsStyle
if let fixedLocale = locale?.toLocale() {
formatter.locale = fixedLocale
}
return formatter.localizedString(for: self.date, relativeTo: since?.date ?? Date())
}

Expand Down
33 changes: 19 additions & 14 deletions Tests/SwiftDateTests/TestFormatters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -492,31 +492,36 @@ class TestFormatters: XCTestCase {
XCTAssert(value1 == "2 hours, 5 minutes, 32 seconds", "Failed to format interval to string")
XCTAssert(value2 == "2d 5h", "Failed to format interval to string")
}
/*

func testColloquialFormatter() {
let ago5Mins = DateInRegion() - 5.minutes
let r1 = ago5Mins.toRelative(style: RelativeFormatter.defaultStyle(), locale: Locales.italian)
let r2 = ago5Mins.toRelative(style: RelativeFormatter.twitterStyle(), locale: Locales.italian)
let r1 = ago5Mins.toRelative(since: DateInRegion(), dateTimeStyle: .numeric, unitsStyle: .full, locale: Locales.italian)
let r2 = ago5Mins.toRelative(since: DateInRegion(), dateTimeStyle: .numeric, unitsStyle: .short, locale: Locales.italian)
XCTAssert(r1 == "5 minuti fa", "Failed to use colloquial formatter")
XCTAssert(r2 == "5 min fa", "Failed to use colloquial formatter")

let justNow = DateInRegion() - 10.seconds
let r3 = justNow.toRelative(style: RelativeFormatter.defaultStyle(), locale: Locales.italian)
XCTAssert(r3 == "ora", "Failed to use colloquial formatter")
// let justNow = DateInRegion() - 10.seconds
// let r3 = justNow.toRelative(since: DateInRegion(), dateTimeStyle: .named, unitsStyle: .full, locale: Locales.italian)
// XCTAssert(r3 == "ora", "Failed to use colloquial formatter")

let justNow2 = DateInRegion() - 2.hours
let r4 = justNow2.toRelative(style: RelativeFormatter.twitterStyle(), locale: Locales.italian)
XCTAssert(r4 == "2h fa", "Failed to use colloquial formatter")
let r4 = justNow2.toRelative(since: DateInRegion(), dateTimeStyle: .numeric, unitsStyle: .abbreviated, locale: Locales.italian)
XCTAssert(r4 == "2 h fa", "Failed to use colloquial formatter")

let justNow3 = DateInRegion() - 1.minutes
let r5 = justNow3.toRelative(style: RelativeFormatter.twitterStyle(), locale: Locales.english)
let r5 = justNow3.toRelative(since: DateInRegion(), dateTimeStyle: .numeric, unitsStyle: .abbreviated, locale: Locales.english)
XCTAssert(r5 == "1 min. ago", "Failed to use colloquial formatter")

let justNow4 = DateInRegion() - 51.seconds
let r6 = justNow4.toRelative(style: RelativeFormatter.twitterStyle(), locale: Locales.english)
XCTAssert(r6 == "1 min. ago", "Failed to use colloquial formatter")
}
*/
// let justNow4 = DateInRegion() - 51.seconds
// let r6 = justNow4.toRelative(since: DateInRegion(), dateTimeStyle: .numeric, unitsStyle: .abbreviated, locale: Locales.english)
// XCTAssert(r6 == "1 min. ago", "Failed to use colloquial formatter")

let justNow4 = DateInRegion() - 1.minutes
let r6 = justNow4.toRelative(since: DateInRegion(), dateTimeStyle: .numeric, unitsStyle: .short, locale: Locales.german)
XCTAssert(r6 == "vor 1 Min.", "Failed to use colloquial formatter")

}

func testISOParser() {

func testISO(_ src: String, _ exp: String) {
Expand Down