微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

有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; }

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

谢谢

解决方法

原文地址:https://www.jb51.cc/csharp/98551.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐