• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <iostream>
2 #include <boost/date_time/local_time/local_time.hpp>
3 
main()4 int main(){
5   using namespace boost::gregorian;
6   using namespace boost::posix_time;
7   using namespace boost::local_time;
8   using namespace std;
9 
10   /****** basic use ******/
11   date d(2004, Feb, 29);
12   time_duration td(12,34,56,789);
13   stringstream ss;
14   ss << d << ' ' << td;
15   ptime pt(not_a_date_time);
16   cout << pt << endl; // "not-a-date-time"
17   ss >> pt;
18   cout << pt << endl; // "2004-Feb-29 12:34:56.000789"
19   ss.str("");
20   ss << pt << " EDT-05EDT,M4.1.0,M10.5.0";
21   local_date_time ldt(not_a_date_time);
22   ss >> ldt;
23   cout << ldt << endl; // " 2004-Feb-29 12:34:56.000789 EDT"
24 
25 
26   /****** format strings ******/
27   local_time_facet* output_facet = new local_time_facet();
28   local_time_input_facet* input_facet = new local_time_input_facet();
29   ss.imbue(locale(locale::classic(), output_facet));
30   ss.imbue(locale(ss.getloc(), input_facet));
31   output_facet->format("%a %b %d, %H:%M %z");
32   ss.str("");
33   ss << ldt;
34   cout << ss.str() << endl; // "Sun Feb 29, 12:34 EDT"
35 
36   output_facet->format(local_time_facet::iso_time_format_specifier);
37   ss.str("");
38   ss << ldt;
39   cout << ss.str() << endl; // "20040229T123456.000789-0500"
40   output_facet->format(local_time_facet::iso_time_format_extended_specifier);
41   ss.str("");
42   ss << ldt;
43   cout << ss.str() << endl; // "2004-02-29 12:34:56.000789-05:00"
44 
45   // extra words in format
46   string my_format("The extended ordinal time %Y-%jT%H:%M can also be represented as %A %B %d, %Y");
47   output_facet->format(my_format.c_str());
48   input_facet->format(my_format.c_str());
49   ss.str("");
50   ss << ldt;
51   cout << ss.str() << endl;
52 
53   // matching extra words in input
54   ss.str("The extended ordinal time 2005-128T12:15 can also be represented as Sunday May 08, 2005");
55   ss >> ldt;
56   cout << ldt << endl; // cout is using default format "2005-May-08 12:15:00 UTC"
57 
58   /****** content strings ******/
59   // set up the collections of custom strings.
60   // only the full names are altered for the sake of brevity
61   string month_names[12] = { "january", "february", "march",
62                              "april", "may", "june",
63                              "july", "august", "september",
64                              "october", "november", "december" };
65   vector<string> long_months(&month_names[0], &month_names[12]);
66   string day_names[7] = { "sunday", "monday", "tuesday", "wednesday",
67                           "thursday", "friday", "saturday" };
68   vector<string> long_days(&day_names[0], &day_names[7]);
69 
70   //  create date_facet and date_input_facet using all defaults
71   date_facet* date_output = new date_facet();
72   date_input_facet* date_input = new date_input_facet();
73   ss.imbue(locale(ss.getloc(), date_output));
74   ss.imbue(locale(ss.getloc(), date_input));
75 
76   // replace names in the output facet
77   date_output->long_month_names(long_months);
78   date_output->long_weekday_names(long_days);
79 
80   // replace names in the input facet
81   date_input->long_month_names(long_months);
82   date_input->long_weekday_names(long_days);
83 
84   // customize month, weekday and date formats
85   date_output->format("%Y-%B-%d");
86   date_input->format("%Y-%B-%d");
87   date_output->month_format("%B"); // full name
88   date_input->month_format("%B"); // full name
89   date_output->weekday_format("%A"); // full name
90   date_input->weekday_format("%A"); // full name
91 
92   ss.str("");
93   ss << greg_month(3);
94   cout << ss.str() << endl; // "march"
95   ss.str("");
96   ss << greg_weekday(3);
97   cout << ss.str() << endl; // "tuesday"
98   ss.str("");
99   ss << date(2005,Jul,4);
100   cout << ss.str() << endl; // "2005-july-04"
101 
102 
103   /****** special values ******/
104   // reset the formats to defaults
105   output_facet->format(local_time_facet::default_time_format);
106   input_facet->format(local_time_input_facet::default_time_input_format);
107 
108   // create custom special_values parser and formatter objects
109   // and add them to the facets
110   string sv[5] = {"nadt","neg_inf", "pos_inf", "min_dt", "max_dt" };
111   vector<string> sv_names(&sv[0], &sv[5]);
112   special_values_parser sv_parser(sv_names.begin(), sv_names.end());
113   special_values_formatter sv_formatter(sv_names.begin(), sv_names.end());
114   output_facet->special_values_formatter(sv_formatter);
115   input_facet->special_values_parser(sv_parser);
116 
117   ss.str("");
118   ldt = local_date_time(not_a_date_time);
119   ss << ldt;
120   cout << ss.str() << endl; // "nadt"
121 
122   ss.str("min_dt");
123   ss >> ldt;
124   ss.str("");
125   ss << ldt;
126   cout << ss.str() << endl; // "1400-Jan-01 00:00:00 UTC"
127 
128   /****** date/time periods ******/
129   // reset all formats to defaults
130   date_output->format(date_facet::default_date_format);
131   date_input->format(date_facet::default_date_format);
132   date_output->month_format("%b");    // abbrev
133   date_input->month_format("%b");     // abbrev
134   date_output->weekday_format("%a");  // abbrev
135   date_input->weekday_format("%a");   // abbrev
136 
137   // create our date_period
138   date_period dp(date(2005,Mar,1), days(31)); // month of march
139 
140   // custom period formatter and parser
141   period_formatter per_formatter(period_formatter::AS_OPEN_RANGE,
142                                  " to ", "from ", " exclusive", " inclusive" );
143   period_parser per_parser(period_parser::AS_OPEN_RANGE,
144                            " to ", "from ", " exclusive" , " inclusive" );
145 
146   // default output
147   ss.str("");
148   ss << dp;
149   cout << ss.str() << endl; // "[2005-Mar-01/2005-Mar-31]"
150 
151   // add out custom parser and formatter to  the facets
152   date_output->period_formatter(per_formatter);
153   date_input->period_parser(per_parser);
154 
155   // custom output
156   ss.str("");
157   ss << dp;
158   cout << ss.str() << endl; // "from 2005-Feb-01 to 2005-Apr-01 exclusive"
159 
160 
161   /****** date generators ******/
162   // custom date_generator phrases
163   string dg_phrases[9] = { "1st", "2nd", "3rd", "4th", "5th",
164                            "final", "prior to", "following", "in" };
165   vector<string> phrases(&dg_phrases[0], &dg_phrases[9]);
166 
167   // create our date_generator
168   first_day_of_the_week_before d_gen(Monday);
169 
170   // default output
171   ss.str("");
172   ss << d_gen;
173   cout << ss.str() << endl; // "Mon before"
174 
175   // add our custom strings to the date facets
176   date_output->date_gen_phrase_strings(phrases);
177   date_input->date_gen_element_strings(phrases);
178 
179   // custom output
180   ss.str("");
181   ss << d_gen;
182   cout << ss.str() << endl; // "Mon prior to"
183 
184   return 0;
185 }
186 
187 
188 /*  Copyright 2005: CrystalClear Software, Inc
189  *  http://www.crystalclearsoftware.com
190  *
191  *  Subject to the Boost Software License, Version 1.0.
192  * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
193  */
194 
195