0

NSDate value is returned by CloudKit. Which representation will system choose, when I am storing date in Core Data as NSTimeInterval? I know NSTimeInterval stores number of seconds since some specific reference date. But which reference date will be choosen here?

mo.setValue(record.creationDate, forKey: "creationDate")

Latter on I have to compare this date with current date, so I have to convert it back to NSDate, so I have to use one of the NSDate timeIntervalSinceXXX methods.


I have found this in docs:

Dates and Times NSManagedObject represents date attributes using NSDate objects, and stores times internally as an NSTimeInterval value since the reference date (which has a time zone of GMT). Time zones are not explicitly stored—indeed you should always represent a Core Data date attribute in GMT, this way searches are normalized in the database. If you need to preserve the time zone information, you need to store a time zone attribute in your model. This may again require you to create a subclass of NSManagedObject.

I does not say anything what is the reference

János
  • 32,867
  • 38
  • 193
  • 353
  • i'd guess `dateWithTimeIntervalSince1970:` Everything else would just cause problems. – luk2302 Jun 06 '15 at 09:37
  • phew, i did not know about that one ;) i will google for a bit... – luk2302 Jun 06 '15 at 09:40
  • have you seen http://stackoverflow.com/questions/6841579/why-is-an-nsdate-in-a-core-data-managed-object-converted-to-nstimeinterval ? – luk2302 Jun 06 '15 at 09:46
  • and here you probably have your answer: http://stackoverflow.com/questions/10705062/behind-the-scenes-core-data-dates-stored-with-31-year-offset – luk2302 Jun 06 '15 at 09:47
  • doc is not so 'straightforward', 1970 could be also a 'reference date' but I think `stores times internally as an NSTimeInterval value since the reference date` sentence means `timeIntervalSinceReferenceDate` I have to use – János Jun 06 '15 at 09:54

1 Answers1

0

As stated in the documentation and in comments, the internal NSDate reference date is 1 January 2001, which is what is used by Core Data internally when serializing NSDate objects to the database representation.

See Wikipedia's article on epoch. NSDate and CFDate both use the Cocoa epoch time (which dates at least to 1994, in the OpenStep specifications).

Léo Natan
  • 56,823
  • 9
  • 150
  • 195