views:

443

answers:

2

how do i convert ticks to time_t or filetime().

Please suggest

thanks

+1  A: 

For time_t:

long ticks =  633918528000000000;
DateTime target = new DateTime(ticks);
DateTime start = new DateTime(1970, 1, 1, 0, 0, 0);
double time_t = (target - start).TotalSeconds;
Stuart Dunkeld
+3  A: 

time_t is defined as the number of seconds from the Unix epoch date January 1, 1970 @ 00:00 UTC. So, you basically need to figure out how much time your ticks represent and subtract the epoch time. The following should do it.

double GetTimeTSecondsFrom(long ticks)
{
    DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    return (new DateTime(ticks) - epoch).TotalSeconds;
}
Steve Guidi
this is great... also plese can u tell me how to convert from time_t to ticksthanks
Better answer than mine, as I forgot to specify UTC..
Stuart Dunkeld
To perform the inverse operation, just add `epoch.Ticks` to `TimeSpan.FromSeconds(time_t_value).TotalTicks`.
Steve Guidi