Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

objective c - NSTimeZone: what is the difference between localTimeZone and systemTimeZone?

Under NSTimeZone class, there is both +localTimeZone and +systemTimeZone. I did a test on iphone simulator, both return NSTimeZone object indicating the same timezone. What is the difference? Which one I should use to find out the timezone setting of the iPhone? Thanks

My test:

NSLog(@"Local Time Zone %@",[[NSTimeZone localTimeZone] name]);
NSLog(@"System Time Zone %@",[[NSTimeZone systemTimeZone] name]);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The user (or your application or an other application) may change either the default time zone for an application (using +[NSTimeZone setDefaultTimeZone]) or the system time zone (using System Preferences) at any time. +[NSTimeZone localTimeZone] returns a proxy that will always act as if it is the current default time zone for the application, even if that default changes. You could change the default time zone for an application to make it behave as if it were in a different time zone.

+[NSTimeZone systemTimeZone] returns the current system time zone (as set using System Preferences). In most cases, these will be the same (the app's default time zone is set to the system time zone at app startup, I believe).

If you want to know the system's time zone setting, you probably want to use +[NSTimeZone systemTimeZone]. If you just want the correct time zone for your app to work in, you probably want +[NSTimeZone localTimeZone].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...