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
217 views
in Technique[技术] by (71.8m points)

ios - NSDateFormatter milliseconds bug

I want to create a NSDateFormatter that should parse dates like this "2014-05-13 23:31:41.374577". So:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSSSSS";
NSString *dateString = @"2014-05-13 23:31:41.374577";
NSDate *date = [formatter dateFromString:dateString];
NSString *anotherDateString = [formatter stringFromDate:date];

However, the anotherDateString is 2014-05-13 23:31:41.374000. Why does it truncate the milliseconds?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

It seems that NSDateFormatter works only with millisecond resolution, for the following reasons:

  • By setting a breakpoint in CFDateFormatterCreateDateFromString, one can see that this function is called from dateFromString::

    (lldb) bt
    * thread #1: tid = 0x26d03f, 0x018f47d0 CoreFoundation`CFDateFormatterCreateDateFromString, queue = 'com.apple.main-thread', stop reason = breakpoint 3.1
        frame #0: 0x018f47d0 CoreFoundation`CFDateFormatterCreateDateFromString
        frame #1: 0x0116e0ea Foundation`getObjectValue + 248
        frame #2: 0x0116dfc7 Foundation`-[NSDateFormatter getObjectValue:forString:errorDescription:] + 206
        frame #3: 0x0116879f Foundation`-[NSDateFormatter dateFromString:] + 71
      * frame #4: 0x00002d56 foo`main(argc=1, argv=0xbfffee54) + 182 at main.mm:25
    
  • CFDateFormatterCreateDateFromString() is from CFDateFormatter.c which is open source. One can see that all calendrical calculations are made using the ICU Calendar Classes.

  • It is stated in calendar.h that Calendar uses UDate which has a millisecond resolution:

    /**
     * <code>Calendar</code> is an abstract base class for converting between
     * a <code>UDate</code> object and a set of integer fields such as
     * <code>YEAR</code>, <code>MONTH</code>, <code>DAY</code>, <code>HOUR</code>,
     * and so on. (A <code>UDate</code> object represents a specific instant in
     * time with millisecond precision. See UDate
     * for information about the <code>UDate</code> class.)
     * ...
    

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

...