gpt4 book ai didi

c++ - boost::posix_time:检索带夏令时的时间

转载 作者:行者123 更新时间:2023-11-30 03:35:21 28 4
gpt4 key购买 nike

我使用以下方法通过 boost::posix_time 检索包含当前时间的字符串:

wstring TimeField::getActualTime() const {
// Defined elsewhere
auto m_facet = new new boost::posix_time::wtime_facet(L"%Y%m%d-%H:%M:%f");
std::locale m_locale(std::wcout.getloc(), m_facet);
// method body
std::basic_stringstream<wchar_t> wss;
wss.imbue(m_locale);
boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
wss << now;
return wss.str();
}

我得到以下结果:

20161227-22:52:238902

而在我的电脑上时间是 23:52。在我的 PC (windows 10) 中,自动调整夏令时选项已激活。

有没有办法在考虑夏令时选项的情况下检索 PC 时间(并根据方面格式化它)?

最佳答案

我同意。 DST 未生效。此外,根据定义,posix_time::ptime 不是时区感知时间戳(因此:POSIX 时间)。

但是,您当然可以要求本地时间而不是世界时间:

boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();

文档会警告您不要相信系统提供的默认时区信息和数据库,但您可能没问题。

Live On Coliru

#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <string>
#include <iostream>

namespace /*static*/ {
// Defined elsewhere
auto m_facet = new boost::posix_time::wtime_facet(L"%Y%m%d-%H:%M:%f");
std::locale m_locale(std::wcout.getloc(), m_facet);
}

std::wstring getActualTime() {
std::basic_stringstream<wchar_t> wss;
wss.imbue(m_locale);

wss << boost::posix_timemicrosec_clock::local_time();
return wss.str();
}

int main() {
std::wcout << getActualTime();
}

关于c++ - boost::posix_time:检索带夏令时的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41353233/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com