根据我们的应用规范,我们需要安排电话 session ,而我在安排时一直面临日光时差问题。
情况是这样的:美国夏令时在今年 11 月 3 日发生变化,当时我安排了一个电话,例如:11 月 4 日下午 5 点,那时我正在转换为 GMT 并发送到服务器。如果我将 GMT 时间转换为 11 月 4 日下午 4 点,而不是下午 5 点。尽管它只是正确的,但根据用户的观点,他安排在下午 5 点而不是下午 4 点,那么如何处理这种情况。
我正在转换成格林威治标准时间...
- (NSDateComponents*) convert_to_gmt_time : (NSDate*) date
{
NSDate *localDate = date;
NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneOffset;
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
NSDateComponents *date_comp = [[NSCalendar currentCalendar] components: NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:gmtDate];
return date_comp;
}
编辑:
这就是我将 GMT 时间转换为本地时间的方式...
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy"];
NSDate *strt_date_loc = [gregorian dateFromComponents:start_date]; // start_date i'm filling after received from server as GMT.
// Get date string.
NSString *strt_date = [dateFormatter stringFromDate: strt_date_loc];
非常感谢任何帮助。谢谢。
最佳答案
一次又一次……:您无法将日期转换为时区。您可以将日期的表示形式转换为时区。这不会更改日期。不要试图比日期更棘手。
如果您有本地格式(即美国)的日期表示,只需使用本地日期格式化程序从中构建一个日期。然后将这个日期发送到 $anywhere。
在返回的路上读取这个日期(不是它的表示之一)并使用本地格式化程序构建本地表示。
Never 的意思是从不将日期的任何本地表示发送到任何地方。 GMT 是本地表示。 (G代表什么?)
关于ios - 处理日光时差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19650290/