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

PM/AM Formatting not working correctly #349

Closed
OliverZiegler opened this issue Nov 30, 2016 · 4 comments
Closed

PM/AM Formatting not working correctly #349

OliverZiegler opened this issue Nov 30, 2016 · 4 comments
Assignees
Labels
Milestone

Comments

@OliverZiegler
Copy link

OliverZiegler commented Nov 30, 2016

When changing your iPhone settings to 12 hour format and trying to get an iso date string, the string is not right.

Trying to parse an PM date the resulting output is an AM date

Printing description of gmtTimeDate: 30.11.2016, 10:00:00 PM GMT

gmtTimeDate.string(format: .iso8601(options: [.withInternetDateTime]))
"2016-11-30T10:00:00Z"

Is there anything I do wrong or is this a bug in SwiftDate.

My recent Settings of Date&Time are:
24-hours: off
automatic timezone: on
Timezone: Berlin

Edit:
Probably this is unexpected behaviour of NSDateFormater as you can see here

@malcommac
Copy link
Owner

Hi,
In order to parse correctly a 12h clock you need to use hh in place of HH for hour.
Based upon your string I suggest you to use a custom formatter:

Below an example:

let str = "30.11.2016, 10:00:00 PM GMT"
let date = try! str.date(format: .custom("dd.MM.yyyy',' hh:mm:ss a ZZZZ")) // 30 nov 2016, 23:00:00 CET

It will return 30 nov 2016, 23:00:00 CET as you need.
More on formatting options are available here

@malcommac malcommac modified the milestone: 4.0.8 Nov 30, 2016
@OliverZiegler
Copy link
Author

OliverZiegler commented Nov 30, 2016

Unfortunately this won't solve my problem.

I need to parse a given date to the right ISO8601 String which won't happen if your device is on 12 hour mode.

If I create a Date with Date() or DateInRegion() and current time for example 10:00 PM or 22:00 the resulting ISO8601 string after formatting will be: 2016-11-30T10:00:00Z which is not the correct ISO8601 string.

Using following code will give me the right ISO8601 String for my date:

let region = Region(tz: TimeZoneName.gmt, cal: CalendarName.current, loc: LocaleName.englishUnitedStatesComputer)
let gmtDate = DateInRegion(absoluteDate:Date(), in: region)
let gmtTimeDate = try! gmtDate.atTime(hour: newValue.hour, minute: newValue.minute, second: newValue.second)
let string = gmtTimeDate.string(format: .iso8601(options: [.withInternetDateTime]))
print(string)

So prints out 2016-11-30T22:00:00Z

But trying to parse this string back to a Date or DateInRegion this won't work, so try "2016-11-30T22:00:00Z".date(format: .iso8601(options: [.withInternetDateTime])) will raise an exception.

As ISO8601 is strictly defined I think this is very unexpected behaviour and should be fixed.

Edit:
FYI:
Using

let region: Region = Region(tz: TimeZoneName.gmt, cal: CalendarName.current, loc: LocaleName.englishUnitedStatesComputer)
let date = try "2016-11-30T22:00:00Z".date(format: .iso8601(options: [.withInternetDateTime]), fromRegion: region)

will work and result in the right Date.
So for parsing ISO8601 strings there should always be used the "en_US_POSIX" Locale I think (and this should be done internally).

@malcommac
Copy link
Owner

malcommac commented Nov 30, 2016

I think you are right, we should set en_US_POSIX manually. Thank you 👍
Added for 4.0.9.

@malcommac malcommac added this to the 4.0.9 milestone Nov 30, 2016
@malcommac malcommac self-assigned this Nov 30, 2016
@malcommac malcommac added bug and removed wontfix labels Nov 30, 2016
@malcommac
Copy link
Owner

Just for reference, this is the official statement from Apple regarding NSDateFormatter and Internet Date&Time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants