NSDateFormatter 毫秒错误

2024-03-09

我想创建一个 NSDateFormatter 来解析像这样的日期“2014-05-13 23:31:41.374577”。所以:

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];

但是,那anotherDateString is 2014-05-13 23:31:41.374000。为什么它会截断毫秒?


看起来NSDateFormatter仅适用于毫秒分辨率,对于 原因如下:

  • 通过设置断点CFDateFormatterCreateDateFromString, 一罐 看到这个函数是从调用的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()来自CFDateFormatter.c http://www.opensource.apple.com/source/CF/CF-744.19/CFDateFormatter.c这是开源的。可以看出,所有的历法计算都是使用ICU 日历课程 http://userguide.icu-project.org/datetime/calendar.

  • 它记载于日历.h https://www.opensource.apple.com/source/ICU/ICU-6.2.6/icuSources/i18n/unicode/calendar.h that Calendar uses UDate其分辨率为毫秒:

    /**
     * <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.)
     * ...
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NSDateFormatter 毫秒错误 的相关文章

随机推荐

  • PHP 中的正则表达式重复捕获组

    我正在尝试从一个带有路线的文件中获取信息 因此对于这项工作 我选择了正则表达式 但我遇到了重复信息的问题 为了做一个更好的问题 我将把我拥有的内容和我想要的内容放在一起 所以我有一个文件 Codes C Connected S Static
  • 如何在 scala 中按 Ordered 参数化 Int

    我有一个具有参数化类型的类 我想对其进行比较运算符 我认为我需要使用 Ordered 特征来实现这一点 但编译器不喜欢我使用它 假设我有以下课程 class Test T lt Ordered T def someOp t T if t l
  • 仅当在构建服务器上运行时单元测试才会失败

    为了帮助单元测试 我们已经完成了DateTime委托中的类 以便DateTime Now可以在单元测试中被覆盖 public static class SystemTime region Static Fields public stati
  • 如何检测我的 Android 应用程序中的广告拦截器?

    有什么方法可以检测广告拦截器何时在应用程序中运行 我想在我的应用程序中检测用户是否使用任何应用程序来阻止应用程序中的广告 如果是 那么我想向用户显示弹出窗口 要求他在使用应用程序时禁用广告拦截器 Android 操作系统没有提供官方方法来以
  • Typescript/babel 导入导致“_1.default 不是函数”

    我正在尝试使用https github com timmywil panzoom https github com timmywil panzoom来自使用 webpack 和 babel 编译的 typescript 项目 问题是打字稿方
  • 自定义树视图

    有没有办法自定义 winform 树视图以获得类似的东西 目的是通过父项目使用一种颜色并定义一个三角形而不是 图标来开发项目 Use TreeViewDrawMode OwnerDrawText所以缩进将由TreeView 除此之外 你应该
  • ::ng-deep 将被弃用 - 有其他选择吗?

    医生说 阴影穿透后代组合器已被弃用 并且主要浏览器和工具正在删除支持 因此 我们计划放弃对 Angular 的支持 对于 deep gt gt gt 和 ng deep 的所有 3 个 在那之前 应该首选 ng deep 以获得与工具更广泛
  • 在 JBoss 上部署 Hibernate 应用程序时出错 - 找不到适用于 jdbc 的驱动程序

    我有一个 Spring Hibernate Web 应用程序 目前在 Tomcat 上运行良好 我正尝试将其部署到 JBoss 7 1 AS 应用程序全部正确启动 但如果我设置 Hibernate Persistence 设置来创建表 则会
  • 如何使用冒号解组 XML 属性?

    我正在使用的一些 SVG XML 文件的属性名称中包含破折号和冒号 例如
  • 如何使用 jasmine 测试 $window.open

    这是我的功能 scope buildForm function majorObjectId name window open FormBuilder Index scope currentAppId form majorObjectId n
  • 如何在 perl 中创建多维数组?

    我这样创建一个多维数组 usr bin perl use warnings use strict my a1 1 2 my a2 a1 3 但事实证明我仍然得到一个一维数组 Perl 中正确的方法是什么 你得到一个一维数组 因为数组 a1在
  • 让 Collections.binarySearch() 与 CompareToIgnoreCase 一起使用?

    因此 我正在一个巨大的 ArrayList 中搜索特定的 String 值 但如果我正在查找的 String 与我传递给的 String 相等 不区分大小写 我需要 Collections binarySearch 返回一个 gt 0 的值
  • 如何使用 jQuery 获取文本输入的所有值?

    我有一个表 其中有一列预先填充的文本输入 table tr td td td td tr table
  • 如何将 zip 文件上传到 azure blob,然后在那里解压缩 [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我有很多 zip 文件 其中只有几个文件夹和 50 多个文件 如何将这些 zip 文件上传到 azure blob 然后在那里解压缩 将服务
  • mvc c# html.dropdownlist 和 viewbag

    所以我有以下 伪代码 string selectedvalud C List
  • 如何在node.js模块中实现继承?

    我正在编写 Nodejs 应用程序 它基于expressjs 我对在 Nodejs 模块中进行继承感到困惑 我想做的是创建一个模型基类 比方说 my model js module exports function my model my
  • 在 C# 中提取 .cab 文件

    我正在开发一个 C 应用程序 我需要提取一个 cab 文件 我找不到在 C 中执行此操作的库 由于许可问题 我无法使用 Microsoft Deployment Compression Cab dll I found this https
  • 联系表格 7 至 WordPress 用户数据库

    我编写了以下函数 以便在发送联系表单时将其添加到用户数据库字段中 问题是它发送了最终电子邮件 但没有向数据库输入任何内容 因此我一定在某个地方出现错误 任何帮助将不胜感激 add action wpcf7 before send mail
  • 如何向 Spring Security 用户详细信息添加其他详细信息

    我想向用户详细信息添加其他信息 例如用户的 IP 地址 有什么办法可以实现这一点吗 我尝试创建一个新的 CustomSpringUser 类 但问题是如何从 Authentication 对象获取此信息 有没有其他方法来存储经过身份验证的用
  • NSDateFormatter 毫秒错误

    我想创建一个 NSDateFormatter 来解析像这样的日期 2014 05 13 23 31 41 374577 所以 NSDateFormatter formatter NSDateFormatter alloc init form