0

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.

Graeme
  • 4,514
  • 5
  • 43
  • 71

2 Answers2

1

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<>

Nim
  • 33,299
  • 2
  • 62
  • 101
  • Just found a function already wrappin this up: boost::date_time::month_str_to_ushort. Thanks for the help! – Graeme Feb 24 '12 at 14:31
  • @Graeme, aha - it had to be there somewhere - it's a pretty trivial function... :) – Nim Feb 24 '12 at 14:32
0

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

CashCow
  • 30,981
  • 5
  • 61
  • 92