Converting millisecond UTC to Human Readable Date_Time

后端 未结 1 837
别跟我提以往
别跟我提以往 2021-01-22 13:47

I\'m struggling to figure out how to preform a conversion with boost::date_time. I want to convert a millisecond value measured from the Unix epoch (00:00, Jan 1, 1970) to a hu

相关标签:
1条回答
  • 2021-01-22 14:00

    time_t is usually seconds since "the epoch", rather than milliseconds. If you dont care about milliseconds you should be able to do this:

    std::time_t tt = static_cast<time_t>(ticksFromEpoch/1000)
    

    If you do care about milliseconds you can either add them back in at the end (which is tricky to get right for times like 12:00:00.001 AM )

    Or you'll need to go another route. You may need to use something like this: (untested)

    boost::posix_time::ptime t2(
      boost::gregorian::date(1970,boost::date_time::Jan,1),  //The epoch
      boost::posix_time::seconds( ticksFromEpoch / 1000 ) + // Number of seconds
      boost::posix_time::milliseconds( ticksFromEpoch % 1000)  // And the micros too.
      );
    
    0 讨论(0)
提交回复
热议问题