4

I'm trying to get an NSDate object that has 21:00 as the local time - don't care about what day. I'm scratching my head at this very strange result:

NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setHour:21];
[components setMinute:0];
[components setSecond:0];

NSDate *date = [calendar dateFromComponents:components];
NSLog(@"%@", date);

The result is 0001-01-02 04:52:58 +0000

I have no idea why. The current time is 17:34 PST, but the result doesn't change with the local time.

If I adjust the setMinute and setSecond lines to

[components setMinute:7];
[components setSecond:2];

I get 0001-01-02 05:00:00 +0000, which is correct (21:00 PST).

apb
  • 3,270
  • 3
  • 29
  • 23
  • 1
    What do you get if you set the year to 2012? – bneely Nov 28 '12 at 01:43
  • Somehow your timezone has gotten set weird. Or else NSDateFormatter is attempting to account for 2000 years of leap seconds. – Hot Licks Nov 28 '12 at 01:54
  • @bneely that fixed it! Feel free to answer and I'll accept. – apb Nov 28 '12 at 01:57
  • Oddly the switchover year for acquiring an extra 7 minutes and 2 seconds appears to be 1884 — i.e. 1883 gives 04:52:58 and 1884 gives 05:00:00. Can anyone spot any significance in that? (EDIT: no, I've figured it out: that's when PST was standardised) – Tommy Nov 28 '12 at 02:00
  • it's always the timezone – Daij-Djan Nov 28 '12 at 02:06

1 Answers1

10

The problem is that railroad time wasn't implemented until November 18, 1883. You're neglecting to set a year so you're getting a date before that. Prior to the implementation of railroad time, the US time zones weren't exact hour differences from GMT. I'm not sure exactly what time zone Apple selects for you but whichever it was seems to have been adjusted by 7 minutes and 2 seconds upon the move to PST in 1883.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • They may have selected San Francisco solar time or some such. – Hot Licks Nov 28 '12 at 02:07
  • Having had a quick look, one supplies a closest city in Date & Time. My CA-bought Mac is set to Cupertino and I don't recall having set it myself at any time. So I guess they'll either have nominated Cupertino time to be representative of pre-railroad PST or, possibly, they'll actually be using that closest city. – Tommy Nov 28 '12 at 02:09
  • +1 - nice explanation. I knew that year and timezone fixed it but wasn't sure exactly why. Good to know. – bryanmac Nov 28 '12 at 02:15
  • Of course, there's no way they've reproduced the convoluted history of timekeeping in the US, even going back just 20 years or so. Especially back prior to maybe 1965 cities would decide individually whether they were going on DST or not, and some would adopt entirely different timezones in order to "keep time" with a larger neighbor. – Hot Licks Nov 28 '12 at 02:31
  • It would be interesting to try it with other locations. – Hot Licks Nov 28 '12 at 02:32
  • I wrote a program and confirmed it switches on 11/18/1883 and is specific to PST (setting eastern doesn't have the issue) – bryanmac Nov 28 '12 at 02:54