1

I'm trying to fetch events from the user's Calendar app. That itself works fine, but one thing isn't quite right. I'm displaying the title, location, startDateand endDate in different labels. Now, my problem is that both endDate and startDate are running one hour late, so when I set an event for 19:00, it appears as 18:00 in my app. I'm using this code:

// Create the predicate's start and end dates.
CFGregorianDate gregorianStartDate, gregorianEndDate;
CFGregorianUnits startUnits = {0, 0, -30, 0, 0, 0};
CFGregorianUnits endUnits = {0, 0, 15, 0, 0, 0};
CFTimeZoneRef timeZone = CFTimeZoneCopySystem();

gregorianStartDate = CFAbsoluteTimeGetGregorianDate(
CFAbsoluteTimeAddGregorianUnits(CFAbsoluteTimeGetCurrent(), timeZone, startUnits),
timeZone);
gregorianStartDate.hour = 0;
gregorianStartDate.minute = 0;
gregorianStartDate.second = 0;

gregorianEndDate = CFAbsoluteTimeGetGregorianDate(
CFAbsoluteTimeAddGregorianUnits(CFAbsoluteTimeGetCurrent(), timeZone, endUnits),
timeZone);
gregorianEndDate.hour = 0;
gregorianEndDate.minute = 0;
gregorianEndDate.second = 0;

NSDate* startDate =
[NSDate     dateWithTimeIntervalSinceReferenceDate:CFGregorianDateGetAbsoluteTime(gregorianStartDate,     timeZone)];
NSDate* endDate =
[NSDate     dateWithTimeIntervalSinceReferenceDate:CFGregorianDateGetAbsoluteTime(gregorianEndDate, timeZone)];

CFRelease(timeZone);

// Create the predicate.
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate     endDate:endDate calendars:nil]; // eventStore is an instance variable.

// Fetch all events that match the predicate.
NSArray *events = [eventStore eventsMatchingPredicate:predicate];

Does anybody have an idea how to fix this? Thanks in advance!

kopproduction
  • 465
  • 5
  • 19

2 Answers2

3

Alright, figured it out myself. What I had to do was add this line:

int z = [[NSTimeZone localTimeZone] secondsFromGMT] / 3600; 

and add z to the startDate and endDate

kopproduction
  • 465
  • 5
  • 19
1

Are you positive the time zone for the system is the same as the time zone for the calendar. I've had odd issues before where it was slightly different (by an hour or two) because I was in a different time zone than when I had created the calendar event. Odd issue.

Dylan Gattey
  • 1,713
  • 13
  • 34
  • And on daylight savings time you may get other issues when you assume stuff about the day. – fearmint Dec 11 '11 at 20:48
  • Yes, true. Daylight savings time would change things as well. – Dylan Gattey Dec 11 '11 at 20:49
  • Yes, the time zones are the same.. any other ideas? – kopproduction Dec 11 '11 at 22:21
  • Weird. Your code looks perfect. It has to be some sort of issue with the time zone though, as that would be the only way it would be an hour off. Daylight savings seems most likely. I'm not sure. – Dylan Gattey Dec 12 '11 at 05:39
  • Tried again using another sample, this time without modification of anything: http://developer.apple.com/library/ios/#samplecode/SimpleEKDemo/Introduction/Intro.html. Still the same problem, it runs an hour early. I've been messing with the timezones, setting it from Vienna, Austria, to say, Miami, U.S.A (I changed it in both settings and event) changes the `startDate` and `endDate` to something completely different. – kopproduction Dec 12 '11 at 12:38
  • I used the exact sample. Just downloaded it and ran it on the Simulator (5.0) and my iPhone 4 (5.0.1) and it worked perfectly. Hours matched up perfectly. Were you running it in the simulator? – Dylan Gattey Dec 12 '11 at 17:00