Search code examples
objective-ccalendartimezoneekeventkitekevent

Issue with Apple's EKEvent Sample


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!


Solution

  • 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