Question

I'm importing a number of references dates from a remote source. The dates represent times of day on an arbitrary day of the year (in this case 01/01/2000). They are supplied in as Unix timestamps. For example 946717200 represents 9am. I'm also getting the timezone for each location.

I'm using the following code to translate these times into real dates with the correct offset from UTC.

-(NSDate *)dateWithInterval:(NSTimeInterval)interval referenceDate:(NSDate *)aRealDate timezone:(NSTimeZone *)aTimeZone
{
  NSDate *time = [NSDate dateWithTimeIntervalSince1970:interval];
  NSDateComponents *components = [[NSDateComponents alloc] init];

  //Set the timezone
  [components setTimeZone:aTimeZone];

  //I'm using Erica Sadun's NSDate+utilities category on NSDate to provide the shorthand methods
  [components setDay:[aRealDate day]];
  [components setMonth:[aRealDate month]];
  [components setYear:[aRealDate year]];
  [components setHour:[time hour]];
  [components setMinute:[time minute]];
  [components setSecond:[time seconds]];

  return [[NSCalendar currentCalendar] dateFromComponents:components];
}

For example With the given input of 946717200, the current date, and America/Los_Angeles timezone, I'm given a date 2013-10-07 16:00:00 +0000, but when I format it using the the NSDateFormatter, with the timezone set to America/Los_Angeles, and the system set to the same timezone, this appears as 2013-10-07T01:00:00-0700

What am i not doing correctly?

Was it helpful?

Solution

The Problem was NSDate-utilities category. It completely ignores the timezone modifier of a date and always creates it's with taking the timezone in to account. By using my own set of date components and setting the timezone property the issue went away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top