【问题标题】:Converting an "NS.time" from plist to an actual time将“NS.time”从 plist 转换为实际时间
【发布时间】:2014-12-04 03:28:45
【问题描述】:

我可以在 plist 文件中找到以下数据:

"NS.time" 是数字值的键。

NSLog(@"Type: %@", [[obj objectForKey:@"NS.time"] class]);
NSLog(@"My dictionary: %@", obj);

2014-10-09 11:04:49.409 MyApp[3792:303] Type: __NSCFNumber
2014-10-09 10:34:46.393 MyApp[3644:303] My dictionary: {
    "NS.time" = "434488287.651975";
}

这是几秒钟?毫秒?微秒? 如何从中获取正确的时间信息?

编辑

我使用 Python 找到了正确的日期:

>>> datetime.datetime(2001, 1, 1) + datetime.timedelta(seconds=434488287.651975)
datetime.datetime(2014, 10, 8, 19, 11, 27, 651975)

如何使用 Objective-C 做到这一点?

【问题讨论】:

  • 谁把信息放在了plist中?
  • 自参考日期起的秒数,通常参考日期为 1.1.2001 左右...

标签: python objective-c macos


【解决方案1】:
  1. 为 2001、1、1 创建一个 NSDate。
  2. 使用 NSDate 的- (id)dateByAddingTimeInterval:(NSTimeInterval)seconds 获取新日期。 NSDate *tagDate = [referenceDate dateByAddingTimeInterval:434488287.651975];

由 OP 编辑​​:

这是我使用的完整代码:

NSTimeInterval interval = [[obj objectForKey:@"NS.time"] doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:interval];

dateWithTimeIntervalSinceReferenceDate 在这种情况下非常有用。它创建并返回一个NSDate 对象,该对象设置为从格林威治标准时间 2001 年 1 月 1 日的第一时刻开始的给定秒数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-19
    • 2011-07-29
    • 2020-07-02
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多