9

My country is located in GMT+0. We are +1 hour now, because we are in daylight saving.

When I do

[NSDate date];

it was supposed to return the current date and time, but when I do that, I receive the GMT time not considering the daylight saving.

This is what the docs say about the date command

Creates and returns a new date set to the current date and time.

Why Apple does that to us? Why everything is so complex? Is this a bug? Is there a way to get the current real time the device is on? the same time that is displayed on the device's clock?

My app depends on dates and times and having a wrong date for an user located in a different timezone around the world that is on summertime or wintertime will be a disaster.

Thanks.


EDIT

After several answers, I have tried this code:

NSDate *wrongToday = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];

[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *currentTime = [dateFormatter wrongToday];

NSDate *today = [dateFormatter dateFromString:currentTime];

guess what, today = wrongToday... in other words, no change, I continue to have the wrong date without daylight saving. what is more amazing is that currentTime shows in NSString the date with daylight saving...

any clues? thanks again.

Duck
  • 34,902
  • 47
  • 248
  • 470
  • 2
    NSDate objects represent an absolute moment in time. They are not affected by daylight saving or timezones. To output a date in a specific time zone, you should create an instance of NSDateFormatter. – albertamg Jul 28 '11 at 23:02
  • 1
    sorry, but Apple docs say that [NSDate date] returns the current date and time and this is not true. A current date and time must consider daylight saving. – Duck Jul 28 '11 at 23:05
  • 2
    Timezone and daylight savings are localisation issues. The docs mention GMT in a number of places, but they certainly could clearer. – MRAB Jul 28 '11 at 23:18
  • 1
    NSDate is just the amount of time since a reference date. The different human date representations vary between timezones and daylight saving settings. – albertamg Jul 28 '11 at 23:23
  • Sorry, but I don't agree to that. The english sentence says it gives the current date and the date without daylight saving is not anything and is an unnecessary increase in complexity as Apple always do. Can you please care to answer how to get the correct date considering daylight saving with code? thanks. – Duck Jul 28 '11 at 23:24
  • @Digital Robot - The issue is there are many countries that do not observe Daylight Savings. Considering that fact, I think Apple has done the right thing and made it a localisation issue. Otherwise, how would NSDate know if your location observes Daylight Savings? – sosborn Jul 28 '11 at 23:53
  • I have edited the question to include some code... please read it. – Duck Jul 28 '11 at 23:58
  • @sosbom, apple has developed an API that tests that: [NSTimeZone isDaylightSavingTime];, anyway, as always their docs are crappy. – Duck Jul 29 '11 at 00:06
  • I'm pretty sure you want the [first link](http://stackoverflow.com/questions/1081647/how-to-convert-time-to-the-timezone-of-the-iphone-device/1082179#1082179) I posted in my answer. – Joe Osborn Jul 29 '11 at 00:43
  • @Digital Robot "I continue to have the wrong date without daylight saving." `NSDate`s don't have anything to do with daylight saving. The *current date and time* at the time of writting is something like `333617327.897088` (seconds since the first instant of 1 January 2001, GMT). In Denver, it would be `7/29/11 1:28 AM` while in London, it would be `7/29/11 8:28 AM`. Still it is the same `NSDate` object, the same instant in time. If you log the date directly without using a `NSDateFormatter`, bear in mind that `NSDate`'s `description` method returns times in UTC. – albertamg Jul 29 '11 at 08:05

4 Answers4

8

albertamg, MRAB and Joe Osborn are all correct. They're all trying to explain to you that NSDate is a "number", "an absolute moment in time".

This value is INDEPENDENT of whether you're in London, in Los Angeles or in Singapore. It's independent of whether your county respects daylight savings time.

To INTERPRET NSDate in terms of something recognizable (like "Th July 28, 4:28pm"), you need an NSDateFormatter. You also need to make sure your locale is defined correctly: including timezone, whether daylight savings is honored, and various time/date formatting conventions.

'Hope that helps ..

paulsm4
  • 114,292
  • 17
  • 138
  • 190
4

Hard coding:

    BOOL isDayLightSavingTime = [sysTimezone isDaylightSavingTimeForDate:currentDate];
if (isDayLightSavingTime) {
    NSTimeInterval timeInterval = [sysTimezone  daylightSavingTimeOffsetForDate:currentDate];
    currentDate = [currentDate dateByAddingTimeInterval:timeInterval];
}

the current date is now daylight saving time.

Hope help.

frank
  • 2,327
  • 1
  • 18
  • 20
3

My suggestion!

NSString *dateToTest = @"16-10-2016"; // <-- daylight saving
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];

[dateformatter setDateFormat:@"dd-MM-yyyy"];

NSDate *data = [dateformatter dateFromString:dateToTest];

NSLog(@"data before --> %@:", data);


if (data == nil && [[NSTimeZone systemTimeZone] isDaylightSavingTimeForDate:data]) {

    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:[NSTimeZone localTimeZone].name];

    [dateformatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:[timeZone secondsFromGMT]]];

    data = [dateformatter dateFromString:dateToTest];

    NSLog(@"data after --> %@:", data);
}
Carlos Irano
  • 682
  • 7
  • 8
2

There are a couple of good answers on the site already here and here and here. An NSDate is an interval since the (UTC) reference date, but you can use NSTimeZone as detailed in those answers.

Community
  • 1
  • 1
Joe Osborn
  • 1,145
  • 7
  • 10