Standard conformant way of converting std::time_t to System::DateTime?

前端 未结 2 1883
感情败类
感情败类 2020-12-10 08:45

I have already found several answers related to converting a std::time_t value to System::DateTime and back. However, almost all answers seem to ne

2条回答
  •  心在旅途
    2020-12-10 09:11

    Here's the solution my team arrived at:

    DateTime represents the number of mixed-fractional days since midnight December 30, 1899, expressed as a double. I believe that this epoch date was used to account for the fact that 1900 was not a leap year, and it allows for an extra two days (Why two and not one? - It is not clear to me why Dec 31, 1899 was not chosen as their epoch.)

    So a DateTime of 2.50 would be equivalent to January 1, 1900 12:00:00 , (i.e. the fraction represents 1/2 the day - 12PM).

    We calculated that Jan 1, 1970 - the Unix Epoch - is 25569 days after the DateTime Epoch.

    So the equivalent formula would be:

    #include 
    System::DateTime toDateTime(time_t &t)
    {
        return 25569.0 + t / 86400.0; // ensure you use floating point math or you will truncate the fractional portion
    }
    

提交回复
热议问题