• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSCalendar date calculation anomaly
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSCalendar date calculation anomaly


  • Subject: NSCalendar date calculation anomaly
  • From: Scott Ribe <email@hidden>
  • Date: Tue, 27 Apr 2010 21:45:07 -0600

I have an int representing the number of days since 1/1/2001, and wish to get an NSDate representing that date in the system's local time zone:

NSDateComponents *dc = [[[NSDateComponents alloc] init] autorelease];
[dc setDay: numdays];
NSDate * cd = [[NSCalendar currentCalendar] dateByAddingComponents: dc toDate: [NSDate dateWithString: @"2001-01-01"] options: 0];
NSLog( @"date 1: %@", [cd description] );
[dc setDay: 0];
[dc setSecond: -[[NSTimeZone systemTimeZone] secondsFromGMTForDate: cd]];
cd = [[NSCalendar currentCalendar] dateByAddingComponents: dc toDate: cd options: 0];
NSLog( @"date 2: %@", [cd description] );

If for example numdays is the number of days from 1/1/2001 to 5/4/2010, then the output is this:

2010-04-27 21:33:55.803 PedCard[95387:a0f] date 1: 2010-05-03 17:00:00 -0600
2010-04-27 21:33:55.804 PedCard[95387:a0f] date 2: 2010-05-03 23:00:00 -0600

And, BTW, my current time zone is MDT, so -0600 is not correct, it should be -0700. So what's going on? I would expect those dates to be:

2010-05-03 17:00:00 -0700
2010-05-04 00:00:00 -0700

It seems as though some calls are using one time zone and some are using another??? I do not do anything in my app to set a local or default time zone, and I have not changed the time zone in my system preferences in a very long time. (Multiple reboots ago.) Add to that, the code did produce the correct date a few times earlier tonight... Also, it doesn't seem to make a difference whether I use systemTimeZone or localTimeZone.

OS X 10.6.3, Xcode 3.2.2

--
Scott Ribe
email@hidden
http://www.elevated-dev.com/
(303) 722-0567 voice




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: NSCalendar date calculation anomaly
      • From: glenn andreas <email@hidden>
    • Re: NSCalendar date calculation anomaly
      • From: Kubernan <email@hidden>
    • Re: NSCalendar date calculation anomaly
      • From: Kyle Sluder <email@hidden>
    • Re: NSCalendar date calculation anomaly
      • From: Scott Ribe <email@hidden>
  • Prev by Date: Re: white screen windows at first
  • Next by Date: Re: NSCalendar date calculation anomaly
  • Previous by thread: Re: Selecting an object based on the value of one of its attributes
  • Next by thread: Re: NSCalendar date calculation anomaly
  • Index(es):
    • Date
    • Thread