NSDate incorrect time displayed

Discussion in 'Mac Programming' started by Mark FX, May 9, 2012.

  1. macrumors member

    Mark FX

    Nov 18, 2011
    Recently I used the NSDate class in a project, and found that it is producing
    the correct date but the incorrect time, for example.

    NSDate *now = [NSDate date];
    NSLog(@"%@", now);
    Display incorrect time.

    The output to the log showed the correct date part of the string, but
    displayed the time part as one hour behind the current system time, the
    minutes and seconds where correct, but not the hours.

    My settings in the System preferences are all correct, and when writing an
    Applescript to check the date, this also produced the correct time.

    set theDate to the (current date)
    Displays the correct time.

    even the date shell command displays the correct date & time, but the NSDate method
    above always shows the hours part of the date as one hour behind.

    Any Ideas what causes this.

    Regards Mark
  2. chown33, May 9, 2012
    Last edited: May 9, 2012

    macrumors 603

    Aug 9, 2009
    What is your locale? What is your timezone?

    What locale and timezone does NSDate use by default? (You can look that up in NSDate's class reference doc.)

    A one hour difference sounds like a DST issue. Different locales (countries, states, regions) have different rules for when DST goes into effect. Without knowing your locale and timezone, no one knows what rules are being followed in your case.
  3. thread starter macrumors member

    Mark FX

    Nov 18, 2011
    Sorry for the slow reply.

    I'm in the UK, and time setting preference is for GMT.

    Regards Mark
  4. macrumors 603

    Aug 9, 2009
    Use a specific NSDateFormatter.

    Also read the NSDate class reference doc. The -description method is what %@ uses. Read that. Also read the two other methods whose name starts with "description".

    Do you mean GMT as in UTC (which doesn't do DST), or GMT as in Western European Time with DST adjustment?

    When I asked for locale and timezone, I meant what are your default NSLocale and NSTimeZone objects. Be specific. In particular, is DST in effect or not?
  5. macrumors G5


    Nov 25, 2005
    The time displayed is correct.
    The clock on your wall displays local time, not UTC. Off by one hour.
    Your assumption that NSLog should display the local time is wrong.

    To make this more clear: If you somewhere in the UK, another programmer in New York, and a third programmer in Los Angeles, run this program at exactly the same time, then you will all see identical output. But if you all three look at your watch at the same moment, the time displayed by your watches will be very different.
  6. macrumors 603

    Aug 9, 2009
    When I try it, I see local time:
    #import <Foundation/Foundation.h>
    int main(int arcgc, char *argv[])
    	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    	NSDate *now = [NSDate date];
    	NSLog(@"%@", now);
    	[pool drain];
    	return 0;
    I also see a TZ offset. It does the same thing across 10.4, 10.5, 10.6. I don't have a 10.7 at hand to test it on.

    Commands to compile and run:
    gcc -std=c99 -framework Foundation now.m -o now

    Going back to the OP:
    If I change my System Preferences > Date & Time > Time Zone to London ENG, UK, it shows my Time Zone as "British Summer Time". If I move it to Dublin, it shows "Irish Summer Time". If I move it to Iceland, it shows "Greenwich Mean Time", because Iceland doesn't observe DST.

    Please post the output from running the above program in Terminal. Also post the output from this Terminal command:
    If you're in the UK, and your Time Zone shows as "Greenwich Mean Time" in System Preferences, then I'd guess you have your TZ set wrong. You should pick one of the cities near you that observes one of the Summer Times. Tell us which city it is, so we can duplicate it. And tell us what city it was before changing it. Also tell us exactly which OS version you're using.
  7. thread starter macrumors member

    Mark FX

    Nov 18, 2011
    As previously stated, the terminal shell command displays the correct local date and time, as BST.

    The Applescript (current date) method displays the current local date and time correctly.

    But the [NSDate date] method displays the time as one hour early.

    I am aware that I can use the NSDateFormatter NSDateComponents classes to manipulate the output to the desired and correct time, but was I under the impression that the [NSDate Date] method call always gave the result as the
    local date correctly, regardless of your location, clearly I was wrong.

    I have looked into the documentation at the NSTimeZone class, which allows for localizing your date outputs, but Does beg the question, why should a class method designed for displaying a local date, need to be manipulated.

    Thanks for all the help.

    Regards Mark
  8. macrumors 68030


    Sep 13, 2001
    Portland, OR

    [NSDate date] doesn't display anything, and isn't in any sense "designed for displaying a local date". It's for creating an NSDate object (NSDate objects do not contain any locale information and have purposes other than display). The -description method on NSDate, which is what %@ uses, is intended for debug/logging use only. Do not rely on it to be in *any* particular format, or for its format to be consistent between machines or OS versions.
  9. thread starter macrumors member

    Mark FX

    Nov 18, 2011
    Thanks for the clarification on this, as a reletive newcomer to Objectiv-C, I was
    guided by the Apple documentation on the NSDate class, which kind of implies that the class method results in an accurate system date and time, in the same
    way the date shell command does.


    Thanks again

    Regards Mark
  10. macrumors 603

    Aug 9, 2009
    The NSDate is accurate. The problem you're having is one of formatting: what it prints, not whether the NSDate itself is accurate.

    If NSDate has a formatting problem, you have to post the details that allow someone to diagnose the problem. When I asked for things like the exact output from running the program, and the settings in System Preferences (like what you chose as Nearest City), it was to allow someone else to duplicate those settings, in an effort to duplicate the problem. So far, you've posted none of the details, so no one can duplicate the problem, nor begin trying to diagnose its cause.

    Also, you originally posted that your time zone was GMT, then you later revised that to BST. Getting accurate details of your setup will be important to solving the problem. We don't even know what OS version you're using, one of the more fundamental details.

    You could try creating a new user account, then running the posted NSDate test program there, and seeing if the problem also occurs there. If not, then it's probably a setting specific to your original account. If the problem also occurs in the new user, then it's probably a system-wide setting. Once again, though, complete and accurate details will be needed.

Share This Page