NSCalendarDate saving and loading to get the same date-time

  • Hi,
    I have a NSDictionary with a couple of NSCalendarDate objects as
    values, I save and load the dictionary to a plist file (using
    NSDictionary writeToFile... and NSDictionary
    dictionaryWithContentsOfURL... methods).

    Say I set the date-time to Oct 9th, 2006 10:30 AM (My timezone is set
    to New York in System Preferences) and save the dictionary. I change
    my timezone to San Francisco (In System Preferences) and  load the
    dictionary the date-time is Oct 9th, 2006 7:30 AM.

    Is there a way to make sure the date-time is the same value (same
    mm-dd-yyyy hh:mm:ss ) after save and load even if the timezone is
    changed.

    I know that the plist only saves date in NSDate, I tried this with
    NSDate values and I get the same result. I also tried creating the
    NSCalendarValues with [NSTimeZone  localTimeZone] but I still get the
    same result.

    Interestingly if you save the value [NSDate
    timeIntervalSinceReferenceDate] (which is NSTimeInterval == double)
    and recreate the NSDate after load using [NSDate
    dateWithTimeIntervalSinceRefenceDate:...] the date-time you get is not
    the same meaning it is adjusted based on the timezone. I thought the
    value should be same because you are giving an absolute elapsed time
    since reference date.

    I read all the earlier posts and documentation on NSCalendarDate and
    NSDate but I didn't find anything related to this case. Any help is
    greatly appreciated.

    Regards
    Sarat
  • On Oct 9, 2006, at 1:28 PM, Sarat Kongara wrote:

    > Is there a way to make sure the date-time is the same value (same
    > mm-dd-yyyy hh:mm:ss ) after save and load even if the timezone is
    > changed.

    No. But you could save the time zone name string as a different key.

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • Am 09.10.2006 um 21:28 schrieb Sarat Kongara:
    > I have a NSDictionary with a couple of NSCalendarDate objects as
    > values, I save and load the dictionary to a plist file (using
    > NSDictionary writeToFile... and NSDictionary
    > dictionaryWithContentsOfURL... methods).
    >
    > Say I set the date-time to Oct 9th, 2006 10:30 AM (My timezone is set
    > to New York in System Preferences) and save the dictionary. I change
    > my timezone to San Francisco (In System Preferences) and  load the
    > dictionary the date-time is Oct 9th, 2006 7:30 AM.
    >
    > Is there a way to make sure the date-time is the same value (same
    > mm-dd-yyyy hh:mm:ss ) after save and load even if the timezone is
    > changed.

      Save the time zone relative to which you specified your time and
    restore it later, or query the OS for the time difference and save
    that along with the date so you can adjust for it?

      It *is* the exact same time, just expressed in another time zone
    (i.e. when it's 2:30 in the US, it's about 11:30 in central Europe).
    The time zone doesn't change the date, it changes how it is
    displayed. So none of the things you observed is in any way surprising.

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
  • Thanks Nick, I wasted 2 days on trying to figure this out. I was
    thinking about either saving the TimeZone or saving the Date-Time
    components as an array (mm-dd-yyyy hh:mm:ss) instead.

    Some part of my application data is saved using Core Data. What if the
    date is part of a NSManagedObject, I used the date type in the
    NSManagedObject Model and on load and save I see the same issue. Any
    suggestions?

    Regards
    Sarat

    On 10/9/06, Nick Zitzmann <nick...> wrote:
    >
    > On Oct 9, 2006, at 1:28 PM, Sarat Kongara wrote:
    >
    >> Is there a way to make sure the date-time is the same value (same
    >> mm-dd-yyyy hh:mm:ss ) after save and load even if the timezone is
    >> changed.
    >
    > No. But you could save the time zone name string as a different key.
    >
    > Nick Zitzmann
    > <http://www.chronosnet.com/>
    >
    >
    >
  • > It *is* the exact same time, just expressed in another time zone
    > (i.e. when it's 2:30 in the US, it's about 11:30 in central Europe).
    > The time zone doesn't change the date, it changes how it is
    > displayed. So none of the things you observed is in any way surprising.

    Yes, this I understand. But I was wondering why saving the value from
    [date timeIntervalSinceReferenceDate] and recreating a new date using
    [NSDate dateWithTimeIntervalSinceReferenceDate] returns different
    date-time when different timezones are set during Save and Load.
    Shouldn't the date-time be the same for all timezones when you are
    calculating it from a absolute elapsed time from a reference date?

    On 10/9/06, Uli Kusterer <witness.of.teachtext...> wrote:
    >
    > Am 09.10.2006 um 21:28 schrieb Sarat Kongara:
    >> I have a NSDictionary with a couple of NSCalendarDate objects as
    >> values, I save and load the dictionary to a plist file (using
    >> NSDictionary writeToFile... and NSDictionary
    >> dictionaryWithContentsOfURL... methods).
    >>
    >> Say I set the date-time to Oct 9th, 2006 10:30 AM (My timezone is set
    >> to New York in System Preferences) and save the dictionary. I change
    >> my timezone to San Francisco (In System Preferences) and  load the
    >> dictionary the date-time is Oct 9th, 2006 7:30 AM.
    >>
    >> Is there a way to make sure the date-time is the same value (same
    >> mm-dd-yyyy hh:mm:ss ) after save and load even if the timezone is
    >> changed.
    >
    > Save the time zone relative to which you specified your time and
    > restore it later, or query the OS for the time difference and save
    > that along with the date so you can adjust for it?
    >
    > It *is* the exact same time, just expressed in another time zone
    > (i.e. when it's 2:30 in the US, it's about 11:30 in central Europe).
    > The time zone doesn't change the date, it changes how it is
    > displayed. So none of the things you observed is in any way surprising.
    >
    > Cheers,
    > -- M. Uli Kusterer
    > http://www.zathras.de
    >
    >
    >
  • On 10/9/06, Sarat Kongara <saratkongara...> wrote:
    >> It *is* the exact same time, just expressed in another time zone
    >> (i.e. when it's 2:30 in the US, it's about 11:30 in central Europe).
    >> The time zone doesn't change the date, it changes how it is
    >> displayed. So none of the things you observed is in any way surprising.
    >
    > Yes, this I understand. But I was wondering why saving the value from
    > [date timeIntervalSinceReferenceDate] and recreating a new date using
    > [NSDate dateWithTimeIntervalSinceReferenceDate] returns different
    > date-time when different timezones are set during Save and Load.
    > Shouldn't the date-time be the same for all timezones when you are
    > calculating it from a absolute elapsed time from a reference date?

    When you ask NSDate for date/time information it is giving you a time
    zone adjusted interpretation of itself.

    Time interval since reference date is defined to be "The interval
    between the system's absolute reference date (the first instant of 1
    January 2001, GMT) and the current date and time". It is expressed in
    terms of GMT but once you create an NSDate from this value then that
    NSDate instant will return you time information relative to the
    current time zone of the system (unless you use API that supplies time
    zone).

    -Shawn
  • Thanks Shawn. I understand the NSDate behaviour better now. I guess
    the best option for me is to save the Timezone with the dates so I can
    adjust them on load or save the components individually
    (mm,dd,yyyy,hh,mm,ss).

    Regards
    Sarat

    On 10/9/06, Shawn Erickson <shawnce...> wrote:
    > On 10/9/06, Sarat Kongara <saratkongara...> wrote:
    >>> It *is* the exact same time, just expressed in another time zone
    >>> (i.e. when it's 2:30 in the US, it's about 11:30 in central Europe).
    >>> The time zone doesn't change the date, it changes how it is
    >>> displayed. So none of the things you observed is in any way surprising.
    >>
    >> Yes, this I understand. But I was wondering why saving the value from
    >> [date timeIntervalSinceReferenceDate] and recreating a new date using
    >> [NSDate dateWithTimeIntervalSinceReferenceDate] returns different
    >> date-time when different timezones are set during Save and Load.
    >> Shouldn't the date-time be the same for all timezones when you are
    >> calculating it from a absolute elapsed time from a reference date?
    >
    > When you ask NSDate for date/time information it is giving you a time
    > zone adjusted interpretation of itself.
    >
    > Time interval since reference date is defined to be "The interval
    > between the system's absolute reference date (the first instant of 1
    > January 2001, GMT) and the current date and time". It is expressed in
    > terms of GMT but once you create an NSDate from this value then that
    > NSDate instant will return you time information relative to the
    > current time zone of the system (unless you use API that supplies time
    > zone).
    >
    > -Shawn
    >
  • On Oct 9, 2006, at 2:09 PM, Sarat Kongara wrote:

    > Thanks Shawn. I understand the NSDate behaviour better now. I guess
    > the best option for me is to save the Timezone with the dates so I can
    > adjust them on load or save the components individually
    > (mm,dd,yyyy,hh,mm,ss).

    I still don't understand what your issue is. The NSDate you save is
    the same one you get back when you load what you saved (same in the
    sense that it represents the same exact time). You should have no
    need to adjust anything.

    Can you better describe the problem you are trying to solve?

    -Shawn
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today