]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/date_time/example/gregorian/dates_as_strings.cpp
1 /* The following is a simple example that shows conversion of dates
2 * to and from a std::string.
7 * Tuesday October 9, 2001
8 * An expected exception is next:
9 * Exception: Month number is out of range 1..12
12 #include "boost/date_time/gregorian/gregorian.hpp"
20 using namespace boost::gregorian
;
23 // The following date is in ISO 8601 extended format (CCYY-MM-DD)
24 std::string
s("2001-10-9"); //2001-October-09
25 date
d(from_simple_string(s
));
26 std::cout
<< to_simple_string(d
) << std::endl
;
28 //Read ISO Standard(CCYYMMDD) and output ISO Extended
29 std::string
ud("20011009"); //2001-Oct-09
30 date
d1(from_undelimited_string(ud
));
31 std::cout
<< to_iso_extended_string(d1
) << std::endl
;
33 //Output the parts of the date - Tuesday October 9, 2001
34 date::ymd_type ymd
= d1
.year_month_day();
35 greg_weekday wd
= d1
.day_of_week();
36 std::cout
<< wd
.as_long_string() << " "
37 << ymd
.month
.as_long_string() << " "
38 << ymd
.day
<< ", " << ymd
.year
41 //Let's send in month 25 by accident and create an exception
42 std::string
bad_date("20012509"); //2001-??-09
43 std::cout
<< "An expected exception is next: " << std::endl
;
44 date
wont_construct(from_undelimited_string(bad_date
));
45 //use wont_construct so compiler doesn't complain, but you wont get here!
46 std::cout
<< "oh oh, you shouldn't reach this line: "
47 << to_iso_string(wont_construct
) << std::endl
;
49 catch(std::exception
& e
) {
50 std::cout
<< " Exception: " << e
.what() << std::endl
;
57 /* Copyright 2001-2004: CrystalClear Software, Inc
58 * http://www.crystalclearsoftware.com
60 * Subject to the Boost Software License, Version 1.0.
61 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)