views:

83

answers:

2

Hi,

I have a vector string of dates in the from "dd-mmm-yyyy" so for example todays date would be:

  std::string today("07-Sep-2010"); 

I'd like to use the date class in boost but to create a date object the constructor for date needs to be called as follows:

 date test(2010,Sep,07);

Is there any easy/elegant way of passing dates in the format "dd-mmm-yyyy"? My first thought was to use substr and then cast it? But I've read that there's also the possibility of using 'date facets'?

Thanks!

+1  A: 

There is a builtin parser for this form of date in Boost itself, check out the docs here:

http://www.boost.org/doc/libs/1_44_0/doc/html/date_time/date_time_io.html#date_time.io_objects

date_type parse_date(...) Parameters: string_type input string_type format special_values_parser Parse a date from the given input using the given format.

string inp("2005-Apr-15");
string format("%Y-%b-%d");
date d;
d = parser.parse_date(inp, 
                      format,
                      svp);
// d == 2005-Apr-15

with inp adjusted for your needs.

Steve Townsend
What does the third argument 'svp' represent. Also should the call be format_date_parser.parse_date(inp, format, svp) ??
Wawel100
+3  A: 
include "boost/date_time/gregorian/parsers.hpp"
date test = boost::gregorian::from_us_string("07-Sep-2010")
adrianm
Thanks! Though it should be from_uk_string: // From delimited date string where with order day-month-year eg: 25-1-2002 or 25-Jan-2003 (full month name is also accepted). date from_uk_string(std::string s);
Wawel100
I'm getting a number of warnings telling me: "Function call with parameters that may be unsafe" is there any way of avoiding this?
Wawel100
Warning is for some internal stuff in the library#pragma warning( disable : 4996 )
adrianm