OGeek|极客世界-中国程序员成长平台

标题: c# - .NET 日期如何转换为 NSTimeInterval? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:42
标题: c# - .NET 日期如何转换为 NSTimeInterval?

我们有 .NET 代码 (C#) 写入 SQLite 数据库,然后由带有 Objective-c 的 iOS (iPhone) 应用程序读取。

SQLite 中使用的日期格式是 NSTimeInterval(使用 dateWithIntervalSince1970 表示 NSDate),因为它在 iPhone 上很有效。

如何将.NET DateTime 转换为 NSTimeInterval 以便在使用 dateWithIntervalSince1970 提取时日期正确?

注意事项: 1)我试图搜索 NSTimeInterval 的基本工作原理,但只找到这个不透明的文档: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html

2) 尽管 C# 代码是理想的我很乐意使用从 3 个整数(年/月/日)到 double 的伪代码。



Best Answer-推荐答案


我认为你的意思是 NSTimeIntervalNSTimeInterval 表示时间跨度,以秒为单位,因此它在 .NET 中的逻辑等价物是 System.TimeSpan 而不是 System.DateTime。但是,如果您将其指定为 1970 年 1 月 1 日 00:00:00 GMT 以来的时间跨度,这就是您使用 -timeIntervalSince1970,则它与System.DateTime之间的转换成为可能。

要在 System.DateTime 和 POSIX 时间戳(在这种情况下 NSTimeInterval 加上亚秒精度)之间进行转换,您可能需要这样的东西:

DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
DateTime dt = ...;
double intervalSince1970 = (dt - unixEpoch).TotalSeconds;

走另一条路:

DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
double intervalSince1970 = ...;
DateTime dt = unixEpoch + TimeSpan.FromSeconds(intervalSince1970);

关于c# - .NET 日期如何转换为 NSTimeInterval?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7734315/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4