简体   繁体   English

在Objective-C中解析ISO8601日期(iPhone OS SDK)

[英]Parse ISO8601 date in Objective-C (iPhone OS SDK)

How do I parse "2010-04-30T00:45:48.711127" into an NSDate? 如何将“2010-04-30T00:45:48.711127”解析为NSDate? (and maintain all precision) (并保持所有精度)

You have your work cut out for you. 你的工作已经完成了。

NSDate will throw out any values past 3 decimal places for seconds. NSDate会抛出超过3位小数的任何值。 You can create a subclass of NSDate to hold on to that precision but you'll also need to implement your own parsing and custom formatters to input and display it since NSDateFormatter and CFDateFormatter , which it is built on, will also truncate precision after 3 decimal places. 您可以创建NSDate的子类来保持该精度,但是您还需要实现自己的解析和自定义格式化程序来输入和显示它,因为它构建的NSDateFormatterCFDateFormatter也会在3位小数后截断精度地方。 Depending on what you're doing though that shouldn't be all that hard. 取决于你正在做什么,虽然这应该不是那么难。

This is a simple subclass (not implementing NSCoding or NSCopying ) that will hold on to all the precision you give it. 这是一个简单的子类(不实现NSCodingNSCopying ),它将保持您提供的所有精度。

@interface RMPreciseDate : NSDate {
    double secondsFromAbsoluteTime;
}

@end

@implementation RMPreciseDate

- (NSTimeInterval)timeIntervalSinceReferenceDate {
    return secondsFromAbsoluteTime;
}

- (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secsToBeAdded {
    if (!(self = [super init]))
        return nil;

    secondsFromAbsoluteTime = secsToBeAdded;

    return self;
}

@end

You can then ask for the -timeIntervalSince1970 to get UNIX epoch time. 然后,您可以请求-timeIntervalSince1970获取UNIX纪元时间。

There is an ISO8601 date/time parser class already out there, but since it's using NSDateComponents to generate its date it's limited to full-second precision currently, but you could use it as a starting point perhaps to create more precise representations. 已经有一个ISO8601日期/时间解析器类 ,但由于它使用NSDateComponents来生成其日期,它当前仅限于全秒精度,但您可以将其用作起点,或许可以创建更精确的表示。

我打算尝试这个ISO 8601解析器和类似情况下的解析器: http ://boredzo.org/iso8601parser/

It seems the NSDate only has millisecond precision. 似乎NSDate只有毫秒精度。

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'hh:mm:ss.SSSSSS"];

    NSDate *date = [dateFormatter dateFromString:@"2010-04-30T00:45:48.711127"];

    NSLog(@"%@", date);

    NSString *string = [dateFormatter stringFromDate:date];

    NSLog(@"%@", string);

    [pool drain];
    return 0;
}

That code yields the following console output: 该代码产生以下控制台输出:

Program loaded.
run
[Switching to process 27202]
Running…
2010-05-08 20:02:46.342 TestNSDate[27202:a0f] 2010-04-30 00:45:48 -0700
2010-05-08 20:02:46.344 TestNSDate[27202:a0f] 2010-04-30T12:45:48.711000

Debugger stopped.
Program exited with status value:0.

So "2010-04-30T00:45:48.711127" turning into "2010-04-30T00:45:48.711000" is probably not what you have in mind. 所以"2010-04-30T00:45:48.711127"变成"2010-04-30T00:45:48.711000"可能不是你的想法。

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

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