C++ boost::posix_时间:使用夏令时检索时间
我使用以下方法使用C++ boost::posix_时间:使用夏令时检索时间,c++,boost,dst,boost-date-time,C++,Boost,Dst,Boost Date Time,我使用以下方法使用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 b
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();
}
wstring TimeField::getActualTime()常量{
//在别处定义
自动m_方面=新的boost::posix_时间::wtime_方面(L“%Y%m%d-%H:%m:%f”);
std::locale m_locale(std::wcout.getloc(),m_facet);
//方法体
std::基本流wss;
wss.imbue(m_语言环境);
boost::posix_time::ptime now=boost::posix_time::microsec_clock::universal_time();
wss我同意。DST无效。而且,根据定义,posix_时间::ptime
不是时区感知时间戳(因此:posix时间)
但是,您当然可以要求当地时间,而不是世界时:
boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
文档将警告您不要信任系统提供的默认时区信息和数据库,但您可能会没事
#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();
}
#包括
#包括
#包括
#包括
名称空间/*静态*/{
//在别处定义
自动m_方面=新的boost::posix_时间::wtime_方面(L“%Y%m%d-%H:%m:%f”);
std::locale m_locale(std::wcout.getloc(),m_facet);
}
std::wstring getActualTime(){
std::基本流wss;
wss.imbue(m_语言环境);
wss夏令时当前未生效。您可能需要解决另一个问题。谢谢,它似乎有效。我必须研究不信任系统提供的默认时区的原因…@Jepessen我认为如果服务器在您的控制下,通常是可以的。这只适用于某些时间点有问题的应用程序安全性含义