2012-01-12 106 views
1

我试图从EKEventStore的一个实例中使用eventsMatchingPredicate检索所有事件:但是据我所知,NSDate对象默认设置为GMT,而EKEventStore isn “T。所以我的问题是如何更改EKEventStore的时区或调整NSDate对象,以便每个时区都不关闭时间?EKEventStore所在的时区不是格林尼治时间

例如,我在格林威治标准时间-0600,并在1月16日和17日点击TKCalendarMonthView我用于日历UI显示两个日期的马丁路德金日。开始时间是1月16日上午6点,结束时间是1月17日上午5点59分(由于我的时区),而不是从上午12:00开始,直到下午11:59。用于检索事件的代码如下所示。

- (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d { 
    // Update tableData with event data from date 
    [tableData removeAllObjects]; 
    NSArray *a = [systemCalendar eventsMatchingPredicate:[systemCalendar predicateForEventsWithStartDate:d endDate:[NSDate dateWithTimeInterval:84600 sinceDate:d] calendars:nil]]; 
    [tableData addObjectsFromArray:a]; 
    [self.eventsTable reloadData]; 
} 

回答

2

鉴于我在一个很短的时间表,我想出了一个解决方案,它似乎工作。我唯一担心的是,即使时间间隔偏移量本身为负值,我也必须将偏移量乘以-1。这没有意义,因为我们试图从NSDate中减去而不是添加到NSDate中。正数减负数给我们一个更大的数字,所以我稍微担心PM的另一边的GMT区,并想知道我是否应该实际上将所有时间间隔乘以-1。任何人有任何想法?

- (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d { 
    [NSTimeZone resetSystemTimeZone]; 
    NSTimeZone *tz = [NSTimeZone systemTimeZone]; 
    NSArray *comps = [[tz description] componentsSeparatedByString:@" "]; 
    NSTimeInterval offset = (NSTimeInterval)[[comps lastObject] floatValue]; 
    if (offset < 0) { 
    offset *= -1; 
    } 
    NSDate *startDate = [d dateByAddingTimeInterval:offset]; 

    NSArray *a = [systemCalendar eventsMatchingPredicate:[systemCalendar predicateForEventsWithStartDate:startDate endDate:[NSDate dateWithTimeInterval:84600 sinceDate:startDate] calendars:nil]]; 
    NSLog(@"Events for the date: %@", a); 
    [tableData addObjectsFromArray:a]; 
    [self.eventsTable reloadData]; 
} 
相关问题