How do I parse "2010-04-30T00:45:48.711127" into an 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. 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. 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.
@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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With