NSDateFormatter for BST instead of GMT+1

╄→尐↘猪︶ㄣ 提交于 2019-12-04 02:29:55

问题


I want to display the following string on my time axis:

"GMT/BST"

Here's the code:

 NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"zzz"];
timeZoneString = [NSMutableString stringWithFormat:@"%@ / %@",[dateformatter stringFromDate:startDate],[dateformatter stringFromDate:endDate]];

But this gives "GMT/GMT+01:00"

What is the NSDateFormatter code to turn "GMT+01:00" into "BST" ? I can't get the right formatters to do this, having tried z|zzz|Z|ZZZ|v|V see... http://waracle.net/iphone-nsdateformatter-date-formatting-table/


回答1:


Turns out there is a built in array of 48 time zone abbreviations (e.g. 'BST') in iOS.

NSDictionary *tzDict = [NSTimeZone abbreviationDictionary]; 

There is an array of 419 time zone names in this array (e.g. 'Europe/London'):

NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];

tzDict contains the abbreviations for daylight saving time for a subset of time zone names. So the algorithm would be to check if we are in DST, then see if tzDict has an entry, and subsitute that or if not, use

[NSTimeZone abbreviation];

Here are a few other topics on time zones in general.

Daylight saving time and time zone best practices

How can I map tz database names to city and country names?

C# british summer time (BST) timezone abbreviation

http://en.wikipedia.org/wiki/List_of_tz_database_time_zones

GMT timezone conversion in objective c



来源:https://stackoverflow.com/questions/21633173/nsdateformatter-for-bst-instead-of-gmt1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!