.NET DateTime to time_t in seconds
try
(dt - new DateTime (1970, 1, 1)).TotalSeconds
see
- http://msdn.microsoft.com/en-us/library/system.timespan.totalseconds.aspx
- http://msdn.microsoft.com/en-us/library/xcfzdy4x.aspx
this seems a little tidier? You could make the epoch a static datetime if you will be using it a lot.
DateTime date = DateTime.Now;
DateTime epoch = new DateTime(1970, 1, 1);
TimeSpan span = (date - epoch);
double unixTime = span.TotalSeconds;
I suggest the following code. It seems to better transport 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;
}