In my app I need to create an event in the Calendar programmatically, all the algorithm works around dates created by method + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)seconds. But with this method I cannot create an event, it seems that it does not have timezone set inside somewhere, because with other methods (i.e. [NSDate date]) event is created successfully.
My solution was to convert NSDate to CFGregorianDate, set timezone and convert back. It seems a bit long I guess.
Code snippet:
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1322045010]; // interval is in seconds
NSLog(@"%@", date);
event.title = @"Test";
event.startDate = date;
event.endDate = date;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
[event addAlarm:[EKAlarm alarmWithAbsoluteDate:date]];
[eventStore saveEvent:event span:EKSpanThisEvent error:nil];
So my question is – why is it not possible to create an event with NSDate initialized with + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)seconds? And could somebody suggest more robust way to solve this problem, I would be very grateful.
Artem
Your error is revealed when you said this: “it seems that it does not have timezone set inside somewhere, because with other methods (i.e.
[NSDate date]) event is created successfully.”No
NSDatehas any timezone information inside it. They are all referenced to GMT. TheNSDateFormatterclass is usually used to present them in the local timezone. And judging by your response to the comments above I doubt you believe me now. Test this out, in some method somewhere run this line of code.According to your user profile you are in Estonia. By my quick search you should be GMT+2:00(I’m sorry if I’m mistaken on your time zone) so the time you see in your console should be 2 hours behind. And I’m sure it is. The same as mine is 5 hours ahead.
You can also tell I am right by your solution: “My solution was to convert NSDate to CFGregorianDate, set timezone and convert back. It seems a bit long I guess.”
So basically you adjusted for the timezone and it works fine, Makes sense.
But to your real question ‘Could somebody suggest a more robust way to solve this problem?’ The answer is relatively simple.
Let’s say your server is returning the time interval since 1970 in estonian time (Eastern Europe?). All you have to do is adjust that time interval to point to the GMT time. Again if I’m wrong on the specifics of your time zone I apologize.
Again this adjustment would be indexed to the servers response.
Edit: In response to:
To quote a well know TV character “Challenge accepted!”.
So I set out to test if an event could be successfully created using
dateWithTimeIntervalSince1970:. And as I expected I found no flaw in NSDate or in the EventKit framework. I used this code:When I run this code I get the “Event added in calendar” message. The two minutes gives me plenty of time to go to calendar and see that an event has in fact been correctly scheduled. And exit and wait for my alarm to go off. And it does.