.NET DateTime to time_t in seconds

There is a C code:

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

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

about time_t:

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

So, to get the seconds in .NET, I do this (F #):

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

or in C # (to use the more popular C # tag):

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

As far as I understand, this time is from 12:00:00 on January 1, 0001 UTC.

Therefore, to use the time_t format, I need to add 1970 years in seconds.

So the final function should be (F #):

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

FROM#:

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

I am really afraid that I was mistaken here. Please study this solution! (check if this is done correctly)

thanks

+5
source share
5 answers

Does this seem a bit neater? You can make the era static datetime if you use it a lot.

 DateTime date = DateTime.Now; DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0); TimeSpan span = (date - epoch); double unixTime =span.TotalSeconds; 
+5
source

I suggest the following code. It seems better to convey the meaning of the code

 private static readonly DateTime REFERENCE = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); Int64 seconds(DateTime dt) { return (dt - REFERENCE).TotalSeconds; } 
+2
source

In C #:

 Int64 Secs(DateTime dt) { var delta = dt - new DateTime(1970, 1, 1); return Convert.ToInt64(delta.TotalSeconds); } 
+2
source

After reading the @jheriko comment on the accepted answer, I wrote a quick console application to check if the time () came out of msvcrt.dll for calculations using controlled date / time functions, which, fortunately, are not present when using UTC . Generally speaking, where possible, dates and times should be calculated and stored in UTC as a common base, and then, if necessary, converted back to the appropriate time zone for display.

For reference and to illustrate various ways to achieve the number of seconds between January 1, 1970 and now, my test code is:

 class Program { [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)] public unsafe static extern int time(int* timer); static unsafe void Main(string[] args) { DateTime now = DateTime.Now; DateTime utc_now = DateTime.UtcNow; int time_t_msvcrt = time(null); int time_t_managed = (int)Math.Floor((now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds); int time_t_managed_2 = (int)Math.Floor((utc_now - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds); Console.WriteLine(time_t_msvcrt == time_t_managed); Console.WriteLine(time_t_msvcrt == time_t_managed_2); DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime time_t_now = epoch.Add(TimeSpan.FromSeconds(time_t_msvcrt)); long now_secs = now.Ticks / 10000000L; long utc_now_secs = utc_now.Ticks / 10000000L; long time_t_now_secs = time_t_now.Ticks / 10000000L; Console.WriteLine(time_t_now_secs == now_secs); Console.WriteLine(time_t_now_secs == utc_now_secs); Console.ReadLine(); } } 

It leads to exit

 True True True True 

as was expected.

0
source

Source: https://habr.com/ru/post/890026/


All Articles