Carelinkz
Carelinkz

Reputation: 936

CalEvent with IsAllDay=TRUE gets me in trouble with timezone

I have a TimeZone problem with allday events in CalCalendar:

- (BOOL)setDayType:(NSString *)type 
            forDay:(NSDate *)date;
{
    NSError *err = nil;
    CalEvent *e = [CalEvent event];
    [e setTitle:type];
    [e setIsAllDay:YES];
    [e setStartDate:date];
    [e setEndDate:[date dateByAddingTimeInterval:1]];
    [e setCalendar:[self calendar]];
    if ( ![[CalCalendarStore defaultCalendarStore] saveEvent:e span:CalSpanThisEvent error:&err] )
    {
...

Let's say I set the date at January 2nd, 2012, with a time of 00:00:00. I am at GMT+2, so timezone +0200. My date object reports 1-1-2012, 22:00:00, at GMT, as per this NSLog output:

"Set daytype ptd on 2012-01-01 22:00:00 +0000"

and also generates the allday even on January 1st!!

How can I make sure that I set the allday event in the correct day, for all timezones? I could easily add three hours but that will not work worldwide!

Upvotes: 0

Views: 186

Answers (1)

lnafziger
lnafziger

Reputation: 25740

As long as date is on the correct day in the current time zone, you can use it as-in.

The "problem" that you see is caused because if you print the stored date via NSLog (or in the debugger) it will always show you GMT time because NSDate has no knowledge of which timezone that you are in.

However, since date is a specific point in time, when you format it for the user (using a date formatter), it will show the correct date and time, no matter what the time zone is, as long as it stays consistent.

The only situation where you will need to worry about adding/subtracting time like you mention is if you need a point in time which is the same date across multiple time zone's.

EDIT:
So it looks like iCal uses GMT time to store everything, so you need to create your date in the GMT timezone. If you need help with this, post your code where you create your NSDate and I'll be happy to point you in the right direction.

Upvotes: 1

Related Questions