GMT到本地时间转换,夏令时变化 [英] GMT to Local Time Conversion with Daylight Saving Time change

查看:1330
本文介绍了GMT到本地时间转换,夏令时变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从服务器我接收GMT时间结构(用户定义的结构),使用我想将其转换为本地时间,我已经通过填充NSDatecomponent接收到的结构来完成它,然后我使用了date formattter来从它获取日期,一切正常,除了一个案例。如果GMT时间是11月3日之后(美国夏令时更改)格式化程序生成1小时时间差。



例如:如果预定的时间是11月3日下午4点,从GMT转换到当地时间为11月3日下午3点。 / p>

任何想法如何避免它。



修改:

  //选定日期
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];



//获取日期格式。
NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@GMT]];

// Start_date格式化程序。
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];


//获取日期字符串。
NSString * sel_date_time = [dateFormatter stringFromDate:strt_date_loc]; +

sel_date_time字符串是1小时比它应该的要少..



记录:



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



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



TimeZone:Palo Alto(美国)



本地到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]组件:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:gmtDate];

return date_comp;
}

thanx。

解决方案

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



夏令时在该日期无效,
因此UTC / GMT与加州时间之间的差异为8小时。
因此

  2013-11-30 06:56:00 +0000 = 2013-11-29 22:56 :00  -  0800 = 2013年11月29日下午10:56 

这就是你得到的。



已添加:您将本地日期转换为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];


From server I'm receiving GMT time structure(user defined structure), using that I want to convert it into local time, I have done it by filling the NSDatecomponent with the structure received, and then I have used date formattter to get date from it, everything works fine except one case. If the GMT time is after Nov 3 (Daylight Saving Time change in US) formatter is producing 1 hour time difference.

for eg: If the intended time is for Nov-3 ,4 PM, after conversion from GMT to local its giving Nov-3, 3 PM.

Any idea how to avoid it.

Edit:

   // 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 string is one hour less than what it supposed to be..

Log :

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

sel_date_time = Nov 29, 2013 10:56 PM (but it should be 11:56 PM)

TimeZone : Palo Alto (US)

Local to gmt Conversion:

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

thanx.

解决方案

Your result is correct. The date formatter does not use the current time difference between your local time and GMT, but the time difference that is in effect at the date that is converted.

The daylight savings time is not in effect at that date, so the difference between UTC/GMT and California time is 8 hours. Therefore

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

and that is what you got.

ADDED: Your conversion of the local date to GMT components does not work correctly because

[[NSTimeZone defaultTimeZone] secondsFromGMT] 

is the current time difference to GMT, and not the time difference that is in effect at the date to be converted. The following should work correctly (as is even slightly shorter):

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

这篇关于GMT到本地时间转换,夏令时变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆