Convert time_t to tics

I have a function that converts ticks to time_t format

long ticks = DateTime.Now.Ticks; long tt = GetTimeTSecondsFrom(ticks); long GetTimeTSecondsFrom(long ticks) { DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); return (long) (new DateTime(ticks) - epoch).TotalSeconds; } 

Now I am confused how to convert it back to ticks using some mathematical formula, not using a function.

Any suggestions...??

thanks

Let me take a general example and explain. DateTime.Now.Ticks gives me a value of 633921719670980000, which is in tick

then I convert this to time_t with the above function and get tt = 1256575167

now i want to convert this back to 633921719670980000. for this i need a formula

0
source share
3 answers

The date 1970-01-01 00:00:00 Z is exactly 62 135 596 800 seconds (621 355 968 000 000 000 ticks), therefore, to convert the DateTime number to time_t value, you can simply scale it (divide by 10 000 000), to get seconds, and then subtract this offset.

To go the other way, do the opposite: add seconds of offset to the time_t value, then scale (multiply by 10,000,000) to get ticks.

+2
source

The answer was given as a comment on your original question regarding the conversion of ticks to time_t.

 DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); return TimeSpan.FromSeconds(time_t_value).Ticks + epoch.Ticks; 
+3
source

From the MSDN documentation:

One tick represents one hundred nanoseconds or one tenth of a millionth of a second. In milliseconds, there are 10,000 ticks.

So, the function to convert seconds to ticks will look something like this:

 long GetTicksFromSeconds(long seconds) { return seconds * 10000000; } 
0
source

All Articles