I’m needing to convert a DateTime to a Unix timestamp. So I googled it looking for some example code
In just about all the results I see, they use double as the return for such a function, even when explicitly using floor to convert it to an integer. Unix timestamps are always integers. So what problem is there with using either long or int instead of double?
static double ConvertToUnixTimestamp(DateTime date)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = date - origin;
return Math.Floor(diff.TotalSeconds);
}
Usually, I would implement it with an unsigned long instead of requiring the user to round up or down and cast to an int or long. One reason someone might want a double is if a structure similar to timeval were being used such as in gettimeofday. It allows for sub-second precision…