NSDateFormatter for BST instead of GMT+1

2020-03-31 08:12发布

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条回答
forever°为你锁心
2楼-- · 2020-03-31 08:31

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

查看更多
登录 后发表回答