加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – .NET DateTime to time_t,以秒为单位

发布时间:2020-12-15 18:27:30 所属栏目:百科 来源:网络整理
导读:有C代码: time1=((double)dt1-25569.0)*86400.0; 它在几秒钟内从TDateTime(VCL)转换为time_t格式,所以最后我需要从.NET DateTime获取time_t格式 关于time_t: It is almost universally expected to be an integral value representing the number of secon
有C代码:
time1=((double)dt1-25569.0)*86400.0;

它在几秒钟内从TDateTime(VCL)转换为time_t格式,所以最后我需要从.NET DateTime获取time_t格式

关于time_t:

It is almost universally expected to be an integral value representing
the number of seconds elapsed since 00:00 hours,Jan 1,1970 UTC. This
is due to historical reasons,since it corresponds to a unix
timestamp,but is widely implemented in C libraries across all
platforms.

所以要在.NET中获得秒数,我正在做这个(F#):

let seconds(dt : DateTime) =
    (dt.Ticks/10000000L)

或者在C#上(使用更流行的C#标签):

Int64 SEOnds(DateTime dt)
{ return (dt.Ticks/ ((Int64)10000000)); } 
// hope it works,but please correct if I mistaken

据我所知,它是从0001 UTC 1月1日12:00:00开始的时间.

所以要使用time_t格式,我需要在几秒钟内添加1970年.

所以最终函数必须是(F#):

let seconds(dt : DateTime) =
    (dt.Ticks/10000000L) + 31536000*1970

C# :

Int64 SEOnds(DateTime dt)
{ return (dt.Ticks/ ((Int64)10000000)) + 31536000*1970; }

我真的害怕我在这里弄错了.请检查这个解决方案! (检查这是否正确)

谢谢

解决方法

尝试
(dt - new DateTime (1970,1,1)).TotalSeconds

看到

> http://msdn.microsoft.com/en-us/library/system.timespan.totalseconds.aspx
> http://msdn.microsoft.com/en-us/library/xcfzdy4x.aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读