objective-c:时区之间的转换 [英] objective-c: Conversion between TimeZones
问题描述
我在将时间字符串转换为其他timeZone时遇到问题.
I'm having problems converting a time string to a different timeZone.
这是一个字符串示例,就像我从服务器上检索到的那样:@"5/14/2013 4:19 am"
This is a string example as the one I retrieve from a server: @"5/14/2013 4:19am"
我知道时区是America/New_York,我想将其转换为我的本地时区:欧洲/罗马.
I know the timeZone is America/New_York and I want to convert it to my local timeZone: Europe/Rome.
这是我编写的用于测试的代码:
Here is the code I wrote to test it:
-(void) convertTimeZone
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone=[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
dateFormatter.dateFormat=@"MM/dd/yyyy HH:mma";
NSString *sourceDateString = @"5/14/2013 4:19am";
NSTimeZone *sourceTimeZone = [[NSTimeZone alloc] initWithName:@"America/New_York"];
NSTimeZone *destinationTimeZone = [NSTimeZone localTimeZone];
NSInteger sourceGMToffset = sourceTimeZone.secondsFromGMT;
NSInteger destinationGMToffset = destinationTimeZone.secondsFromGMT;
NSInteger interval = destinationGMToffset-sourceGMToffset;
NSLog(@"DateFormatter TimeZone: %@", dateFormatter.timeZone);
NSLog(@"Source TimeZone: %@", sourceTimeZone);
NSLog(@"Destination TimeZone: %@", destinationTimeZone);
NSLog(@"sourceOffset: %u destinationOffset: %u interval: %u", sourceGMToffset, destinationGMToffset, interval);
NSLog(@"----------------------------------------------------------------------------------------------------");
NSLog(@"sourceDateString : %@", sourceDateString);
//Convert from string to date
NSDate *dateObject = [dateFormatter dateFromString:sourceDateString];
NSLog(@"From string to date: %@", dateObject);
//Now back from date to string
NSLog(@"From date to string: %@", [dateFormatter stringFromDate:dateObject]);
//Convert from sourceTimeZone to destinationTimeZone
NSDate *newDate = [[NSDate alloc] initWithTimeInterval: interval sinceDate:dateObject];
NSLog(@"destinationDateString: %@", [dateFormatter stringFromDate: newDate]);
}
输出 :
output:
DateFormatter时区:GMT(GMT)偏移量0
DateFormatter TimeZone: GMT (GMT) offset 0
来源时区:美国/纽约(GMT-04:00)偏移-14400(夏令时)
Source TimeZone: America/New_York (GMT-04:00) offset -14400 (Daylight)
目的地时区:本地时区(欧洲/罗马(CEST)偏移7200(夏令时)
Destination TimeZone: Local Time Zone (Europe/Rome (CEST) offset 7200 (Daylight))
sourceOffset:4294952896 destinationOffset:7200间隔:21600
sourceOffset: 4294952896 destinationOffset: 7200 interval: 21600
sourceDateString:2013年5月14日上午4:19
sourceDateString : 5/14/2013 4:19am
从字符串到日期:2013-05-14 00:19:00 +0000
From string to date: 2013-05-14 00:19:00 +0000
从日期到字符串:2013/05/14 00:19 AM
From date to string: 05/14/2013 00:19AM
destinationDateString:2013年5月14日上午6:19
destinationDateString: 05/14/2013 06:19AM
我要生气了!!!!
如果原始日期为"5/14/2013 4:19 am",那么日期格式化程序将其更改为"05/14/2013 00:19 AM" ????????? 如果已正确将其保存到"05/14/2013 04:19 AM +0000",则转换将是完美的(两个TimeZone之间的6小时)!
If the original date is "5/14/2013 4:19am" WHY the date formatter changes it to "05/14/2013 00:19AM" ????????? If it had saved correctly it to "05/14/2013 04:19AM +0000" the conversion would be perfect (6 hours between the two TimeZones)!!!
有任何提示吗?
谢谢
尼古拉
编辑
按照lithu-t-v的建议设置这些参数:
By setting these parameters as suggested by lithu-t-v:
dateFormatter.timeZone=[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSString *sourceDateString = @"5/14/2013 4:19am -0400";
它似乎可以工作,但是不起作用.
It seems to work but it doesn't.
我说似乎"是因为:
sourceDateString:2013年5月14日上午4:19 -0400
sourceDateString: 5/14/2013 04:19am -0400
从字符串到日期:2013-05-13 04:19:00 +0000(确定)
From string to date: 2013-05-13 04:19:00 +0000 (OK)
从日期到字符串:2013年5月14日04:19(确定)
From date to string: 05/14/2013 04:19 (OK)
destinationDateString:2013年5月14日10:19(正确)
destinationDateString: 05/14/2013 10:19 (CORRECT)
是正确的,但是:
sourceDateString:2013年5月14日10:19 pm -0400
sourceDateString: 5/14/2013 10:19pm -0400
从字符串到日期:2013-05-14 16:19:00 +0000(错误)
From string to date: 2013-05-14 16:19:00 +0000 (WRONG)
从日期到字符串:2013年5月14日16:19
From date to string: 05/14/2013 16:19
destinationDateString:2013年5月14日22:19(错误!)
destinationDateString: 05/14/2013 22:19 (WRONG!)
正确的结果应该是:2013年5月15日上午04:19
The correct result should be: 05/15/2013 04:19AM
推荐答案
+0000组件使用时区定义日期并提供实际时间.NSDate对象也包括此部分.因为该部分缺少时差.相对于当地时区而来
The +0000 component defines the date with the time zone and provide with actual time .A NSDate object include this part also.since that part is missing the time difference comes,with respect to local time zone
您在此处设置了时区,但未将其设置为datefromatter.将时区设置为dateformatter并尝试
Here you ceated the timezone but is not set to the datefromatter.Set timezone to the dateformatter and try it
在字符串后附加+0000,然后执行其余操作.
Append +0000 to the string and do the remaining.It will work
这篇关于objective-c:时区之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!