Boost logo

Boost Users :

Subject: [Boost-users] [locale] how to read a local date from an istream?
From: Frédéric (ufospoke_at_[hidden])
Date: 2017-11-02 10:09:44


Hi,

With boost::locale we can output a localized date:
double d=...;
std::cout<<boost::locale::as::date<<d;

but how can I read a date from a string?
std::cin>>boost::locale::as::date>>d;
does not work. It stops reading at the first "/".

For example, this small program
#include <iostream>
#include <sstream>
#include <boost/locale/formatting.hpp>
#include <boost/locale/generator.hpp>

int main(int argc, char **argv) {
  boost::locale::generator gen;
  gen.categories();
  auto loc = gen("");
  double date = 5.; // default value to see if it changes
  std::istringstream iss("2/11/2017"); // November 1st, 2017 in French
  iss.imbue(loc);
  iss >> boost::locale::as::date >> boost::locale::as::local_time >> date;
  std::cout.imbue(loc);
  std::cout << "as_posix: " << boost::locale::as::posix << date << "\n"
            << "as_date: " << boost::locale::as::date << date << "\n";
  return 0;
}

outputs
as_posix: 2
as_date: 01/01/1970

instead of
as_posix: 1509617233 (may be different)
as_date: 02/11/2017

F


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net