2013-08-04 33 views
0

我試圖讓NSTimeInterval的時間設置爲零,所以一天的開始,但總是我的時區GMT + 2,而不是GMT,我'在做這樣的:NSTimeInterval的startOfDay NSDate到GMT

這是設置一天的開始在NSDate的類別:

#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit) 
#define CURRENT_CALENDAR [NSCalendar currentCalendar] 

- (NSDate *) dateAtStartOfDay 
{ 
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self]; 
components.hour = 0; 
components.minute = 0; 
components.second = 0; 
return [CURRENT_CALENDAR dateFromComponents:components]; 
} 

那麼我這樣做:

NSTimeInterval now = [[[NSDate date] dateAtStartOfDay] timeIntervalSince1970]; 

,並給我這個NSTimeInterval:

1375567200 

,如果我的檢查:

http://www.epochconverter.com

GMT時間是這樣的:

GMT: Sat, 03 Aug 2013 22:00:00 GMT 

Mine tiem zone: 04 agosto 2013 00:00:00 CEST GMT+2 

我怎麼能始終在GMT時間間隔?我正在尋找一個在紐約,羅馬,里約熱內盧工作的解決方案,例如,必須給我nMyInterval才能讓用戶在哪裏,任何人都可以幫助我?

回答

0

您從currentCalendar獲得的日曆使用當前的語言環境以及因此運行的任何系統的時區。您應該爲此計算創建自己的日曆,並將其時區設置爲GMT。

NSCalendar * GMTCal = [[NSCalendar alloc] initWithCalendarIdentifier:[[NSCalendar currentCalendar] calendarIdentifier]; 
[GMTCal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];