Graeme
Graeme

Reputation: 4592

Convert string to boost::gregorian::greg_month

In the Boost date time library, is there a utility function for converting month short strings (e.g. Jan, Feb, Mar, Apr) to boost::gregorian::greg_month type? The documentation for the library isn't great and I can't see anything in the headers.

Upvotes: 0

Views: 1074

Answers (2)

Nim
Nim

Reputation: 33655

A hacky work around could be:

#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>

int main(void)
{
  auto ptr = boost::gregorian::greg_month::get_month_map_ptr();

  if (ptr)
  {
    auto it = ptr->begin();
    for(; it != ptr->end(); ++it)
    {
      std::cout << it->first << " " << it->second << '\n';
    }
  }
}

This map contains mapping between all the short/long names and the short necessary to create a greg_month instance. Just need to create a little wrapper around it...

per Graeme's discovery, there is a convenience function which wraps this already boost::date_time::month_str_to_ushort<>

Upvotes: 1

CashCow
CashCow

Reputation: 31435

Yes, there are boost date time facets that can be used to create locales and put into streams.

Beware though that if you are going to print or parse a large number of dates and times you do not create the facet and locale for each one you parse.

Look here for documentation on inputting dates. Some of their examples use short month names, which appears to have %b as its format specifier

Upvotes: 0

Related Questions