• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* The following is a simple example that shows conversion of dates
2  * to and from a std::string.
3  *
4  * Expected output:
5  * 2001-Oct-09
6  * 2001-10-09
7  * Tuesday October 9, 2001
8  * An expected exception is next:
9  * Exception: Month number is out of range 1..12
10  */
11 
12 #include "boost/date_time/gregorian/gregorian.hpp"
13 #include <iostream>
14 #include <string>
15 
16 int
main()17 main()
18 {
19 
20   using namespace boost::gregorian;
21 
22   try {
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;
27 
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;
32 
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
39               << std::endl;
40 
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;
48   }
49   catch(std::exception& e) {
50     std::cout << "  Exception: " <<  e.what() << std::endl;
51   }
52 
53 
54   return 0;
55 }
56 
57 /*  Copyright 2001-2004: CrystalClear Software, Inc
58  *  http://www.crystalclearsoftware.com
59  *
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)
62  */
63 
64