Get a consistent date format from NSDate? - objective-c

I'm using NSDates as keys for an NSDictionary, but for key value coding, I need a string. Importantly, I need it to not change representation across OSes or locales. When I show it to users, I'll use NSDateFormatter, of course, but internally it must always be the same. What's the best way get a consistent format as a string?

Use NSDateFormatter and specify the time zone as GMT and the local as "en_US_POSIX".
From Apple's: Data Formatting Guide
If you're working with fixed-format dates, you should first set the
locale of the date formatter to something appropriate for your fixed
format. In most cases the best locale to choose is en_US_POSIX, a
locale that's specifically designed to yield US English results
regardless of both user and system preferences.
Another option is to convert a NSDate to a NSTimeInterval with a method such as timeIntervalSince1970 and then to a hexascii value to use as a key. NSDate returns a time value relative to an absolute reference date—the first instant of 1 January 2001, GMT.

Related

How to show nsdate without converting to local time zone?

I want to present a date to the user in the dates own timezone. So without it being converted to the users timezone.
Basically what happens: I retrieve a string '2015-04-01T15:35:00-04:00' from the backend, and I convert it to a NSDate. Later on I want to show the user the time: 15:35.
But NSDateFormatter converts the date to my system time zone (+02:00) which results in showing 21:35.
I've searched all over the internet to find out I can skip this convert, but I can't find anything.
What do I miss?
Help is really appreciated.
You will have to save the original timezone in addition to the NSDate representation and use it when you want to present the time in the original timezone.
NSDate keeps the date/time relative to GMT (UTC).

Is there a way to get the user-preference Time Zone value from the Dwolla API?

The Dwolla transaction "Date" field apparently uses this value for generating a timestamp, but I don't see any way to get the "Time Zone" preference from the API?
Zooming out, I'm trying to get the definitive/comparable date for a transaction, but I need to know the timezone of the date string.
While this method doesn't state what the time zone is, others like this one make it clear that the dates are in UTC, so I would guess that all dates are in UTC throughout the API.
The only thing I don't like about their API is they are showing example dates like:
"Date": "8/31/2011 10:19:09 AM"
It's not very smart of them to use M/D/YYYY format, nor to use a 12-hour clock. This is better represented by "2011-08-31T10:19:09Z", which is in ISO format and indicates UTC. If you ever get a chance to speak with them, you should recommend they use this format (ISO-8601).
Just be aware of this when you are parsing it, since the format they are using may or may not be the correct format for your current culture.
Dwolla are making the change to the ISO timestamp on August 4th 2014
This change will be rolled out in 60 days from today, on 9:00 AM CT August 4th, 2014. If you need help making adjustments to your code to parse the new datetime format, let us know and we'll be glad to lend a hand.
https://discuss.dwolla.com/t/api-update-new-timestamp-format-for-clearingdate-parameter/401

'Normalized' NSDateFormatter

I have to parse strings like 2012-06-06T18:00:00 or 2012-06-06 without any timezone information. When I parse this the date formatter uses the local timezone and takes DST into account.
But what the date actually means is that this event occurs on 2012-06-06 not depending on the timezone, but on 2012-06-06T00:00:00 in every timezone.
Should I store the year/month/day values separately?
The same time in different time zones will correspond to different absolute times (which are represented by NSDate). So one NSDate object cannot represent what you are trying to represent. You will need one NSDate per time zone you are interested in.
You can create an NSDateComponent instance with your string. Then use NSCalender with the locale (time zone) you want to transform those date components into an NSDate (a point in time).

Most efficient way to convert a NSDate object with a given time to one with the same time and the current date

I'm currently creating a scheduling application that deals with schools schedules. Each schedule has an array of period objects that contains both a start and an end time. Currently I have these times stored as NSDate objects, where only the time is really relevant and each NSDate object has some arbitrary date associated with it that is largely insignificant to my application (it's whatever the UIDatePicker assigns to it, but I only use the TimePickerMode). I do a number of things with these dates. First, I use an NSDateFormatter to output a string representation of the time for each period in a UITableView. I then also attempt to find the current period in the day by comparing the current time, fetched by [NSDate date], to the start and end time. But I am having trouble deciding the best way to do this.
Currently, I use an NSDateFormatter to convert the start and end times to strings, and then back to dates that now have a date the same as today's date. Then after that I can do my date comparisons using - (NSComparisonResult)compare: comparing the start and end dates to the current date. However, I could see this as being very inefficient.
Other possible ways could be to convert each NSDate to NSDateComponents, then use NSCalendar to convert that back into an NSDate with the current date and the same original time.
Or, I could take a different approach and ditch the NSDate storage technique altogether. Instead I could store each start and end time as NSDateComponents, then it would be simple enough to output a formatted version of the time for the UITableView, and then I could convert the date obtained by [NSDate date] to NSDateComponents to be used for logical comparisons to obtain the current period.
I see three or four different ways of going about this, and actually after talking it out I I'm pretty confident the final way I discussed would be the most efficient as it requires the least amount of runtime conversions between different types of date objects. I'm just looking for other people's inputs.
The problem with using NSDate to store a time-of-day is that its value is dependent on the time zone. If the time zone or DST changes while your app is running in the foreground or background, then all of your times will suddenly be wrong. Ditto if you actually save NSDate values to disk. (One option is to set your app's default time zone to UTC, but then your app's idea of "now" will be incorrect to the user).
I would store the time separately and implement methods to convert to and from NSDate. You could store the time using NSDateComponents, or you could just store it as a single integer (say, minutes past midnight) which makes it trivial to compare two times.
When converting minutes to and from NSDate you use NSDateComponents, and you need to make sure that your UIDatePickers, NSCalendars, and NSDateFormatters are using the same time zone, which by default is the local time zone.
It seems like you're worried about the overhead that NSDateFormatter currently levies on your program to synchronize the day of your periods and the current date. But really, you don't care about the date at all, you just want the time since the beginning of the day.
Why not cut out the middleman? Store the start and end times as NSTimeIntervals, which are really just doubles. Mod the start and end times by 86,400, the seconds in a day, in order to distill the time down to simply the time in seconds after the start of a new day. You don't really care what day it represents, except that that day is represented as second 0.
Then whenever you pull the current NSDate, mod its NSTimeInterval by 86,400 to obtain the time in seconds and compare it against the stored period values. The only object conversion involved in the whole process would be using the primitive timeIntervalSinceReferenceDate. The rest are simple mod and comparison operators, and you get to store your period numbers as simple NSTimeIntervals.

Adding two NSDate

I've got two NSDate, date and time. I'd like to add them in such a manner that I get the date from date and time from time. Any suggestions on how I do this?
Cheers
Nik
If I got you right NSDates -dateByAddingTimeInterval: together with -timeIntervalSinceDate: are what you looking for.
hth
–f
Use NSCalendars components:fromDate: to get the components of the two dates.
Then reassemble them as needed using dateFromComponents:.
I've got two NSDate, date and time.
Sounds like you're going about it wrong.
An NSDate represents a specific moment in time (X seconds since the epoch). An NSDate is not simply “x o'clock” or “this date on the calendar”, and you shouldn't attempt to combine them as if they were because effects such as DST may make your computation wrong (in some time zones, some dates have two 1:00 hours, and some have no 2:00 hours).
Consider using an NSDatePicker or UIDatePicker (as appropriate) to let the user enter the date and time from a single place. Not only is this easier for you to do, it'll also give more correct results.
If you're reading the two pieces separately from a file or similar source, and you don't control the format (so you can't order the generating side to emit dates with their times in one value), you'll need to do one of two things:
If possible, combine the two strings. For example, an ISO 8601 date (e.g., “2010-05-10”) and an ISO 8601 time (e.g., “23:20:19-0700”) can be concatenated with a ‘T’ between them to form a valid, fully-specified ISO 8601 date (“2010-05-10T23:20:19-0700”). You can then pass this single string to a properly-configured NSDateFormatter.
If, for some reason, you can't combine the strings meaningfully, parse them yourself and fill out a single NSDateComponents object yourself.
This will not be pleasant, but correctness is important, and a bug (incorrect date parsing) that only happens in two hours out of every year for only some of your users will be maddening.
The goal is to produce exactly one NSDate object that completely describes the date and time in question. This is the only way to ensure that, in all circumstances, you get the correct NSDate value.