2012-01-12 26 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]; 
} 
相關問題