.NET DateTime到time_t,以秒为单位 [英] .NET DateTime to time_t in seconds

查看:85
本文介绍了.NET DateTime到time_t,以秒为单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有C代码:

time1=((double)dt1-25569.0)*86400.0;

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

it's convert from TDateTime (VCL) to time_t format in seconds, so finally I need to get time_t format from .NET DateTime

关于time_t:


几乎普遍希望它是一个整数值,表示自UTC时间1970年1月1日00:00以来经过的秒数。这个
是由于历史原因,因为它对应于unix
时间戳,但是已在所有
平台的C库中广泛实现。

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#):

So to get seconds in .NET I'm doing this (F#):

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

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

or on C# (to use more popular C# tag) :

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

了解时间是从0001年1月1日12:000:00 UTC时间开始。

As far as I understand it's time from 12:00:00 Jan 1, 0001 UTC.

因此,要使用time_t格式,我需要添加 1970 年(秒)。

So to use time_t format I need to add 1970 years in seconds.

因此最终函数必须为(F#):

So final function must be (F#):

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

C#:

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

我真的很害怕我在这里犯了错误。 请检查此解决方案!(检查此操作是否正确)

I really afraid I made mistake here. Please examine this solution ! (check if this is done right)

谢谢

推荐答案

尝试

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

see

see

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

这篇关于.NET DateTime到time_t,以秒为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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