Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store dates without times in Core Data

I've been trying to find a sensible way of storing daily data using Core Data on the iPhone.

My app receives data in csv format, with a date but no time:

date, cycles
2009-08-01, 123
2009-08-02, 234
2009-08-03, 345
2009-08-04, 456

When this data is stored, there should only be one record per day. I figured the best thing to do was create an NSDate to be stored, but strip out the time & time zone data.

I can easily create an NSDate without hours, minutes or seconds using either NSDateComponents or an NSDateFormatter. However even when I set the time zone explicitly to UTC or to zero seconds from GMT, outputting a created date with NSLog() always has my local timezone like:

2009-07-29 00:00:00 +0100

Does anyone know of a better way to make NSDates without time components? Or perhaps a better way of storing the dates?

like image 900
Ben Avatar asked Aug 05 '09 09:08

Ben


3 Answers

A good programming rule of thumb is to always store dates in UTC. It doesn't matter whether you use Core Data or not; you'll still have to do some work because Apple's date classes pretty much suck.

Dates are represented internally as a number of seconds since a reference date which is, I believe, 1 January 2001 00:00:00 (although the actual reference date isn't very important). Point is, NSDate objects are always natively in UTC. If the dates you're getting in your CSV file are local, you'll need to do something like this to get the UTC time:

NSDate *UTCDate = [localDate addTimeInterval:-[[NSTimeZone localTimeZone] secondsFromGMT]];

Then, I'd set the time to 00:00:00. Now you're saving the date, at midnight, in UTC. For presentation purposes, you will use an NSDateFormatter configured with the time zone of your choice (the system time zone is the default if you don't specify one) to display those dates.

Time zones don't really matter when you're just dealing with dates, though. As long as you make sure to set the time zone on your NSDateFormatter to UTC, you'll always show the same date, no matter what time zone the user has selected on her device.

If you don't like this solution, you can always store your dates in an alternative format. You could use a double or int to store the date in some custom format (e.g. the number of days since some reference date), or you could even roll your own class to model the date exactly the way you want and store it as an NSData object. As long as the class implements NSCoding, you can serialize it to an NSData object in Core Data. You just need to set the attribute type in Core Data to "Transformable".

You have a ton of options here, and none of them involve the effort of writing your own SQLite queries and databases.

like image 95
Alex Avatar answered Nov 11 '22 18:11

Alex


An NSDate doesn't have a time zone. NSLog uses your local time zone; it says +0100 because that's where you are.

like image 29
Peter Hosey Avatar answered Nov 11 '22 18:11

Peter Hosey


The NSDate object will always incorporate time date - to quote the docs it "represent[s] a single point in time" and does so by storing a time value since its reference date (the start of 1 January 2001 at GMT, again according to the docs). Therefore you cannot have an NSDate that is unaware of time of day.

Rather than try and store dates your own way, I would still use NSDate in your model and consider adding a pair of methods to your entity class, one of which will do what you described above, setting the NSDate to 00:00:00 on a given day. The other could return just the date from an NSDate in your preferred format. These would then use the Core Data-generated getter and setter to access the NSDate property.

By still using NSDate you are using a class that Core Data can work with natively, meaning you can still use predicates easily for filtering, or sort your fetched results by date without having to think too hard about it.

like image 27
marramgrass Avatar answered Nov 11 '22 17:11

marramgrass