问题
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.
回答1:
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];
来源:https://stackoverflow.com/questions/19675405/gmt-to-local-time-conversion-with-daylight-saving-time-change