目标c中的GMT时区转换 [英] GMT timezone conversion in objective c

查看:233
本文介绍了目标c中的GMT时区转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将nsstring转换为nsdate,然后转换为systemtimezone.我的代码对吗?任何帮助表示赞赏.

I am trying to convert nsstring to nsdate and then to the systemtimezone. Is my code right? Any help appreciated.

NSString *str=@"2012-01-15 06:27:42";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [dateFormatter dateFromString:str];

NSDate* sourceDate = dateFromString;

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT-07:00"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];

bottomLabel2.text=[NSString stringWithFormat:@"- %@ -",destinationDate];

代码结果为2012-01-15 06:27:42 +0000,与源代码相同!

The code result is 2012-01-15 06:27:42 +0000 which is the same as the source!

推荐答案

认为我可以看到这里存在的问题-这是此行:

I think I can see what the problem is here - it's this line:

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT-07:00"];

GMT-07:00不是此方法的有效缩写.如果调用[NSTimeZone abbreviationDictionary],您将获得包含所有可用缩写的词典.您需要使用实际的缩写(例如,太平洋标准时间为"PST"),而不要使用格式"GMT-07:00".

GMT-07:00 is not a valid abbreviation for this method. If you call [NSTimeZone abbreviationDictionary] you'll get a dictionary containing all available abbreviations. You need to use the actual abbreviation (eg, 'PST' for Pacific Standard Time), and not the format "GMT-07:00".

为节省您的时间,以下是使用NSTimeZone支持的缩写的完整列表.

To save you time, here's the full list of supported abbreviations using NSTimeZone.

    ADT = "America/Halifax";
    AKDT = "America/Juneau";
    AKST = "America/Juneau";
    ART = "America/Argentina/Buenos_Aires";
    AST = "America/Halifax";
    BDT = "Asia/Dhaka";
    BRST = "America/Sao_Paulo";
    BRT = "America/Sao_Paulo";
    BST = "Europe/London";
    CAT = "Africa/Harare";
    CDT = "America/Chicago";
    CEST = "Europe/Paris";
    CET = "Europe/Paris";
    CLST = "America/Santiago";
    CLT = "America/Santiago";
    COT = "America/Bogota";
    CST = "America/Chicago";
    EAT = "Africa/Addis_Ababa";
    EDT = "America/New_York";
    EEST = "Europe/Istanbul";
    EET = "Europe/Istanbul";
    EST = "America/New_York";
    GMT = GMT;
    GST = "Asia/Dubai";
    HKT = "Asia/Hong_Kong";
    HST = "Pacific/Honolulu";
    ICT = "Asia/Bangkok";
    IRST = "Asia/Tehran";
    IST = "Asia/Calcutta";
    JST = "Asia/Tokyo";
    KST = "Asia/Seoul";
    MDT = "America/Denver";
    MSD = "Europe/Moscow";
    MSK = "Europe/Moscow";
    MST = "America/Denver";
    NZDT = "Pacific/Auckland";
    NZST = "Pacific/Auckland";
    PDT = "America/Los_Angeles";
    PET = "America/Lima";
    PHT = "Asia/Manila";
    PKT = "Asia/Karachi";
    PST = "America/Los_Angeles";
    SGT = "Asia/Singapore";
    UTC = UTC;
    WAT = "Africa/Lagos";
    WEST = "Europe/Lisbon";
    WET = "Europe/Lisbon";
    WIT = "Asia/Jakarta";

这篇关于目标c中的GMT时区转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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