-1

I'm trying to create a date in ios with:

 NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
        [gregorian setTimeZone:[NSTimeZone systemTimeZone]];
        NSDateComponents *dateComponentes=[[NSDateComponents alloc] init];

        //We create a date invented.
        [dateComponentes setYear:2014];
        [dateComponentes setMonth:2];
        [dateComponentes setDay:15];
        [dateComponentes setHour:0];
        [dateComponentes setMinute:0];
        [dateComponentes setSecond:0];
        [dateComponentes setTimeZone:[NSTimeZone systemTimeZone]];

        NSDate *initialDate = [gregorian dateFromComponents:dateComponentes];

The thing is, my current time zone is BST, but when I print the time I get this:

(lldb) po [initialDate descriptionWithLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]]
Saturday, 15 February 2014 00:00:00 Greenwich Mean Time

But should I get the 2014-02-14 23:00:00 +0000 for this date? :

(lldb) po [initialDate description]
2014-02-15 00:00:00 +0000

I tried with NSDate and is right,

(lldb) po [[NSDate date] descriptionWithLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]] 
Thursday, 29 May 2014 16:35:21 British Summer Time

(lldb) po [[NSDate date] description]
2014-05-29 15:35:26 +0000

Any ideas?

xarly
  • 2,054
  • 4
  • 24
  • 40

1 Answers1

0

Your first example is in February, which is in the winter, when England is on UTC. Your second example is after the DST switch, when England is UTC+1. Play around with the dates using timeanddate's World Clock and you'll see that NSCalendar is correctly computing the NSDate.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Fristly thanks for your answer, Yes you are totally right. But actually my problem was I was creating the initial date with the code above and more dates with: [initialDate dateByAddingTimeInterval:(60*60*24) * i]; So I was creating dates for every day in a range. Unfortunatelly this dates were saved with the same NSTimeZone because is an increment. That made me think! :) Thanks – xarly May 29 '14 at 19:47