5

I keep dates and times in my CoreData store and need to accurately present to users the time as originally entered. The problem is that if they entered 4:00 on the East Coast, then later look at the recorded time on the West Coast, it appears as 1:00, since the iPhone translates universal times to local times. I need it to show 4:00 -- probably 4:00 (+3h00).

Before I go and restructure my Core Data model, I want to be sure there's no way to derive the NSTimeZone that was active at creation-time from a stored NSDate object. Is there? If not, any recommendation how best to capture NSTimeZone at creation time myself? An NSNumber representing the creation-time NSTimeZone time difference from GMT?

Thanks.

ed94133
  • 1,477
  • 2
  • 19
  • 40

1 Answers1

8

As you've discovered, NSDate represents a universal point in time and does not include specific time zone information.

I'd suggest instead of storing the offset, store the NSTimeZone's name property. Then you can use +[NSTimeZone timeZoneWithName:] to get the time zone and thence convert it to local time. This gives you maximum flexibility in how you display the information to the user.

grahamparks
  • 16,130
  • 5
  • 49
  • 43
  • +1 for not trying to futz about with `NSDate`! Storing time zone information separately is definitely the way to go. – Nick Moore Jan 18 '11 at 08:36
  • 1
    In most cases you can also store the date as GMT and do conversions to the users local timezone when you need to display information. – Chris Wagner Jan 18 '11 at 16:48
  • 1
    Thanks! This seems like a good approach, except that I don't think the name property captures Daylight Savings Time offsets. Probably a real edge case, but isn't it possible I could use the name of a European time zone as the basis for conversion to an American one, and get the conversion wrong because the time zone offset was abnormal during that week or two of unsync'ed DST? Maybe it's best to capture the offset from GMT at entry-time after all? I suppose I could also store the time zone name or abbreviation just for user info. – ed94133 Jan 19 '11 at 08:17
  • @ed94133 It should deal with all that stuff automatically. – grahamparks Jan 19 '11 at 11:46