我试图在我的代码中处理日期和时间,并且已经指向了 boost 库的方向——特别是 boost::locale::date_time (部分是因为这让我避免了夏令时的怪异,这让我以前的实施困难)。
但是,我得到的结果不一致。当我将日期存储在 date_time 对象中,然后尝试从中获取数据时,这是不正确的。这是一个例子:
#include <boost\\asio\\error.hpp>
#include <boost\\locale.hpp>
using namespace std;
int main()
{
// Necessary to avoid bad_cast exception - system default should be fine
boost::locale::generator gen;
std::locale::global(gen(""));
// Create date_time of 12/19/2016
boost::locale::date_time dt = boost::locale::period::year(2016) + boost::locale::period::month(12) + boost::locale::period::day(19);
unsigned int month = dt.get(boost::locale::period::month());
unsigned int day = dt.get(boost::locale::period::day());
unsigned int year = dt.get(boost::locale::period::year());
cout << month << "/" << day << "/" << year << endl;
// Expected output: 12/19/2016
// Actual output: 0/19/2017
}
我究竟做错了什么?我只想提取保存的天、月、年、小时等。
谢谢你。
编辑:我最初可能以不正确的方式设置 date_time 。假设我有整数(不是字符串)格式的所有相关数据,是否有更好的方法来显式设置日期时间(例如,设置为 2016 年 12 月 19 日)?