ios 如何使用 NSTimeZone -timeZoneWithName: 与来自 Rails ActiveSupport 的城市名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10382932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:10:06  来源:igfitidea点击:

How to use NSTimeZone -timeZoneWithName: with a city name from Rails ActiveSupport?

iostimezonenstimezoneactivesupport

提问by Hlung

If I only have the city name like Bangkokor Tokyo, how can I supply a timezone parameter in [NSTimeZone timeZoneWithName:@"Asia/Tokyo"]where it also has continent and slash in front of city?

如果我只有像BangkokTokyo这样的城市名称,我如何[NSTimeZone timeZoneWithName:@"Asia/Tokyo"]在城市前面也有大陆和斜线的地方提供时区参数 ?

I already tried [NSTimeZone timeZoneWithName:@"Tokyo"], it doesn't work.

我已经试过了[NSTimeZone timeZoneWithName:@"Tokyo"],它不起作用。

回答by Hlung

Thanks for the answers guys, but it looks like those city names are just a format that Rails "ActiveSupport::TimeZone" uses. So I just have to map it back. The mapping is here -> http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html

感谢大家的回答,但看起来这些城市名称只是 Rails "ActiveSupport::TimeZone" 使用的一种格式。所以我只需要把它映射回来。映射在这里 -> http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html

It maps the timezone from Rails "ActiveSupport::TimeZone" format to another format that iOS uses (e.g. "International Date Line West" => "Pacific/Midway", "Midway Island" => "Pacific/Midway", "Samoa" => "Pacific/Pago_Pago", ... ).

它将时区从 Rails 的“ActiveSupport::TimeZone”格式映射到 iOS 使用的另一种格式(例如“International Date Line West”=>“Pacific/Midway”、“Midway Island”=>“Pacific/Midway”、“Samoa” => "Pacific/Pago_Pago", ... )。

I have created a plist filecontaining a NSDictionary property that can easily be used for mapping.

我创建了一个包含 NSDictionary 属性的 plist 文件,可以轻松地用于映射。

Edit: an updated version(with usage example) for Rails 3.2 (thanks RJ Regenold!)

编辑:Rails 3.2的更新版本(带有使用示例)(感谢 RJ Regenold!)

回答by tagirkaZ

You can see list of all timezone names.

您可以看到所有时区名称的列表。

NSLog(@"%@", [NSTimeZone knownTimeZoneNames]);

Hope this will be helpful for you.

希望这对你有帮助。

回答by Chris

If they're all in Asia, why not use something like:

如果他们都在亚洲,为什么不使用类似的东西:

MYCITY=Tokyo
[NSTimeZone timeZoneWithName:@"Asia/$MYCITY"]

If you need more cities and timezones, you could create a static text list file based on all the time zones found at /usr/share/zoneinfo.

如果您需要更多城市和时区,您可以根据在 /usr/share/zoneinfo 找到的所有时区创建一个静态文本列表文件。

create file 'timezones.by.city.txt' with the following text. (Truncated here.)

使用以下文本创建文件“timezones.by.city.txt”。(这里截断。)

"Asia/Aden"
"Asia/Almaty"
"Asia/Amman"
"Asia/Anadyr"
"Asia/Aqtau"
"Asia/Aqtobe"
"Asia/Ashgabat"
"Asia/Ashkhabad"
"Asia/Baghdad"
"Asia/Bahrain"
"Asia/Baku"
"Asia/Bangkok"
"Asia/Beirut"
"Asia/Bishkek"
... etc.
"US/Alaska"
"US/Aleutian"
"US/Arizona"
"US/Central"
"US/Eastern"
"US/East-Indiana"
"US/Hawaii"
"US/Indiana-Starke"
"US/Michigan"
"US/Mountain"
"US/Pacific"
"US/Pacific-New"
"US/Samoa"

Then:

然后:

MYCITY=Bangkok
MYTZ=`grep "$MYCITY" ./timezones.by.city.txt`
[df setTimeZone:[NSTimeZone timeZoneWithName:@"$MYTZ"]]

回答by Allamaprabhu

NSArray *zones = [NSTimeZone knownTimeZoneNames];

It returns all the set of timeszones supported by your system.

它返回系统支持的所有时区集。

You have to setTimezone like below, which you have tried already.

你必须setTime像下面这样分区,你已经尝试过了。

[NSTimeZone timeZoneWithName:@"Asia/Tokyo"];

回答by Roger

You may find the dataset and/or API from http://www.geonames.org/useful in solving this sort of problem. They have the correct olsen timezone (which is what the 'Asia/Tokyo' is called) for a vast number of locations and you can either download and wrangle the data yourself, or use their API to get what you need.

您可能会发现http://www.geonames.org/ 中的数据集和/或 API对解决此类问题很有用。他们拥有适用于大量地点的正确奥尔森时区(这就是所谓的“亚洲/东京”),您可以自己下载和整理数据,也可以使用他们的 API 来获取您需要的信息。

回答by Jignesh Radadiya

NSString * TimeZoneName=[[NSTimeZone localTimeZone]name];