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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/csharp/98551.html

猜你在找的C#相关文章