iPhone - current date considering daylight saving

Viewed 7726

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.

4 Answers

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);
}
Related