Boost 最后一次写入时间是一个小时

Boost 最后一次写入时间是一个小时,boost,time,boost-filesystem,Boost,Time,Boost Filesystem,我试图解决以下代码中的一个bug,在这里,我与boost上次的写入时间相差一个小时 为了更好地解释它:我创建了一个文件,然后尝试提取它是用boost::filesystem::path创建的时间 void PrintTime(boost::filesystem::path _file) { time_t sys_time{ last_write_time(_file) }; ptime p_time{ boost::posix_time::from_time_t(sys_time

我试图解决以下代码中的一个bug,在这里,我与boost上次的写入时间相差一个小时

为了更好地解释它:我创建了一个文件,然后尝试提取它是用boost::filesystem::path创建的时间

void PrintTime(boost::filesystem::path _file) {
    time_t sys_time{ last_write_time(_file) };
    ptime p_time{ boost::posix_time::from_time_t(sys_time) };
    boost::posix_time::time_duration time_dur{ p_time.time_of_day() };

    long h{ time_dur.hours() }; //1a
    long m{ time_dur.minutes() };
    long s{ time_dur.seconds() };

    //...print h, m, s.
    }

    //1a: Here when for example the time I expect is 12:34:56,
    //I always get 11:34:56
知道为什么吗? 在boost最后写入时间的某个地方有时区吗?
当我通过系统检查文件时,我的操作系统显示正确的时间。

您必须转换为“演示”时区,如“当[您]通过系统检查文件时”。文件系统的时间戳是UTC时间

如果你这样做了

std::cout << boost::posix_time::second_clock::local_time() << "\n";
std::cout << boost::posix_time::second_clock::universal_time() << "\n";
修正:
添加了一个基于Brilliant的建议修复!伟大和快速的回答,谢谢你的作品完美!
2018-Feb-27 16:03:12
2018-Feb-27 15:03:12
#include <boost/date_time/c_local_time_adjustor.hpp>

void PrintTime(boost::filesystem::path _file) {
    using boost::posix_time::ptime;
    using adj = boost::date_time::c_local_adjustor<ptime>;

    time_t const sys_time = last_write_time(_file);
    ptime const utc       = boost::posix_time::from_time_t(sys_time);
    ptime const local     = adj::utc_to_local(utc);
#include <boost/filesystem.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/date_time/posix_time/conversion.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/c_local_time_adjustor.hpp>

void PrintTime(boost::filesystem::path _file) {
    using boost::posix_time::ptime;
    using adj = boost::date_time::c_local_adjustor<ptime>;

    time_t const sys_time = last_write_time(_file);
    ptime const utc       = boost::posix_time::from_time_t(sys_time);
    ptime const local     = adj::utc_to_local(utc);

    std::cout << "utc: " << utc << "\n";
    std::cout << "local: " << local << "\n";
    {
        long h{ local.time_of_day().hours() };
        long m{ local.time_of_day().minutes() };
        long s{ local.time_of_day().seconds() };

        //...print h, m, s.
        std::cout << h << ":" << m << ":" << s << '\n';
    }
}

int main() {
    PrintTime("main.cpp");
}
utc: 2018-Feb-27 15:19:45
local: 2018-Feb-27 16:19:45
16:19:45