PDA

View Full Version : NSDate incorrect time displayed




Mark FX
May 9, 2012, 12:26 PM
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.

Appescript

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



chown33
May 9, 2012, 12:37 PM
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 (http://en.wikipedia.org/wiki/Daylight_saving_time) 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.

Mark FX
May 11, 2012, 12:29 PM
Sorry for the slow reply.

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

Regards Mark

chown33
May 11, 2012, 01:02 PM
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?
http://en.wikipedia.org/wiki/Greenwich_Mean_Time

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?

gnasher729
May 11, 2012, 02:09 PM
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 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.

chown33
May 11, 2012, 03:18 PM
Your assumption that NSLog should display the local time is wrong.

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
./now



Going back to the OP:
I'm in the UK, and time setting preference is for GMT.

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:
date

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.

Mark FX
May 11, 2012, 04:40 PM
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

Catfish_Man
May 11, 2012, 05:20 PM
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


[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.

Mark FX
May 12, 2012, 01:12 PM
Originally posted by Catfish_Man
[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.

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.

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003641

Thanks again

Regards Mark

chown33
May 12, 2012, 02:49 PM
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.