views:

104

answers:

2

I hav a string e.g. 2010-09-24. Now I want this string read as it is in an other timezone as I am. So I create an NSDateFormatter and set it's time zone e.g. to Europe/Berlin. What I get back is an NSDate object adjusted (2010-09-23 18:00:00) to my local time zone (e.g. America/New_York).

But I want an NSDate in the time zone Europe/Berlin with the time value 2010-09-24 00:00:00. How can I do this?

I tried to set the defaultTimeZone to Europe/Berlin during the [dateFormatter dateFromString:@"2010-09-24"] but when I change back the timeZone to my local time zone the NSDate object changes too.

+6  A: 

I think answer to your question is in stack over it self http://stackoverflow.com/questions/1980903/handling-time-zones-in-cocoa

Ravi shankar
+1  A: 

NSDate is just a CFDate, which represents a CFAbsoluteTime, which is just POSIX time (UTC-seconds-since-epoch) with a different epoch (2001 instead of 1970). (I'm pretty sure that CFAbsoluteTimeGetCurrent() just calls gettimeofday(); it only seems to have microsecond resolution.)

Or, in short: NSDate is a timestamp, not a calendar date. If you want to represent a calendar date/time, use NSDateComponents. In either case, you'll need to specify a calendar (probably Gregorian) and a time zone; see NSCalendar.

The difference depends approximately on what happens when you change time zone offset:

  • "Transit of Venus Party at my house" happens simultaneously everywhere, so use universal time (NSDate)
  • "Have Lunch" happens at noonish local time, so use NSDateComponents.

If you assume that the time zone offset of an event doesn't change, it's convenient to specify an NSDate and NSTimeZone name. Many calendar apps do this, and (almost) everything goes wrong when the US decides to change DST rules.

tc.