Skip to content

Commit

Permalink
Merge pull request #5 from Blackjacx/improve_robustness_of_no_time_sh…
Browse files Browse the repository at this point in the history
…ort_date

Improve Robustness By Using Apple's Default Formatting Styles
  • Loading branch information
Blackjacx authored Feb 26, 2018
2 parents ba38e74 + 2cd6ddb commit 83b0dd2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## [Unreleased]
* [#5](https://github.com/Blackjacx/SHDateFormatter/pull/5): Improve Robustness By Using Apple's Default Formatting Styles - [@blackjacx](https://github.com/blackjacx).

## [1.0.2] - 2017-10-10
### Changed
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ This framework supports encoding as well as decoding many different date and tim
* `longWeekdayName` => Sunday, Monday, Tuesday, ...
* `shortTimeNoDate` => 12:00 PM, 1:00 AM
* `noTimeShortDateNoYear` => 12/31
* `noTimeShortDate` => 12/31/2016
* `noTimeLongDate` => December 31, 2016
* `noTimeShortDate` => 12/02/2018
* `noTimeLongDate` => December 2, 2018
* `noTimeRelativeDate` => Today, Tomorrow, ...
* `ISO8601` => "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"

Expand Down
21 changes: 13 additions & 8 deletions Source/SHDateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ struct Calendars {
public enum SHDateFormat: String {
case shortWeekdayName = "EEE"
case longWeekdayName = "EEEE"
case shortTimeNoDate = "shortTimeNoDate"
case shortTimeNoDate
case noTimeShortDateNoYear = "d.M."
case noTimeShortDate = "d.M.yy"
case noTimeLongDate = "d. MMMM yyyy"
case noTimeRelativeDate = "noTimeRelativeDate"
case noTimeShortDate
case noTimeLongDate
case noTimeRelativeDate
/**
* The only correct date format for client/server communication.
* http://oleb.net/blog/2011/11/working-with-date-and-time-in-cocoa-part-2/
Expand Down Expand Up @@ -48,9 +48,7 @@ public struct SHDateFormatter {
switch format {
case .shortWeekdayName: fallthrough
case .longWeekdayName: fallthrough
case .noTimeShortDateNoYear: fallthrough
case .noTimeShortDate: fallthrough
case .noTimeLongDate:
case .noTimeShortDateNoYear:
SHDateFormatter.formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: format.rawValue, options: 0, locale: locale)

case .ISO8601:
Expand All @@ -62,11 +60,18 @@ public struct SHDateFormatter {
SHDateFormatter.formatter.timeStyle = .short
SHDateFormatter.formatter.dateStyle = .none

case .noTimeShortDate:
SHDateFormatter.formatter.timeStyle = .none
SHDateFormatter.formatter.dateStyle = .short

case .noTimeLongDate:
SHDateFormatter.formatter.timeStyle = .none
SHDateFormatter.formatter.dateStyle = .long

case .noTimeRelativeDate:
SHDateFormatter.formatter.doesRelativeDateFormatting = true
SHDateFormatter.formatter.timeStyle = .none
SHDateFormatter.formatter.dateStyle = .long

}
}

Expand Down
8 changes: 4 additions & 4 deletions Tests/DateFormatterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class DateFormatterSpec: QuickSpec {

it("NoTimeShortDate") {
let result = SHDateFormatter.shared.stringFromDate(date: testDate, format: .noTimeShortDate, locale: deDE_Locale, timeZone: timeZone)
expect(result) == "1.1.00"
expect(result) == "01.01.00"
}

it("NoTimeLongDate") {
Expand Down Expand Up @@ -170,7 +170,7 @@ class DateFormatterSpec: QuickSpec {

it("NoTimeShortDate") {
let result = SHDateFormatter.shared.stringFromDate(date: testDate, format: .noTimeShortDate, locale: frFR_Locale, timeZone: timeZone)
expect(result) == "01/01/00"
expect(result) == "01/01/2000"
}

it("NoTimeLongDate") {
Expand Down Expand Up @@ -218,7 +218,7 @@ class DateFormatterSpec: QuickSpec {

it("NoTimeShortDate") {
let result = SHDateFormatter.shared.stringFromDate(date: testDate, format: .noTimeShortDate, locale: deDE_Locale, timeZone: timeZone)
expect(result) == "2.1.00"
expect(result) == "02.01.00"
}

it("NoTimeLongDate") {
Expand Down Expand Up @@ -304,7 +304,7 @@ class DateFormatterSpec: QuickSpec {

it("NoTimeShortDate") {
let result = SHDateFormatter.shared.stringFromDate(date: testDate, format: .noTimeShortDate, locale: frFR_Locale, timeZone: timeZone)
expect(result) == "02/01/00"
expect(result) == "02/01/2000"
}

it("NoTimeLongDate") {
Expand Down

0 comments on commit 83b0dd2

Please sign in to comment.