Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost date/time

I have the following code but get the locale error when I try to compile

#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;
}

This is the error message:

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’

after the edits, the error is:

In function ‘int main(int, char**)’:
test.cpp:18:1: error: ‘locale’ was not declared in this scope
like image 766
itcplpl Avatar asked Jul 27 '26 23:07

itcplpl


1 Answers

Your missing std:: in front of locale. Specifically,

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

should become

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

And you need to #include <locale>.

like image 89
rcollyer Avatar answered Jul 30 '26 14:07

rcollyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!