提升日期/时间

时间:2011-08-28 18:04:57

标签: c++ datetime boost

我有以下代码,但在尝试编译时会出现语言环境错误

#include <iostream>
#include <string>
#include <locale>

#include "boost/date_time/gregorian/gregorian.hpp"
#include <boost/date_time/gregorian/parsers.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>

using namespace boost::posix_time;
using namespace boost::gregorian;

int main(int argc, char *argv[])
{
  std::string ds("2011-01-02");
  date dt(from_string(ds));

  date_facet *f=new date_facet("%Y-%m-%d");
  std::locale loc=std::locale(std::locale("en_US"),f);
  std::cout.imbue(loc);

  time_facet *facet = new time_facet("%Y-%m-%d %H:%M:%S");
  std::cout<<second_clock::local_time()<<std::endl;

  return 0;
}

这是错误消息:

In function ‘int main(int, char**)’:
test.cpp:18:1: error: ‘locale’ was not declared in this scope
test.cpp:18:8: error: expected ‘;’ before ‘loc’

编辑后,错误是:

In function ‘int main(int, char**)’:
test.cpp:18:1: error: ‘locale’ was not declared in this scope

2 个答案:

答案 0 :(得分:1)

std::前面您遗失的locale。具体地,

locale loc=locale(locale("en_US"),f);

应该成为

std::locale loc=std::locale(std::locale("en_US"),f);

您需要#include <locale>

答案 1 :(得分:1)

Boost.DateTime has a binary component that is only needed if you're using its to_string/from_string
来自getting started guide

您应首先构建它,this应该帮助