【问题标题】:GMT to Local Time Conversion with Daylight Saving Time change格林威治标准时间与夏令时更改的本地时间转换
【发布时间】:2013-11-09 14:56:13
【问题描述】:

我从服务器接收 GMT 时间结构(用户定义的结构),使用我想将其转换为本地时间,我通过用接收到的结构填充 NSDatecomponent 来完成它,然后我使用日期格式化程序来从中获取日期,除一种情况外,一切正常。如果 GMT 时间在 11 月 3 日之后(Daylight Saving Time 美国更改),格式化程序将产生 1 小时的时差。

例如:如果预期时间是 11 月 3 日下午 4 点,则在从 GMT 转换为本地时间后,它会给出 11 月 3 日下午 3 点。

知道如何避免它。

编辑:

   // Selected Dates
   NSDateComponents *sel_date = [[NSDateComponents alloc]init];
    sel_date.second = sch_detail.sel_dates.seconds;
    sel_date.minute = sch_detail.sel_dates.mins;

    sel_date.hour   = sch_detail.sel_dates.hours;
    sel_date.day    = sch_detail.sel_dates.date;
    sel_date.month  = sch_detail.sel_dates.month;
    sel_date.year   = sch_detail.sel_dates.year;
    sel_date.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];



    // Get the Date format.
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

    // Start_date formatter.
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMM dd, yyyy hh:mm a"];
   [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];

   NSDate *strt_date_loc = [gregorian dateFromComponents:sel_date];


   // Get date string.
   NSString *sel_date_time = [dateFormatter stringFromDate: strt_date_loc];+

sel_date_time 字符串比它应该的时间少一小时..

日志:

strt_date_loc = 2013-11-30 06:56:00 +0000

sel_date_time = 2013 年 11 月 29 日晚上 10:56(但应该是晚上 11:56)

时区:帕洛阿尔托(美国)

本地到 GMT 转换:

- (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;
}

谢谢。

【问题讨论】:

  • 显示您的相关代码并显示一些示例数据。包括相关值的实际日志输出。
  • 请稍等
  • 记录strt_date_locsel_date_time。粘贴实际的日志输出。你在哪个时区?
  • @maddy 请检查编辑

标签: ios nsdate nsdateformatter nstimezone


【解决方案1】:

你的结果是正确的。日期格式化程序不使用 current 时差 在您的当地时间和 GMT 之间,但在 转换的日期。

夏令时在该日期无效, 所以 UTC/GMT 和加利福尼亚时间之间的差异是 8 小时。 因此

2013-11-30 06:56:00 +0000 = 2013-11-29 22:56:00 -0800 = Nov 29, 2013 10:56 PM

这就是你得到的。

添加:您将本地日期转换为 GMT 组件无法正常工作 因为

[[NSTimeZone defaultTimeZone] secondsFromGMT] 

当前 与 GMT 的时差,而不是有效的时差 在转换日期。 以下应该可以正常工作(甚至更短):

NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDateComponents *date_comp = [cal components: NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:localDate];

【讨论】:

  • 解释一下... 通常美国西海岸比 UTC (GMT) 时间晚 8 小时。在夏令时 (DST) 期间,美国西海岸比 UTC (GMT) 晚 7 小时。见Time changes in year 2013 for USA - California - Los Angeles。今年,2013 年,夏令时从 3 月 10 日开始,到 11 月 3 日星期日 02:00 结束。
  • 感谢您的回答,我遇到了问题,但是如何处理这种情况,例如:我正在安排 11 月 4 日下午 4 点的通话,我将其转换为 gmt 并发送到服务器,当我下载回来并进行 gmt 到本地转换时,时间显示为下午 3 点,所以我需要在安排自己时检查任何条件吗?我希望在下午 4 点而不是下午 3 点建立通话。
  • @fztest:看来您将“11 月 4 日下午 4 点”转换为 GMT 是错误的。
  • @MartinR 请检查我的编辑。可以吗。它适用于正常情况,除了日光变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-11
  • 2020-11-25
  • 1970-01-01
  • 2011-10-30
  • 2015-08-09
  • 2011-05-13
相关资源
最近更新 更多