From c23b350329d24ed258da429044fb67a94b630e48 Mon Sep 17 00:00:00 2001 From: Stefan Herold Date: Fri, 7 Aug 2020 10:06:48 +0200 Subject: [PATCH] Add year name formatting --- README.md | 8 +- Tests/DateFormatterTests.swift | 270 ++++++++++++++++++--------- source/Classes/SHDateFormatter.swift | 16 +- 3 files changed, 197 insertions(+), 97 deletions(-) diff --git a/README.md b/README.md index 9f5d41b..41a4659 100644 --- a/README.md +++ b/README.md @@ -37,10 +37,10 @@ This framework supports encoding as well as decoding many different date and time format. At the moment the follownig are supported (see SHDateFormat): -* `shortWeekdayName` => Sun, Mon, Tue, ... -* `longWeekdayName` => Sunday, Monday, Tuesday, ... -* `shortMonthName` => Jan, Feb, ... -* `longMonthName` => January, February, ... +* `shortWeekday` => Sun, Mon, Tue, ... +* `longWeekday` => Sunday, Monday, Tuesday, ... +* `shortMonth` => Jan, Feb, ... +* `longMonth` => January, February, ... * `shortTimeNoDate` => 12:00 PM, 1:00 AM * `shortTimeMediumDate` => Jan 1, 2000 at 12:00 AM * `noTimeShortDateNoYear` => 12/31 diff --git a/Tests/DateFormatterTests.swift b/Tests/DateFormatterTests.swift index 3b4f043..b4feaa5 100644 --- a/Tests/DateFormatterTests.swift +++ b/Tests/DateFormatterTests.swift @@ -9,7 +9,7 @@ final class DateFormatterTests: QuickSpec { // swiftlint:disable:next function_body_length override func spec() { - let gmtZone = TimeZone(identifier: "Europe/London")! + let gmtZone = TimeZone.GMT let deDELocale = Locale(identifier: "de_DE") let enUSLocale = Locale(identifier: "en_US") @@ -69,7 +69,7 @@ final class DateFormatterTests: QuickSpec { context("General Tests") { it("returns a nil date the input string is nil") { - let result = SHDateFormatter.shared.date(from: nil, format: .shortWeekdayName) + let result = SHDateFormatter.shared.date(from: nil, format: .shortWeekday) expect(result).to(beNil()) } } @@ -80,7 +80,7 @@ final class DateFormatterTests: QuickSpec { context("General Tests") { it("returns an empty string the input date is nil") { - let result = SHDateFormatter.shared.string(from: nil, format: .shortWeekdayName) + let result = SHDateFormatter.shared.string(from: nil, format: .shortWeekday) expect(result) == "" } } @@ -99,38 +99,54 @@ final class DateFormatterTests: QuickSpec { context("\(deDELocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: deDELocale, timeZone: gmtZone) expect(result) == "Sa" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: deDELocale, timeZone: gmtZone) expect(result) == "Samstag" } - it("ShortMonthName") { + it("ShortMonth") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortMonthName, + format: .shortMonth, locale: deDELocale, timeZone: gmtZone) expect(result) == "Jan" } - it("LongMonthName") { + it("LongMonth") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longMonthName, + format: .longMonth, locale: deDELocale, timeZone: gmtZone) expect(result) == "Januar" } + it("ShortYear") { + let result = SHDateFormatter.shared.string(from: testDate, + format: .shortYear, + locale: deDELocale, + timeZone: gmtZone) + expect(result) == "00" + } + + it("LongYear") { + let result = SHDateFormatter.shared.string(from: testDate, + format: .longYear, + locale: deDELocale, + timeZone: gmtZone) + expect(result) == "2000" + } + it("ShortTimeNoDate") { let result = SHDateFormatter.shared.string(from: testDate, format: .shortTimeNoDate, @@ -182,38 +198,54 @@ final class DateFormatterTests: QuickSpec { context("\(enUSLocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: enUSLocale, timeZone: gmtZone) expect(result) == "Sat" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: enUSLocale, timeZone: gmtZone) expect(result) == "Saturday" } - it("ShortMonthName") { + it("ShortMonth") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortMonthName, + format: .shortMonth, locale: enUSLocale, timeZone: gmtZone) expect(result) == "Jan" } - it("LongMonthName") { + it("LongMonth") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longMonthName, + format: .longMonth, locale: enUSLocale, timeZone: gmtZone) expect(result) == "January" } + it("ShortYear") { + let result = SHDateFormatter.shared.string(from: testDate, + format: .shortYear, + locale: enUSLocale, + timeZone: gmtZone) + expect(result) == "00" + } + + it("LongYear") { + let result = SHDateFormatter.shared.string(from: testDate, + format: .longYear, + locale: enUSLocale, + timeZone: gmtZone) + expect(result) == "2000" + } + it("ShortTimeNoDate") { let result = SHDateFormatter.shared.string(from: testDate, format: .shortTimeNoDate, @@ -265,38 +297,54 @@ final class DateFormatterTests: QuickSpec { context("\(frFRLocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: frFRLocale, timeZone: gmtZone) expect(result) == "sam." } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: frFRLocale, timeZone: gmtZone) expect(result) == "samedi" } - it("ShortMonthName") { + it("ShortMonth") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortMonthName, + format: .shortMonth, locale: frFRLocale, timeZone: gmtZone) expect(result) == "janv." } - it("LongMonthName") { + it("LongMonth") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longMonthName, + format: .longMonth, locale: frFRLocale, timeZone: gmtZone) expect(result) == "janvier" } + it("ShortYear") { + let result = SHDateFormatter.shared.string(from: testDate, + format: .shortYear, + locale: frFRLocale, + timeZone: gmtZone) + expect(result) == "00" + } + + it("LongYear") { + let result = SHDateFormatter.shared.string(from: testDate, + format: .longYear, + locale: frFRLocale, + timeZone: gmtZone) + expect(result) == "2000" + } + it("ShortTimeNoDate") { let result = SHDateFormatter.shared.string(from: testDate, format: .shortTimeNoDate, @@ -361,38 +409,54 @@ final class DateFormatterTests: QuickSpec { context("\(deDELocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: deDELocale, timeZone: gmtZone) expect(result) == "So" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: deDELocale, timeZone: gmtZone) expect(result) == "Sonntag" } - it("ShortMonthName") { + it("ShortMonth") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortMonthName, + format: .shortMonth, locale: deDELocale, timeZone: gmtZone) expect(result) == "Jan" } - it("LongMonthName") { + it("LongMonth") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longMonthName, + format: .longMonth, locale: deDELocale, timeZone: gmtZone) expect(result) == "Januar" } + it("ShortYear") { + let result = SHDateFormatter.shared.string(from: testDate, + format: .shortYear, + locale: deDELocale, + timeZone: gmtZone) + expect(result) == "00" + } + + it("LongYear") { + let result = SHDateFormatter.shared.string(from: testDate, + format: .longYear, + locale: deDELocale, + timeZone: gmtZone) + expect(result) == "2000" + } + it("ShortTimeNoDate") { let result = SHDateFormatter.shared.string(from: testDate, format: .shortTimeNoDate, @@ -444,38 +508,54 @@ final class DateFormatterTests: QuickSpec { context("\(enUSLocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: enUSLocale, timeZone: gmtZone) expect(result) == "Sun" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: enUSLocale, timeZone: gmtZone) expect(result) == "Sunday" } - it("ShortMonthName") { + it("ShortMonth") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortMonthName, + format: .shortMonth, locale: enUSLocale, timeZone: gmtZone) expect(result) == "Jan" } - it("LongMonthName") { + it("LongMonth") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longMonthName, + format: .longMonth, locale: enUSLocale, timeZone: gmtZone) expect(result) == "January" } + it("ShortYear") { + let result = SHDateFormatter.shared.string(from: testDate, + format: .shortYear, + locale: enUSLocale, + timeZone: gmtZone) + expect(result) == "00" + } + + it("LongYear") { + let result = SHDateFormatter.shared.string(from: testDate, + format: .longYear, + locale: enUSLocale, + timeZone: gmtZone) + expect(result) == "2000" + } + it("ShortTimeNoDate") { let result = SHDateFormatter.shared.string(from: testDate, format: .shortTimeNoDate, @@ -527,38 +607,54 @@ final class DateFormatterTests: QuickSpec { context("\(frFRLocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: frFRLocale, timeZone: gmtZone) expect(result) == "dim." } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: frFRLocale, timeZone: gmtZone) expect(result) == "dimanche" } - it("ShortMonthName") { + it("ShortMonth") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortMonthName, + format: .shortMonth, locale: frFRLocale, timeZone: gmtZone) expect(result) == "janv." } - it("LongMonthName") { + it("LongMonth") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longMonthName, + format: .longMonth, locale: frFRLocale, timeZone: gmtZone) expect(result) == "janvier" } + it("ShortYear") { + let result = SHDateFormatter.shared.string(from: testDate, + format: .shortYear, + locale: frFRLocale, + timeZone: gmtZone) + expect(result) == "00" + } + + it("LongYear") { + let result = SHDateFormatter.shared.string(from: testDate, + format: .longYear, + locale: frFRLocale, + timeZone: gmtZone) + expect(result) == "2000" + } + it("ShortTimeNoDate") { let result = SHDateFormatter.shared.string(from: testDate, format: .shortTimeNoDate, @@ -619,17 +715,17 @@ final class DateFormatterTests: QuickSpec { context("\(deDELocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: deDELocale, timeZone: gmtZone, needsRelativeFormatting: true) expect(result) == "" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: deDELocale, timeZone: gmtZone, needsRelativeFormatting: true) @@ -693,18 +789,18 @@ final class DateFormatterTests: QuickSpec { context("\(enUSLocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: enUSLocale, timeZone: gmtZone, needsRelativeFormatting: true) expect(result) == "" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: enUSLocale, timeZone: gmtZone, needsRelativeFormatting: true) @@ -768,18 +864,18 @@ final class DateFormatterTests: QuickSpec { context("\(frFRLocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: frFRLocale, timeZone: gmtZone, needsRelativeFormatting: true) expect(result) == "" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: frFRLocale, timeZone: gmtZone, needsRelativeFormatting: true) @@ -850,18 +946,18 @@ final class DateFormatterTests: QuickSpec { context("\(deDELocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: deDELocale, timeZone: gmtZone, needsRelativeFormatting: true) expect(result) == "" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: deDELocale, timeZone: gmtZone, needsRelativeFormatting: true) @@ -925,18 +1021,18 @@ final class DateFormatterTests: QuickSpec { context("\(enUSLocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: enUSLocale, timeZone: gmtZone, needsRelativeFormatting: true) expect(result) == "" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: enUSLocale, timeZone: gmtZone, needsRelativeFormatting: true) @@ -1000,18 +1096,18 @@ final class DateFormatterTests: QuickSpec { context("\(frFRLocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: frFRLocale, timeZone: gmtZone, needsRelativeFormatting: true) expect(result) == "" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: frFRLocale, timeZone: gmtZone, needsRelativeFormatting: true) @@ -1082,18 +1178,18 @@ final class DateFormatterTests: QuickSpec { context("\(deDELocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: deDELocale, timeZone: gmtZone, needsRelativeFormatting: true) expect(result) == "" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: deDELocale, timeZone: gmtZone, needsRelativeFormatting: true) @@ -1157,18 +1253,18 @@ final class DateFormatterTests: QuickSpec { context("\(enUSLocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: enUSLocale, timeZone: gmtZone, needsRelativeFormatting: true) expect(result) == "" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: enUSLocale, timeZone: gmtZone, needsRelativeFormatting: true) @@ -1232,18 +1328,18 @@ final class DateFormatterTests: QuickSpec { context("\(frFRLocale.identifier)") { - it("ShortWeekdayName") { + it("ShortWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .shortWeekdayName, + format: .shortWeekday, locale: frFRLocale, timeZone: gmtZone, needsRelativeFormatting: true) expect(result) == "" } - it("LongWeekdayName") { + it("LongWeekday") { let result = SHDateFormatter.shared.string(from: testDate, - format: .longWeekdayName, + format: .longWeekday, locale: frFRLocale, timeZone: gmtZone, needsRelativeFormatting: true) diff --git a/source/Classes/SHDateFormatter.swift b/source/Classes/SHDateFormatter.swift index 41e1354..b0604ef 100644 --- a/source/Classes/SHDateFormatter.swift +++ b/source/Classes/SHDateFormatter.swift @@ -5,10 +5,12 @@ enum Calendars { } public enum SHDateFormat: String { - case shortWeekdayName = "EEE" - case longWeekdayName = "EEEE" - case shortMonthName = "MMM" - case longMonthName = "MMMM" + case shortWeekday = "EEE" + case longWeekday = "EEEE" + case shortMonth = "MMM" + case longMonth = "MMMM" + case longYear = "yyyy" + case shortYear = "yy" case shortTimeNoDate case shortTimeMediumDate case noTimeShortDateNoYear = "d.M." @@ -46,8 +48,10 @@ public struct SHDateFormatter { let locale = SHDateFormatter.formatter.locale switch format { - case .shortWeekdayName, .longWeekdayName, .shortMonthName, .longMonthName, .noTimeShortDateNoYear: - SHDateFormatter.formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: format.rawValue, options: 0, locale: locale) + case .shortWeekday, .longWeekday, .shortMonth, .longMonth, .longYear, .shortYear, .noTimeShortDateNoYear: + SHDateFormatter.formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: format.rawValue, + options: 0, + locale: locale) case .shortTimeNoDate: SHDateFormatter.formatter.timeStyle = .short