iPhone:将 Oracle 时间戳转换为 NSDate 或 Unix 时间戳(例如双精度)

发布于 2024-08-22 09:13:12 字数 599 浏览 1 评论 0原文

有没有一种好+简单的方法将 Oracle Timestamp 转换为 iPhone 上的 NSDate 对象?我已经要求我的客户给我一个时间戳为 Unix 时间戳的数据库(双倍为 0 = 1970 开始),但这对他们来说似乎是一个问题。谢谢。

注意:您可以轻松地将 Unix 时间戳转换为 NSDate,

[NSDate dateWithTimeIntervalSince1970: timestamp]  // timestamp is a "double"

我需要做的是将 Oracle 时间戳转换为双精度,即 Unix 时间戳。剩下的就很容易了。

更多信息:我需要使用以 Oracle 时间戳格式表示日期的 NSString 在 iPhone 上运行 Objective-C 代码。 另一个 StackOverflow 线程 建议使用 NSDateFormatter ,但我不知道要使用什么格式,并且该线程中建议的格式化程序的初始化方法会为我生成警告。

Is there a good+easy way to convert an Oracle Timestamp to an NSDate object on the iPhone? I have asked my customer to give me a DB with timestamps as Unix Timestamp (doubles with 0 = start of 1970) but it seems to be a problem for them. Thanks.

Note: You can easily convert a Unix Timestamp to an NSDate with

[NSDate dateWithTimeIntervalSince1970: timestamp]  // timestamp is a "double"

What I need to do is convert the Oracle timestamp to a double, aka Unix Timestamp. The rest is easy.

More info: I need Objective-C code to run on the iPhone using NSStrings that represent dates in Oracle Timestamp format. Another StackOverflow thread suggests using NSDateFormatter, but I don't know what format to use and the initialization method for the formatter suggested in that thread generates a warning for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原谅过去的我 2024-08-29 09:13:12

我不确定是否转换为 NSDate,但您可以轻松地将 Oracle 日期转换为 unix 时间戳。下面的 SQL 片段可以做到这一点:

(my_date - to_date('01/01/1970','DD/MM/YYYY')) * 86400

I'm not sure about the converting to an NSDate, but you can convert an Oracle date to a unix timestamp easily. The following SQL snippet will do it:

(my_date - to_date('01/01/1970','DD/MM/YYYY')) * 86400
私野 2024-08-29 09:13:12

看起来我最好的选择是使用以下内容:

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];    
[formatter setDateFormat:@"MM/dd/yyyy HH:mm a"];

我不确定 Oracle 时间戳中有多少差异,因此如果上述方法存在问题,我很想知道。

感谢其他答案。我将保留这个问题以邀请其他方法。

It looks like my best bet is to use the following:

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];    
[formatter setDateFormat:@"MM/dd/yyyy HH:mm a"];

I'm not sure how much variance there is in Oracle timestamps, so if there is a problem with the above approach, I'd love to know.

Thanks for the other answers. I will leave the question open to invite other approaches.

囍笑 2024-08-29 09:13:12

遗憾的是您已经接受了这个答案,但这是最好的方法:

NSDate *dateTraded = [NSDate dateWithTimeIntervalSince1970 :[timestampVal integerValue]];

...其中 timestampVal 是表示时间戳值的 NSString 对象。

It's a shame that you accepted that answer already but this is the best way to do it:

NSDate *dateTraded = [NSDate dateWithTimeIntervalSince1970 :[timestampVal integerValue]];

...where timestampVal is an NSString object representing a timestamp value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文