添加秒为DateTime在ArgumentOutOfRangeException有效的double结果 [英] Adding Seconds to DateTime with a Valid Double Results in ArgumentOutOfRangeException

查看:164
本文介绍了添加秒为DateTime在ArgumentOutOfRangeException有效的double结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code崩溃和烧伤,我不明白为什么:

The following code crashes and burns and I don't understand why:

DateTime dt = new DateTime(1970,1,1,0,0,0,0, DateTimeKind.Utc);
double d = double.Parse("1332958778172");

Console.Write(dt.AddSeconds(d));

谁能告诉我这是怎么回事?我似乎没有能够弄清楚为什么...

Can someone tell me what's going on? I just can't seem to be able to figure out why...

修改

此值来自Salesforce的REST API回来,从我的理解这是一个Unix纪元时间戳。 标记问题,psented为自Unix纪元(0点00分00秒UTC在1 1970年1月)数再度$ P $的时间。

This value comes back from the Salesforce REST API and from what I understand it's a Unix epoch time stamp. "The time of token issue, represented as the number of seconds since the Unix epoch (00:00:00 UTC on 1 January 1970)."

解决方案:

Salesforce的REST API实际上是在发送的毫秒的背面为 issued_at 字段执行OAuth的请求时,当他们说他们要发送秒。 ..

Salesforce REST API is in fact sending milliseconds back for the issued_at field when performing the OAuth request when they say they're sending seconds...

推荐答案

正如其他人所说,问题是,值过大。

As others have said, the problem is that the value is too large.

说完看着它,我相信它重新presents的毫秒的,因为Unix的时代,没有的的,所以你想要的:

Having looked over it, I believe it represents milliseconds since the Unix epoch, not seconds so you want:

DateTime dt = new DateTime(1970,1,1,0,0,0,0, DateTimeKind.Utc);
double d = double.Parse("1332958778172");  // Or avoid parsing if possible :)
Console.Write(dt.AddMilliseconds(d));

如果不是这样,或之前除以1000调用 AddSeconds - 但显然,这将导致数据丢失

Either that, or divide by 1000 before calling AddSeconds - but obviously that will lose data.

这篇关于添加秒为DateTime在ArgumentOutOfRangeException有效的double结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆