我正在尝试从用户的日历应用中获取事件。这本身很好,但有一点不太对。我在不同的标签中显示title
,location
,startDate
和endDate
。现在,我的问题是endDate
和startDate
都运行了一个小时,所以当我将事件设置为19:00时,它在我的应用中显示为18:00。我正在使用此代码:
// 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];
有人知道如何解决这个问题吗?提前谢谢!
答案 0 :(得分:3)
好吧,自己想通了。我要做的就是添加这一行:
int z = [[NSTimeZone localTimeZone] secondsFromGMT] / 3600;
并将z添加到startDate
和endDate
答案 1 :(得分:1)
您是否肯定系统的时区与日历的时区相同。我之前遇到过一些奇怪的问题(一两个小时),因为我和创建日历事件的时区不同。奇怪的问题。