Converting time_duration to DATE

后端 未结 2 1171
孤街浪徒
孤街浪徒 2021-01-25 12:47

I want to convert a time_duration to a DATE format, which is the number of days since 1899, 12, 30.

DATE date_from_duration(time_duration td)
{
   double days =          


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 13:41

    I might be missing some of the complexity, but it seems really simple to me:

    Live On Coliru

    #include 
    #include 
    using DATE = double;
    
    boost::posix_time::ptime pTime_from_DATE(double date)
    {
        static const boost::posix_time::ptime::date_type base_date(1899, boost::gregorian::Dec, 30);
        return boost::posix_time::ptime(
                base_date, 
                boost::posix_time::milliseconds(date * 1000 * 60 * 60 * 24));
    }
    
    int main() {
        boost::posix_time::time_duration duration(1007645, 15, 0);
    
        DATE date = duration.total_milliseconds() / 1000.0 / 60 / 60 / 24;
    
        std::cout << date << ": " << pTime_from_DATE(date);
    }
    

    Prints

    41985.2: 2014-Dec-12 05:15:00
    

    See it Live On Coliru

提交回复
热议问题