0

I have an NS date stored that is logged as

2013-03-15 00:00:01 +0000

I presume that the +0000 is UTC time. However, when I try

NSCalendar* theCalendar = [NSCalendar currentCalendar];

unsigned theUnitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

NSDateComponents* compsForDate = [theCalendar components:theUnitFlags fromDate:compDate];

The comps are logged as:

<NSDateComponents: 0xc0537b0>
    Calendar Year: 2013
    Month: 3
    Leap month: no
    Day: 14
    Hour: 20
    Minute: 0
    Second: 1
    Weekday: 5

I would have assumed that the day would be 15 and the hour would be 0, since I didn't think there was a timezone associated with this date. Can anyone tell me where I am going wrong?

SAHM
  • 4,078
  • 7
  • 41
  • 77

2 Answers2

4

There is a time zone associated with that date-UTC. There is also a time zone associated with your calendar; probably the time zone from your locale settings (that's the default anyway). The calendar is giving you the date components that arise from interpreting that date in its time zone.

  • While this makes sense one one level, it is also confusing - shouldn't this give the date components in UTC? How does it know if the date was stored in the current time zone to begin with? Is there any way to get the NSDateComponents in UTC? I purposefully stored them in UTC so that I could get the proper components, and THEN translate them into the desired time zone. – SAHM Mar 16 '13 at 18:49
  • Thanks, I don't know what I was thinking – SAHM Mar 16 '13 at 19:52
1

The +0000 in the time string indicate the hour offset of UTC (Greenwich, England). The resultant time components you've included in the post are the time with your local time zone factored in. Based on the comps, you local time zone is -4 hours away from UTC.

Kevin.

Kevin T.
  • 13
  • 1