【问题标题】:Issue with Apple's EKEvent SampleApple 的 EKEvent 示例问题
【发布时间】:2011-12-11 20:40:30
【问题描述】:

我正在尝试从用户的日历应用中获取事件。这本身工作正常,但有一件事并不完全正确。我在不同的标签中显示titlelocationstartDateendDate。现在,我的问题是 endDatestartDate 都晚了一个小时,所以当我将事件设置为 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];

有人知道如何解决这个问题吗?提前致谢!

【问题讨论】:

    标签: objective-c calendar timezone ekeventkit ekevent


    【解决方案1】:

    好吧,我自己想通了。我所要做的就是添加这一行:

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

    并将 z 添加到 startDateendDate

    【讨论】:

      【解决方案2】:

      您确定系统的时区与日历的时区相同吗?我之前遇到过一些奇怪的问题,因为我与创建日历事件时所处的时区不同(相差一两个小时)。奇怪的问题。

      【讨论】:

      • 在夏令时,当您假设当天的情况时,您可能会遇到其他问题。
      • 是的,是的。夏令时也会改变一些事情。
      • 是的,时区是一样的..还有其他想法吗?
      • 很奇怪。您的代码看起来很完美。不过,这一定是时区的某种问题,因为那将是休息一个小时的唯一方法。夏令时似乎最有可能。我不确定。
      • 再次尝试使用另一个样本,这次没有进行任何修改:developer.apple.com/library/ios/#samplecode/SimpleEKDemo/…。仍然是同样的问题,它提前一个小时运行。我一直在搞乱时区,将其设置为奥地利维也纳,比如说美国迈阿密(我在设置和事件中都更改了它)将 startDateendDate 更改为完全不同的东西。
      猜你喜欢
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多