简体   繁体   English

在可可中,如何使用NSTimeZone以字符串形式获取系统时区偏移量?

[英]In Cocoa, how do I use NSTimeZone to get the system time zone offset as a string?

For PDT, I would want "-0700". 对于PDT,我想要“ -0700”。

I'm getting a date in the past to determine how long ago something happened. 我要确定过去的时间来确定发生了多久。

NSDate *then = [NSDate dateWithString:@"1976-04-01 12:34:56 -0700"]; // Note the hard-coded time zone at the end

I'll be constructing the date string elsewhere but I don't know how to access the local time zone. 我将在其他地方构造日期字符串,但是我不知道如何访问本地时区。

I read the Apple Dates and Times Programming Topics for Cocoa as well as the NSTimeZone and NSDate Class References but it's just too hard for me to put the information together. 我阅读了有关Cocoa的Apple Dates and Times编程主题以及NSTimeZone和NSDate类参考,但是对我来说,将信息汇总在一起实在太难了。 I could really use a few lines of code just to show how it's used. 我真的可以使用几行代码来展示其用法。

Update : While struggling with this, I was writing code using a Command Line template so I could try things quickly. 更新 :在为此苦苦挣扎的同时,我正在使用命令行模板编写代码,因此可以快速尝试。 I just tried my previous code on iPhone and I'm getting NSDate may not respond to '+dateWithString:' Sorry if that added to the confusion, who knew Apple would change up such a basic class. 我刚刚在iPhone上尝试了我以前的代码,但我得到NSDate可能不会响应“ + dateWithString:”很抱歉,如果这加剧了混乱,他知道苹果会改变这样的基本类。

Use NSDateFormatter to build NSDate from a string: 使用NSDateFormatter从字符串构建NSDate:

NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];

NSDate *formatterDate;
formatterDate = [inputFormatter dateFromString:@"1976-04-01 12:34:56 -0700"];

NSString *dateString = [inputFormatter stringFromDate:formatterDate];

NSLog(@"date:%@", dateString);

This way you get the local time from string, for example the date specified by the string: 这样,您可以从字符串获取本地时间,例如,字符串指定的日期:

"1976-04-01 12:34:56 -0700" “ 1976-04-01 12:34:56 -0700”

is in time zone - 0700 , (I'm in time zone GMT +1000) so I get: 在时区0700 ,(我在时区GMT +1000),所以我得到:

2009-11-17 22:13:46.480 cmdline[10593:903] date:1976-04-02 05:34:56 +1000 2009-11-17 22:13:46.480 cmdline [10593:903]日期:1976-04-02 05:34:56 +1000

The time zone offset is dependent on the date in much of the world—those parts of it that use Daylight-Saving Time/Summer Time. 时区偏移量取决于世界上大多数地区的日期,其中的某些部分使用夏时制/夏令时。

The only correct way is to generate the entire string from date and time-zone together. 唯一正确的方法是从日期和时区一起生成整个字符串。 Use NSDateFormatter for this. 为此使用NSDateFormatter。

The best way is to probably use a simple calendar formatter 最好的方法是使用简单的日历格式化程序

NSCalendarDate * date = [NSCalendarDate calendarDate];
[date setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"PDT"]];
NSLog([date descriptionWithCalendarFormat:@"%z"]);

which will output '-0700' 这将输出“ -0700”

or leave out the second line if you want the current time zone of the system (not sure which you were asking for) 或如果您想要系统的当前时区,则省略第二行(不确定您要的是哪个时区)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM