| 1 | <html> |
|---|
| 2 | <head> |
|---|
| 3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
|---|
| 4 | <title>Examples</title> |
|---|
| 5 | <link rel="stylesheet" href="../boostbook.css" type="text/css"> |
|---|
| 6 | <meta name="generator" content="DocBook XSL Stylesheets V1.68.1"> |
|---|
| 7 | <link rel="start" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> |
|---|
| 8 | <link rel="up" href="../date_time.html" title="Chapter 4. Boost.Date_Time"> |
|---|
| 9 | <link rel="prev" href="details.html" title="Details"> |
|---|
| 10 | <link rel="next" href="doxy.html" title="Library Reference"> |
|---|
| 11 | </head> |
|---|
| 12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
|---|
| 13 | <table cellpadding="2" width="100%"> |
|---|
| 14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> |
|---|
| 15 | <td align="center"><a href="../../../index.htm">Home</a></td> |
|---|
| 16 | <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> |
|---|
| 17 | <td align="center"><a href="../../../people/people.htm">People</a></td> |
|---|
| 18 | <td align="center"><a href="../../../more/faq.htm">FAQ</a></td> |
|---|
| 19 | <td align="center"><a href="../../../more/index.htm">More</a></td> |
|---|
| 20 | </table> |
|---|
| 21 | <hr> |
|---|
| 22 | <div class="spirit-nav"> |
|---|
| 23 | <a accesskey="p" href="details.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../date_time.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="doxy.html"><img src="../images/next.png" alt="Next"></a> |
|---|
| 24 | </div> |
|---|
| 25 | <div class="section" lang="en"> |
|---|
| 26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|---|
| 27 | <a name="date_time.examples"></a>Examples</h2></div></div></div> |
|---|
| 28 | <div class="toc"><dl> |
|---|
| 29 | <dt><span class="section"><a href="examples.html#date_time.examples.dates_as_strings">Dates as Strings</a></span></dt> |
|---|
| 30 | <dt><span class="section"><a href="examples.html#date_time.examples.days_alive">Days Alive</a></span></dt> |
|---|
| 31 | <dt><span class="section"><a href="examples.html#date_time.examples.days_between_new_year">Days Between New Years</a></span></dt> |
|---|
| 32 | <dt><span class="section"><a href="examples.html#date_time.examples.end_of_month_day">Last Day of the Months</a></span></dt> |
|---|
| 33 | <dt><span class="section"><a href="examples.html#date_time.examples.localization">Localization Demonstration</a></span></dt> |
|---|
| 34 | <dt><span class="section"><a href="examples.html#date_time.examples.date_period_calc">Date Period Calculations</a></span></dt> |
|---|
| 35 | <dt><span class="section"><a href="examples.html#date_time.examples.print_holidays">Print Holidays</a></span></dt> |
|---|
| 36 | <dt><span class="section"><a href="examples.html#date_time.examples.print_month">Print Month</a></span></dt> |
|---|
| 37 | <dt><span class="section"><a href="examples.html#date_time.examples.month_add">Month Adding</a></span></dt> |
|---|
| 38 | <dt><span class="section"><a href="examples.html#date_time.examples.time_math">Time Math</a></span></dt> |
|---|
| 39 | <dt><span class="section"><a href="examples.html#date_time.examples.print_hours">Print Hours</a></span></dt> |
|---|
| 40 | <dt><span class="section"><a href="examples.html#date_time.examples.local_utc_conversion">Local to UTC Conversion</a></span></dt> |
|---|
| 41 | <dt><span class="section"><a href="examples.html#date_time.examples.time_periods">Time Periods</a></span></dt> |
|---|
| 42 | <dt><span class="section"><a href="examples.html#date_time.examples.simple_time_zone">Simple Time Zones</a></span></dt> |
|---|
| 43 | <dt><span class="section"><a href="examples.html#date_time.examples.calc_rules">Daylight Savings Calc Rules</a></span></dt> |
|---|
| 44 | <dt><span class="section"><a href="examples.html#date_time.examples.flight">Flight Time Example</a></span></dt> |
|---|
| 45 | <dt><span class="section"><a href="examples.html#date_time.examples.seconds_since_epoch">Seconds Since Epoch</a></span></dt> |
|---|
| 46 | </dl></div> |
|---|
| 47 | <div class="section" lang="en"> |
|---|
| 48 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 49 | <a name="date_time.examples.dates_as_strings"></a>Dates as Strings</h3></div></div></div> |
|---|
| 50 | <p> |
|---|
| 51 | Various parsing and output of strings. |
|---|
| 52 | </p> |
|---|
| 53 | <pre class="programlisting"> |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | /* The following is a simple example that shows conversion of dates |
|---|
| 57 | * to and from a std::string. |
|---|
| 58 | * |
|---|
| 59 | * Expected output: |
|---|
| 60 | * 2001-Oct-09 |
|---|
| 61 | * 2001-10-09 |
|---|
| 62 | * Tuesday October 9, 2001 |
|---|
| 63 | * An expected exception is next: |
|---|
| 64 | * Exception: Month number is out of range 1..12 |
|---|
| 65 | */ |
|---|
| 66 | |
|---|
| 67 | #include "boost/date_time/gregorian/gregorian.hpp" |
|---|
| 68 | #include <iostream> |
|---|
| 69 | #include <string> |
|---|
| 70 | |
|---|
| 71 | int |
|---|
| 72 | main() |
|---|
| 73 | { |
|---|
| 74 | |
|---|
| 75 | using namespace boost::gregorian; |
|---|
| 76 | |
|---|
| 77 | try { |
|---|
| 78 | // The following date is in ISO 8601 extended format (CCYY-MM-DD) |
|---|
| 79 | std::string s("2001-10-9"); //2001-October-09 |
|---|
| 80 | date d(from_simple_string(s)); |
|---|
| 81 | std::cout << to_simple_string(d) << std::endl; |
|---|
| 82 | |
|---|
| 83 | //Read ISO Standard(CCYYMMDD) and output ISO Extended |
|---|
| 84 | std::string ud("20011009"); //2001-Oct-09 |
|---|
| 85 | date d1(from_undelimited_string(ud)); |
|---|
| 86 | std::cout << to_iso_extended_string(d1) << std::endl; |
|---|
| 87 | |
|---|
| 88 | //Output the parts of the date - Tuesday October 9, 2001 |
|---|
| 89 | date::ymd_type ymd = d1.year_month_day(); |
|---|
| 90 | greg_weekday wd = d1.day_of_week(); |
|---|
| 91 | std::cout << wd.as_long_string() << " " |
|---|
| 92 | << ymd.month.as_long_string() << " " |
|---|
| 93 | << ymd.day << ", " << ymd.year |
|---|
| 94 | << std::endl; |
|---|
| 95 | |
|---|
| 96 | //Let's send in month 25 by accident and create an exception |
|---|
| 97 | std::string bad_date("20012509"); //2001-??-09 |
|---|
| 98 | std::cout << "An expected exception is next: " << std::endl; |
|---|
| 99 | date wont_construct(from_undelimited_string(bad_date)); |
|---|
| 100 | //use wont_construct so compiler doesn't complain, but you wont get here! |
|---|
| 101 | std::cout << "oh oh, you shouldn't reach this line: " |
|---|
| 102 | << to_iso_string(wont_construct) << std::endl; |
|---|
| 103 | } |
|---|
| 104 | catch(std::exception& e) { |
|---|
| 105 | std::cout << " Exception: " << e.what() << std::endl; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | return 0; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | </pre> |
|---|
| 114 | </div> |
|---|
| 115 | <div class="section" lang="en"> |
|---|
| 116 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 117 | <a name="date_time.examples.days_alive"></a>Days Alive</h3></div></div></div> |
|---|
| 118 | <p> |
|---|
| 119 | Calculate the number of days you have been living using durations and dates. |
|---|
| 120 | </p> |
|---|
| 121 | <pre class="programlisting"> |
|---|
| 122 | <code class="literal"> |
|---|
| 123 | <span class="comment">/* Short example that calculates the number of days since user was born. |
|---|
| 124 | * Demonstrates comparisons of durations, use of the day_clock, |
|---|
| 125 | * and parsing a date from a string. |
|---|
| 126 | */</span><span class="preprocessor"> |
|---|
| 127 | |
|---|
| 128 | #include</span><span class="string"> "boost/date_time/gregorian/gregorian.hpp"</span><span class="preprocessor"> |
|---|
| 129 | #include</span><span class="special"> <</span><span class="identifier">iostream</span><span class="special">></span><span class="keyword"> |
|---|
| 130 | |
|---|
| 131 | int</span><span class="identifier"> |
|---|
| 132 | main</span><span class="special">()</span><span class="special"> |
|---|
| 133 | {</span><span class="keyword"> |
|---|
| 134 | |
|---|
| 135 | using</span><span class="keyword"> namespace</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">gregorian</span><span class="special">;</span><span class="identifier"> |
|---|
| 136 | std</span><span class="special">::</span><span class="identifier">string</span><span class="identifier"> s</span><span class="special">;</span><span class="identifier"> |
|---|
| 137 | std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> <<</span><span class="string"> "Enter birth day YYYY-MM-DD (eg: 2002-02-01): "</span><span class="special">;</span><span class="identifier"> |
|---|
| 138 | std</span><span class="special">::</span><span class="identifier">cin</span><span class="special"> >></span><span class="identifier"> s</span><span class="special">;</span><span class="keyword"> |
|---|
| 139 | try</span><span class="special"> {</span><span class="identifier"> |
|---|
| 140 | date</span><span class="identifier"> birthday</span><span class="special">(</span><span class="identifier">from_simple_string</span><span class="special">(</span><span class="identifier">s</span><span class="special">));</span><span class="identifier"> |
|---|
| 141 | date</span><span class="identifier"> today</span><span class="special"> =</span><span class="identifier"> day_clock</span><span class="special">::</span><span class="identifier">local_day</span><span class="special">();</span><span class="identifier"> |
|---|
| 142 | days</span><span class="identifier"> days_alive</span><span class="special"> =</span><span class="identifier"> today</span><span class="special"> -</span><span class="identifier"> birthday</span><span class="special">;</span><span class="identifier"> |
|---|
| 143 | days</span><span class="identifier"> one_day</span><span class="special">(</span><span class="number">1</span><span class="special">);</span><span class="keyword"> |
|---|
| 144 | if</span><span class="special"> (</span><span class="identifier">days_alive</span><span class="special"> ==</span><span class="identifier"> one_day</span><span class="special">)</span><span class="special"> {</span><span class="identifier"> |
|---|
| 145 | std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> <<</span><span class="string"> "Born yesterday, very funny"</span><span class="special"> <<</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="special"> |
|---|
| 146 | }</span><span class="keyword"> |
|---|
| 147 | else</span><span class="keyword"> if</span><span class="special"> (</span><span class="identifier">days_alive</span><span class="special"> <</span><span class="identifier"> days</span><span class="special">(</span><span class="number">0</span><span class="special">))</span><span class="special"> {</span><span class="identifier"> |
|---|
| 148 | std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> <<</span><span class="string"> "Not born yet, hmm: "</span><span class="special"> <<</span><span class="identifier"> days_alive</span><span class="special">.</span><span class="identifier">days</span><span class="special">()</span><span class="special"> |
|---|
| 149 | <<</span><span class="string"> " days"</span><span class="special"> <<</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="special"> |
|---|
| 150 | }</span><span class="keyword"> |
|---|
| 151 | else</span><span class="special"> {</span><span class="identifier"> |
|---|
| 152 | std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> <<</span><span class="string"> "Days alive: "</span><span class="special"> <<</span><span class="identifier"> days_alive</span><span class="special">.</span><span class="identifier">days</span><span class="special">()</span><span class="special"> <<</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="special"> |
|---|
| 153 | }</span><span class="special"> |
|---|
| 154 | |
|---|
| 155 | }</span><span class="keyword"> |
|---|
| 156 | catch</span><span class="special">(...)</span><span class="special"> {</span><span class="identifier"> |
|---|
| 157 | std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> <<</span><span class="string"> "Bad date entered: "</span><span class="special"> <<</span><span class="identifier"> s</span><span class="special"> <<</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="special"> |
|---|
| 158 | }</span><span class="keyword"> |
|---|
| 159 | return</span><span class="number"> 0</span><span class="special">;</span><span class="special"> |
|---|
| 160 | }</span> |
|---|
| 161 | </code> |
|---|
| 162 | </pre> |
|---|
| 163 | </div> |
|---|
| 164 | <div class="section" lang="en"> |
|---|
| 165 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 166 | <a name="date_time.examples.days_between_new_year"></a>Days Between New Years</h3></div></div></div> |
|---|
| 167 | <p> |
|---|
| 168 | Calculate the number of days till new years |
|---|
| 169 | </p> |
|---|
| 170 | <pre class="programlisting"> |
|---|
| 171 | |
|---|
| 172 | /* Provides a simple example of using a date_generator, and simple |
|---|
| 173 | * mathematical operatorations, to calculate the days since |
|---|
| 174 | * New Years day of this year, and days until next New Years day. |
|---|
| 175 | * |
|---|
| 176 | * Expected results: |
|---|
| 177 | * Adding together both durations will produce 366 (365 in a leap year). |
|---|
| 178 | */ |
|---|
| 179 | #include <iostream> |
|---|
| 180 | #include "boost/date_time/gregorian/gregorian.hpp" |
|---|
| 181 | |
|---|
| 182 | int |
|---|
| 183 | main() |
|---|
| 184 | { |
|---|
| 185 | |
|---|
| 186 | using namespace boost::gregorian; |
|---|
| 187 | |
|---|
| 188 | date today = day_clock::local_day(); |
|---|
| 189 | partial_date new_years_day(1,Jan); |
|---|
| 190 | //Subtract two dates to get a duration |
|---|
| 191 | days days_since_year_start = today - new_years_day.get_date(today.year()); |
|---|
| 192 | std::cout << "Days since Jan 1: " << days_since_year_start.days() |
|---|
| 193 | << std::endl; |
|---|
| 194 | |
|---|
| 195 | days days_until_year_start = new_years_day.get_date(today.year()+1) - today; |
|---|
| 196 | std::cout << "Days until next Jan 1: " << days_until_year_start.days() |
|---|
| 197 | << std::endl; |
|---|
| 198 | return 0; |
|---|
| 199 | }; |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | </pre> |
|---|
| 203 | </div> |
|---|
| 204 | <div class="section" lang="en"> |
|---|
| 205 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 206 | <a name="date_time.examples.end_of_month_day"></a>Last Day of the Months</h3></div></div></div> |
|---|
| 207 | <p> |
|---|
| 208 | Example that gets a month and a year from the user and finds the last day of each remaining month of that year. |
|---|
| 209 | </p> |
|---|
| 210 | <pre class="programlisting"> |
|---|
| 211 | |
|---|
| 212 | /* Simple program that finds the last day of the given month, |
|---|
| 213 | * then displays the last day of every month left in the given year. |
|---|
| 214 | */ |
|---|
| 215 | |
|---|
| 216 | #include "boost/date_time/gregorian/gregorian.hpp" |
|---|
| 217 | #include <iostream> |
|---|
| 218 | |
|---|
| 219 | int |
|---|
| 220 | main() |
|---|
| 221 | { |
|---|
| 222 | using namespace boost::gregorian; |
|---|
| 223 | |
|---|
| 224 | greg_year year(1400); |
|---|
| 225 | greg_month month(1); |
|---|
| 226 | |
|---|
| 227 | // get a month and a year from the user |
|---|
| 228 | try { |
|---|
| 229 | int y, m; |
|---|
| 230 | std::cout << " Enter Year(ex: 2002): "; |
|---|
| 231 | std::cin >> y; |
|---|
| 232 | year = greg_year(y); |
|---|
| 233 | std::cout << " Enter Month(1..12): "; |
|---|
| 234 | std::cin >> m; |
|---|
| 235 | month = greg_month(m); |
|---|
| 236 | } |
|---|
| 237 | catch(bad_year by) { |
|---|
| 238 | std::cout << "Invalid Year Entered: " << by.what() << '\n' |
|---|
| 239 | << "Using minimum values for month and year." << std::endl; |
|---|
| 240 | } |
|---|
| 241 | catch(bad_month bm) { |
|---|
| 242 | std::cout << "Invalid Month Entered" << bm.what() << '\n' |
|---|
| 243 | << "Using minimum value for month. " << std::endl; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | date start_of_next_year(year+1, Jan, 1); |
|---|
| 247 | date d(year, month, 1); |
|---|
| 248 | |
|---|
| 249 | // add another month to d until we enter the next year. |
|---|
| 250 | while (d < start_of_next_year){ |
|---|
| 251 | std::cout << to_simple_string(d.end_of_month()) << std::endl; |
|---|
| 252 | d += months(1); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | return 0; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | </pre> |
|---|
| 260 | </div> |
|---|
| 261 | <div class="section" lang="en"> |
|---|
| 262 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 263 | <a name="date_time.examples.localization"></a>Localization Demonstration</h3></div></div></div> |
|---|
| 264 | <p> |
|---|
| 265 | The boost::date_time library provides the ability to create customized locale facets. Date ordering, language, seperators, and abbreviations can be customized. |
|---|
| 266 | </p> |
|---|
| 267 | <pre class="programlisting"> |
|---|
| 268 | |
|---|
| 269 | /* The following shows the creation of a facet for the output of |
|---|
| 270 | * dates in German (please forgive me for any errors in my German -- |
|---|
| 271 | * I'm not a native speaker). |
|---|
| 272 | */ |
|---|
| 273 | |
|---|
| 274 | #include "boost/date_time/gregorian/gregorian.hpp" |
|---|
| 275 | #include <iostream> |
|---|
| 276 | #include <algorithm> |
|---|
| 277 | |
|---|
| 278 | /* Define a series of char arrays for short and long name strings |
|---|
| 279 | * to be associated with German date output (US names will be |
|---|
| 280 | * retrieved from the locale). */ |
|---|
| 281 | const char* const de_short_month_names[] = |
|---|
| 282 | { |
|---|
| 283 | "Jan", "Feb", "Mar", "Apr", "Mai", "Jun", |
|---|
| 284 | "Jul", "Aug", "Sep", "Okt", "Nov", "Dez", "NAM" |
|---|
| 285 | }; |
|---|
| 286 | const char* const de_long_month_names[] = |
|---|
| 287 | { |
|---|
| 288 | "Januar", "Februar", "Marz", "April", "Mai", |
|---|
| 289 | "Juni", "Juli", "August", "September", "Oktober", |
|---|
| 290 | "November", "Dezember", "NichtDerMonat" |
|---|
| 291 | }; |
|---|
| 292 | const char* const de_long_weekday_names[] = |
|---|
| 293 | { |
|---|
| 294 | "Sonntag", "Montag", "Dienstag", "Mittwoch", |
|---|
| 295 | "Donnerstag", "Freitag", "Samstag" |
|---|
| 296 | }; |
|---|
| 297 | const char* const de_short_weekday_names[] = |
|---|
| 298 | { |
|---|
| 299 | "Son", "Mon", "Die","Mit", "Don", "Fre", "Sam" |
|---|
| 300 | }; |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | int main() |
|---|
| 304 | { |
|---|
| 305 | using namespace boost::gregorian; |
|---|
| 306 | |
|---|
| 307 | // create some gregorian objects to output |
|---|
| 308 | date d1(2002, Oct, 1); |
|---|
| 309 | greg_month m = d1.month(); |
|---|
| 310 | greg_weekday wd = d1.day_of_week(); |
|---|
| 311 | |
|---|
| 312 | // create a facet and a locale for German dates |
|---|
| 313 | date_facet* german_facet = new date_facet(); |
|---|
| 314 | std::cout.imbue(std::locale(std::locale::classic(), german_facet)); |
|---|
| 315 | |
|---|
| 316 | // create the German name collections |
|---|
| 317 | date_facet::input_collection_type short_months, long_months, |
|---|
| 318 | short_weekdays, long_weekdays; |
|---|
| 319 | std::copy(&de_short_month_names[0], &de_short_month_names[11], |
|---|
| 320 | std::back_inserter(short_months)); |
|---|
| 321 | std::copy(&de_long_month_names[0], &de_long_month_names[11], |
|---|
| 322 | std::back_inserter(long_months)); |
|---|
| 323 | std::copy(&de_short_weekday_names[0], &de_short_weekday_names[6], |
|---|
| 324 | std::back_inserter(short_weekdays)); |
|---|
| 325 | std::copy(&de_long_weekday_names[0], &de_long_weekday_names[6], |
|---|
| 326 | std::back_inserter(long_weekdays)); |
|---|
| 327 | |
|---|
| 328 | // replace the default names with ours |
|---|
| 329 | // NOTE: date_generators and special_values were not replaced as |
|---|
| 330 | // they are not used in this example |
|---|
| 331 | german_facet->short_month_names(short_months); |
|---|
| 332 | german_facet->long_month_names(long_months); |
|---|
| 333 | german_facet->short_weekday_names(short_weekdays); |
|---|
| 334 | german_facet->long_weekday_names(long_weekdays); |
|---|
| 335 | |
|---|
| 336 | // output the date in German using short month names |
|---|
| 337 | german_facet->format("%d.%m.%Y"); |
|---|
| 338 | std::cout << d1 << std::endl; //01.10.2002 |
|---|
| 339 | |
|---|
| 340 | german_facet->month_format("%B"); |
|---|
| 341 | std::cout << m << std::endl; //Oktober |
|---|
| 342 | |
|---|
| 343 | german_facet->weekday_format("%A"); |
|---|
| 344 | std::cout << wd << std::endl; //Dienstag |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | // Output the same gregorian objects using US names |
|---|
| 348 | date_facet* us_facet = new date_facet(); |
|---|
| 349 | std::cout.imbue(std::locale(std::locale::classic(), us_facet)); |
|---|
| 350 | |
|---|
| 351 | us_facet->format("%m/%d/%Y"); |
|---|
| 352 | std::cout << d1 << std::endl; // 10/01/2002 |
|---|
| 353 | |
|---|
| 354 | // English names, iso order (year-month-day), '-' separator |
|---|
| 355 | us_facet->format("%Y-%b-%d"); |
|---|
| 356 | std::cout << d1 << std::endl; // 2002-Oct-01 |
|---|
| 357 | |
|---|
| 358 | return 0; |
|---|
| 359 | |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | </pre> |
|---|
| 363 | </div> |
|---|
| 364 | <div class="section" lang="en"> |
|---|
| 365 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 366 | <a name="date_time.examples.date_period_calc"></a>Date Period Calculations</h3></div></div></div> |
|---|
| 367 | <p> |
|---|
| 368 | Calculates if a date is in an 'irregular' collection of periods using period calculation functions. |
|---|
| 369 | </p> |
|---|
| 370 | <pre class="programlisting"> |
|---|
| 371 | |
|---|
| 372 | /* |
|---|
| 373 | This example demonstrates a simple use of periods for the calculation |
|---|
| 374 | of date information. |
|---|
| 375 | |
|---|
| 376 | The example calculates if a given date is a weekend or holiday |
|---|
| 377 | given an exclusion set. That is, each weekend or holiday is |
|---|
| 378 | entered into the set as a time interval. Then if a given date |
|---|
| 379 | is contained within any of the intervals it is considered to |
|---|
| 380 | be within the exclusion set and hence is a offtime. |
|---|
| 381 | |
|---|
| 382 | Output: |
|---|
| 383 | Number Excluded Periods: 5 |
|---|
| 384 | 20020202/20020203 |
|---|
| 385 | 20020209/20020210 |
|---|
| 386 | 20020212/20020212 |
|---|
| 387 | 20020216/20020217 |
|---|
| 388 | In Exclusion Period: 20020216 --> 20020216/20020217 |
|---|
| 389 | 20020223/20020224 |
|---|
| 390 | |
|---|
| 391 | */ |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | #include "boost/date_time/gregorian/gregorian.hpp" |
|---|
| 395 | #include <set> |
|---|
| 396 | #include <algorithm> |
|---|
| 397 | #include <iostream> |
|---|
| 398 | |
|---|
| 399 | typedef std::set<boost::gregorian::date_period> date_period_set; |
|---|
| 400 | |
|---|
| 401 | //Simple population of the exclusion set |
|---|
| 402 | date_period_set |
|---|
| 403 | generateExclusion() |
|---|
| 404 | { |
|---|
| 405 | using namespace boost::gregorian; |
|---|
| 406 | date_period periods_array[] = |
|---|
| 407 | { date_period(date(2002,Feb,2), date(2002,Feb,4)),//weekend of 2nd-3rd |
|---|
| 408 | date_period(date(2002,Feb,9), date(2002,Feb,11)), |
|---|
| 409 | date_period(date(2002,Feb,16), date(2002,Feb,18)), |
|---|
| 410 | date_period(date(2002,Feb,23), date(2002,Feb,25)), |
|---|
| 411 | date_period(date(2002,Feb,12), date(2002,Feb,13))//a random holiday 2-12 |
|---|
| 412 | }; |
|---|
| 413 | const int num_periods = sizeof(periods_array)/sizeof(date_period); |
|---|
| 414 | |
|---|
| 415 | date_period_set ps; |
|---|
| 416 | //insert the periods in the set |
|---|
| 417 | std::insert_iterator<date_period_set> itr(ps, ps.begin()); |
|---|
| 418 | std::copy(periods_array, periods_array+num_periods, itr ); |
|---|
| 419 | return ps; |
|---|
| 420 | |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | int main() |
|---|
| 425 | { |
|---|
| 426 | using namespace boost::gregorian; |
|---|
| 427 | |
|---|
| 428 | date_period_set ps = generateExclusion(); |
|---|
| 429 | std::cout << "Number Excluded Periods: " << ps.size() << std::endl; |
|---|
| 430 | |
|---|
| 431 | date d(2002,Feb,16); |
|---|
| 432 | date_period_set::const_iterator i = ps.begin(); |
|---|
| 433 | //print the periods, check for containment |
|---|
| 434 | for (;i != ps.end(); i++) { |
|---|
| 435 | std::cout << to_iso_string(*i) << std::endl; |
|---|
| 436 | //if date is in exclusion period then print it |
|---|
| 437 | if (i->contains(d)) { |
|---|
| 438 | std::cout << "In Exclusion Period: " |
|---|
| 439 | << to_iso_string(d) << " --> " << to_iso_string(*i) |
|---|
| 440 | << std::endl; |
|---|
| 441 | } |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | return 0; |
|---|
| 445 | |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | </pre> |
|---|
| 450 | </div> |
|---|
| 451 | <div class="section" lang="en"> |
|---|
| 452 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 453 | <a name="date_time.examples.print_holidays"></a>Print Holidays</h3></div></div></div> |
|---|
| 454 | <p> |
|---|
| 455 | This is an example of using functors to define a holiday schedule |
|---|
| 456 | </p> |
|---|
| 457 | <pre class="programlisting"> |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | /* Generate a set of dates using a collection of date generators |
|---|
| 461 | * Output looks like: |
|---|
| 462 | * Enter Year: 2002 |
|---|
| 463 | * 2002-Jan-01 [Tue] |
|---|
| 464 | * 2002-Jan-21 [Mon] |
|---|
| 465 | * 2002-Feb-12 [Tue] |
|---|
| 466 | * 2002-Jul-04 [Thu] |
|---|
| 467 | * 2002-Sep-02 [Mon] |
|---|
| 468 | * 2002-Nov-28 [Thu] |
|---|
| 469 | * 2002-Dec-25 [Wed] |
|---|
| 470 | * Number Holidays: 7 |
|---|
| 471 | */ |
|---|
| 472 | |
|---|
| 473 | #include "boost/date_time/gregorian/gregorian.hpp" |
|---|
| 474 | #include <algorithm> |
|---|
| 475 | #include <functional> |
|---|
| 476 | #include <vector> |
|---|
| 477 | #include <iostream> |
|---|
| 478 | #include <set> |
|---|
| 479 | |
|---|
| 480 | void |
|---|
| 481 | print_date(boost::gregorian::date d) |
|---|
| 482 | { |
|---|
| 483 | using namespace boost::gregorian; |
|---|
| 484 | #if defined(BOOST_DATE_TIME_NO_LOCALE) |
|---|
| 485 | std::cout << to_simple_string(d) << " [" << d.day_of_week() << "]\n"; |
|---|
| 486 | #else |
|---|
| 487 | std::cout << d << " [" << d.day_of_week() << "]\n"; |
|---|
| 488 | #endif |
|---|
| 489 | } |
|---|
| 490 | |
|---|
| 491 | |
|---|
| 492 | int |
|---|
| 493 | main() { |
|---|
| 494 | |
|---|
| 495 | std::cout << "Enter Year: "; |
|---|
| 496 | int year; |
|---|
| 497 | std::cin >> year; |
|---|
| 498 | |
|---|
| 499 | using namespace boost::gregorian; |
|---|
| 500 | |
|---|
| 501 | //define a collection of holidays fixed by month and day |
|---|
| 502 | std::vector<year_based_generator*> holidays; |
|---|
| 503 | holidays.push_back(new partial_date(1,Jan)); //Western New Year |
|---|
| 504 | holidays.push_back(new partial_date(4,Jul)); //US Independence Day |
|---|
| 505 | holidays.push_back(new partial_date(25, Dec));//Christmas day |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | //define a shorthand for the nth_day_of_the_week_in_month function object |
|---|
| 509 | typedef nth_day_of_the_week_in_month nth_dow; |
|---|
| 510 | |
|---|
| 511 | //US labor day |
|---|
| 512 | holidays.push_back(new nth_dow(nth_dow::first, Monday, Sep)); |
|---|
| 513 | //MLK Day |
|---|
| 514 | holidays.push_back(new nth_dow(nth_dow::third, Monday, Jan)); |
|---|
| 515 | //Pres day |
|---|
| 516 | holidays.push_back(new nth_dow(nth_dow::second, Tuesday, Feb)); |
|---|
| 517 | //Thanksgiving |
|---|
| 518 | holidays.push_back(new nth_dow(nth_dow::fourth, Thursday, Nov)); |
|---|
| 519 | |
|---|
| 520 | typedef std::set<date> date_set; |
|---|
| 521 | date_set all_holidays; |
|---|
| 522 | |
|---|
| 523 | for(std::vector<year_based_generator*>::iterator it = holidays.begin(); |
|---|
| 524 | it != holidays.end(); ++it) |
|---|
| 525 | { |
|---|
| 526 | all_holidays.insert((*it)->get_date(year)); |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | //print the holidays to the screen |
|---|
| 530 | std::for_each(all_holidays.begin(), all_holidays.end(), print_date); |
|---|
| 531 | std::cout << "Number Holidays: " << all_holidays.size() << std::endl; |
|---|
| 532 | |
|---|
| 533 | return 0; |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| 536 | |
|---|
| 537 | </pre> |
|---|
| 538 | </div> |
|---|
| 539 | <div class="section" lang="en"> |
|---|
| 540 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 541 | <a name="date_time.examples.print_month"></a>Print Month</h3></div></div></div> |
|---|
| 542 | <p> |
|---|
| 543 | Simple utility to print out days of the month with the days of a month. Demontstrates date iteration (date_time::date_itr). |
|---|
| 544 | </p> |
|---|
| 545 | <pre class="programlisting"> |
|---|
| 546 | |
|---|
| 547 | /* This example prints all the dates in a month. It demonstrates |
|---|
| 548 | * the use of iterators as well as functions of the gregorian_calendar |
|---|
| 549 | * |
|---|
| 550 | * Output: |
|---|
| 551 | * Enter Year: 2002 |
|---|
| 552 | * Enter Month(1..12): 2 |
|---|
| 553 | * 2002-Feb-01 [Fri] |
|---|
| 554 | * 2002-Feb-02 [Sat] |
|---|
| 555 | * 2002-Feb-03 [Sun] |
|---|
| 556 | * 2002-Feb-04 [Mon] |
|---|
| 557 | * 2002-Feb-05 [Tue] |
|---|
| 558 | * 2002-Feb-06 [Wed] |
|---|
| 559 | * 2002-Feb-07 [Thu] |
|---|
| 560 | */ |
|---|
| 561 | |
|---|
| 562 | #include "boost/date_time/gregorian/gregorian.hpp" |
|---|
| 563 | #include <iostream> |
|---|
| 564 | |
|---|
| 565 | int |
|---|
| 566 | main() |
|---|
| 567 | { |
|---|
| 568 | std::cout << "Enter Year: "; |
|---|
| 569 | int year, month; |
|---|
| 570 | std::cin >> year; |
|---|
| 571 | std::cout << "Enter Month(1..12): "; |
|---|
| 572 | std::cin >> month; |
|---|
| 573 | |
|---|
| 574 | using namespace boost::gregorian; |
|---|
| 575 | try { |
|---|
| 576 | //Use the calendar to get the last day of the month |
|---|
| 577 | int eom_day = gregorian_calendar::end_of_month_day(year,month); |
|---|
| 578 | date endOfMonth(year,month,eom_day); |
|---|
| 579 | |
|---|
| 580 | //construct an iterator starting with firt day of the month |
|---|
| 581 | day_iterator ditr(date(year,month,1)); |
|---|
| 582 | //loop thru the days and print each one |
|---|
| 583 | for (; ditr <= endOfMonth; ++ditr) { |
|---|
| 584 | #if defined(BOOST_DATE_TIME_NO_LOCALE) |
|---|
| 585 | std::cout << to_simple_string(*ditr) << " [" |
|---|
| 586 | #else |
|---|
| 587 | std::cout << *ditr << " [" |
|---|
| 588 | #endif |
|---|
| 589 | << ditr->day_of_week() << "]" |
|---|
| 590 | << std::endl; |
|---|
| 591 | } |
|---|
| 592 | } |
|---|
| 593 | catch(std::exception& e) { |
|---|
| 594 | |
|---|
| 595 | std::cout << "Error bad date, check your entry: \n" |
|---|
| 596 | << " Details: " << e.what() << std::endl; |
|---|
| 597 | } |
|---|
| 598 | return 0; |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | </pre> |
|---|
| 603 | </div> |
|---|
| 604 | <div class="section" lang="en"> |
|---|
| 605 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 606 | <a name="date_time.examples.month_add"></a>Month Adding</h3></div></div></div> |
|---|
| 607 | <p> |
|---|
| 608 | Adding a month to a day without the use of iterators. |
|---|
| 609 | </p> |
|---|
| 610 | <pre class="programlisting"> |
|---|
| 611 | |
|---|
| 612 | /* Simple program that uses the gregorian calendar to progress by exactly |
|---|
| 613 | * one month, irregardless of how many days are in that month. |
|---|
| 614 | * |
|---|
| 615 | * This method can be used as an alternative to iterators |
|---|
| 616 | */ |
|---|
| 617 | |
|---|
| 618 | #include "boost/date_time/gregorian/gregorian.hpp" |
|---|
| 619 | #include <iostream> |
|---|
| 620 | |
|---|
| 621 | int |
|---|
| 622 | main() |
|---|
| 623 | { |
|---|
| 624 | |
|---|
| 625 | using namespace boost::gregorian; |
|---|
| 626 | |
|---|
| 627 | date d = day_clock::local_day(); |
|---|
| 628 | add_month mf(1); |
|---|
| 629 | date d2 = d + mf.get_offset(d); |
|---|
| 630 | std::cout << "Today is: " << to_simple_string(d) << ".\n" |
|---|
| 631 | << "One month from today will be: " << to_simple_string(d2) |
|---|
| 632 | << std::endl; |
|---|
| 633 | |
|---|
| 634 | return 0; |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | </pre> |
|---|
| 638 | </div> |
|---|
| 639 | <div class="section" lang="en"> |
|---|
| 640 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 641 | <a name="date_time.examples.time_math"></a>Time Math</h3></div></div></div> |
|---|
| 642 | <p> |
|---|
| 643 | Various types of calculations with times and time durations. |
|---|
| 644 | </p> |
|---|
| 645 | <pre class="programlisting"> |
|---|
| 646 | |
|---|
| 647 | /* Some simple examples of constructing and calculating with times |
|---|
| 648 | * Output: |
|---|
| 649 | * 2002-Feb-01 00:00:00 - 2002-Feb-01 05:04:02.001000000 = -5:04:02.001000000 |
|---|
| 650 | */ |
|---|
| 651 | |
|---|
| 652 | #include "boost/date_time/posix_time/posix_time.hpp" |
|---|
| 653 | #include <iostream> |
|---|
| 654 | |
|---|
| 655 | int |
|---|
| 656 | main() |
|---|
| 657 | { |
|---|
| 658 | using namespace boost::posix_time; |
|---|
| 659 | using namespace boost::gregorian; |
|---|
| 660 | |
|---|
| 661 | date d(2002,Feb,1); //an arbitrary date |
|---|
| 662 | //construct a time by adding up some durations durations |
|---|
| 663 | ptime t1(d, hours(5)+minutes(4)+seconds(2)+millisec(1)); |
|---|
| 664 | //construct a new time by subtracting some times |
|---|
| 665 | ptime t2 = t1 - hours(5)- minutes(4)- seconds(2)- millisec(1); |
|---|
| 666 | //construct a duration by taking the difference between times |
|---|
| 667 | time_duration td = t2 - t1; |
|---|
| 668 | |
|---|
| 669 | std::cout << to_simple_string(t2) << " - " |
|---|
| 670 | << to_simple_string(t1) << " = " |
|---|
| 671 | << to_simple_string(td) << std::endl; |
|---|
| 672 | |
|---|
| 673 | return 0; |
|---|
| 674 | } |
|---|
| 675 | |
|---|
| 676 | </pre> |
|---|
| 677 | </div> |
|---|
| 678 | <div class="section" lang="en"> |
|---|
| 679 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 680 | <a name="date_time.examples.print_hours"></a>Print Hours</h3></div></div></div> |
|---|
| 681 | <p> |
|---|
| 682 | Demonstrate time iteration, clock retrieval, and simple calculation. |
|---|
| 683 | </p> |
|---|
| 684 | <pre class="programlisting"> |
|---|
| 685 | |
|---|
| 686 | /* Print the remaining hours of the day |
|---|
| 687 | * Uses the clock to get the local time |
|---|
| 688 | * Use an iterator to iterate over the remaining hours |
|---|
| 689 | * Retrieve the date part from a time |
|---|
| 690 | * |
|---|
| 691 | * Expected Output something like: |
|---|
| 692 | * |
|---|
| 693 | * 2002-Mar-08 16:30:59 |
|---|
| 694 | * 2002-Mar-08 17:30:59 |
|---|
| 695 | * 2002-Mar-08 18:30:59 |
|---|
| 696 | * 2002-Mar-08 19:30:59 |
|---|
| 697 | * 2002-Mar-08 20:30:59 |
|---|
| 698 | * 2002-Mar-08 21:30:59 |
|---|
| 699 | * 2002-Mar-08 22:30:59 |
|---|
| 700 | * 2002-Mar-08 23:30:59 |
|---|
| 701 | * Time left till midnight: 07:29:01 |
|---|
| 702 | */ |
|---|
| 703 | |
|---|
| 704 | #include "boost/date_time/posix_time/posix_time.hpp" |
|---|
| 705 | #include <iostream> |
|---|
| 706 | |
|---|
| 707 | int |
|---|
| 708 | main() |
|---|
| 709 | { |
|---|
| 710 | using namespace boost::posix_time; |
|---|
| 711 | using namespace boost::gregorian; |
|---|
| 712 | |
|---|
| 713 | //get the current time from the clock -- one second resolution |
|---|
| 714 | ptime now = second_clock::local_time(); |
|---|
| 715 | //Get the date part out of the time |
|---|
| 716 | date today = now.date(); |
|---|
| 717 | date tommorrow = today + days(1); |
|---|
| 718 | ptime tommorrow_start(tommorrow); //midnight |
|---|
| 719 | |
|---|
| 720 | //iterator adds by one hour |
|---|
| 721 | time_iterator titr(now,hours(1)); |
|---|
| 722 | for (; titr < tommorrow_start; ++titr) { |
|---|
| 723 | std::cout << to_simple_string(*titr) << std::endl; |
|---|
| 724 | } |
|---|
| 725 | |
|---|
| 726 | time_duration remaining = tommorrow_start - now; |
|---|
| 727 | std::cout << "Time left till midnight: " |
|---|
| 728 | << to_simple_string(remaining) << std::endl; |
|---|
| 729 | return 0; |
|---|
| 730 | } |
|---|
| 731 | |
|---|
| 732 | |
|---|
| 733 | </pre> |
|---|
| 734 | </div> |
|---|
| 735 | <div class="section" lang="en"> |
|---|
| 736 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 737 | <a name="date_time.examples.local_utc_conversion"></a>Local to UTC Conversion</h3></div></div></div> |
|---|
| 738 | <p> |
|---|
| 739 | Demonstrate utc to local and local to utc calculations including dst. |
|---|
| 740 | </p> |
|---|
| 741 | <pre class="programlisting"> |
|---|
| 742 | |
|---|
| 743 | |
|---|
| 744 | /* Demonstrate conversions between a local time and utc |
|---|
| 745 | * Output: |
|---|
| 746 | * |
|---|
| 747 | * UTC <--> New York while DST is NOT active (5 hours) |
|---|
| 748 | * 2001-Dec-31 19:00:00 in New York is 2002-Jan-01 00:00:00 UTC time |
|---|
| 749 | * 2002-Jan-01 00:00:00 UTC is 2001-Dec-31 19:00:00 New York time |
|---|
| 750 | * |
|---|
| 751 | * UTC <--> New York while DST is active (4 hours) |
|---|
| 752 | * 2002-May-31 20:00:00 in New York is 2002-Jun-01 00:00:00 UTC time |
|---|
| 753 | * 2002-Jun-01 00:00:00 UTC is 2002-May-31 20:00:00 New York time |
|---|
| 754 | * |
|---|
| 755 | * UTC <--> Arizona (7 hours) |
|---|
| 756 | * 2002-May-31 17:00:00 in Arizona is 2002-Jun-01 00:00:00 UTC time |
|---|
| 757 | */ |
|---|
| 758 | |
|---|
| 759 | #include "boost/date_time/posix_time/posix_time.hpp" |
|---|
| 760 | #include "boost/date_time/local_time_adjustor.hpp" |
|---|
| 761 | #include "boost/date_time/c_local_time_adjustor.hpp" |
|---|
| 762 | #include <iostream> |
|---|
| 763 | |
|---|
| 764 | int |
|---|
| 765 | main() |
|---|
| 766 | { |
|---|
| 767 | using namespace boost::posix_time; |
|---|
| 768 | using namespace boost::gregorian; |
|---|
| 769 | |
|---|
| 770 | //This local adjustor depends on the machine TZ settings-- highly dangerous! |
|---|
| 771 | typedef boost::date_time::c_local_adjustor<ptime> local_adj; |
|---|
| 772 | ptime t10(date(2002,Jan,1), hours(7)); |
|---|
| 773 | ptime t11 = local_adj::utc_to_local(t10); |
|---|
| 774 | std::cout << "UTC <--> Zone base on TZ setting" << std::endl; |
|---|
| 775 | std::cout << to_simple_string(t11) << " in your TZ is " |
|---|
| 776 | << to_simple_string(t10) << " UTC time " |
|---|
| 777 | << std::endl; |
|---|
| 778 | time_duration td = t11 - t10; |
|---|
| 779 | std::cout << "A difference of: " |
|---|
| 780 | << to_simple_string(td) << std::endl; |
|---|
| 781 | |
|---|
| 782 | |
|---|
| 783 | //eastern timezone is utc-5 |
|---|
| 784 | typedef boost::date_time::local_adjustor<ptime, -5, us_dst> us_eastern; |
|---|
| 785 | |
|---|
| 786 | ptime t1(date(2001,Dec,31), hours(19)); //5 hours b/f midnight NY time |
|---|
| 787 | |
|---|
| 788 | std::cout << "\nUTC <--> New York while DST is NOT active (5 hours)" |
|---|
| 789 | << std::endl; |
|---|
| 790 | ptime t2 = us_eastern::local_to_utc(t1); |
|---|
| 791 | std::cout << to_simple_string(t1) << " in New York is " |
|---|
| 792 | << to_simple_string(t2) << " UTC time " |
|---|
| 793 | << std::endl; |
|---|
| 794 | |
|---|
| 795 | ptime t3 = us_eastern::utc_to_local(t2);//back should be the same |
|---|
| 796 | std::cout << to_simple_string(t2) << " UTC is " |
|---|
| 797 | << to_simple_string(t3) << " New York time " |
|---|
| 798 | << "\n\n"; |
|---|
| 799 | |
|---|
| 800 | ptime t4(date(2002,May,31), hours(20)); //4 hours b/f midnight NY time |
|---|
| 801 | std::cout << "UTC <--> New York while DST is active (4 hours)" << std::endl; |
|---|
| 802 | ptime t5 = us_eastern::local_to_utc(t4); |
|---|
| 803 | std::cout << to_simple_string(t4) << " in New York is " |
|---|
| 804 | << to_simple_string(t5) << " UTC time " |
|---|
| 805 | << std::endl; |
|---|
| 806 | |
|---|
| 807 | ptime t6 = us_eastern::utc_to_local(t5);//back should be the same |
|---|
| 808 | std::cout << to_simple_string(t5) << " UTC is " |
|---|
| 809 | << to_simple_string(t6) << " New York time " |
|---|
| 810 | << "\n" << std::endl; |
|---|
| 811 | |
|---|
| 812 | |
|---|
| 813 | //Arizona timezone is utc-7 with no dst |
|---|
| 814 | typedef boost::date_time::local_adjustor<ptime, -7, no_dst> us_arizona; |
|---|
| 815 | |
|---|
| 816 | ptime t7(date(2002,May,31), hours(17)); |
|---|
| 817 | std::cout << "UTC <--> Arizona (7 hours)" << std::endl; |
|---|
| 818 | ptime t8 = us_arizona::local_to_utc(t7); |
|---|
| 819 | std::cout << to_simple_string(t7) << " in Arizona is " |
|---|
| 820 | << to_simple_string(t8) << " UTC time " |
|---|
| 821 | << std::endl; |
|---|
| 822 | |
|---|
| 823 | return 0; |
|---|
| 824 | } |
|---|
| 825 | |
|---|
| 826 | |
|---|
| 827 | </pre> |
|---|
| 828 | </div> |
|---|
| 829 | <div class="section" lang="en"> |
|---|
| 830 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 831 | <a name="date_time.examples.time_periods"></a>Time Periods</h3></div></div></div> |
|---|
| 832 | <p> |
|---|
| 833 | Demonstrate some simple uses of time periods. |
|---|
| 834 | </p> |
|---|
| 835 | <pre class="programlisting"> |
|---|
| 836 | |
|---|
| 837 | |
|---|
| 838 | /* Some simple examples of constructing and calculating with times |
|---|
| 839 | * Returns: |
|---|
| 840 | * [2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.999999999] |
|---|
| 841 | * contains 2002-Feb-01 03:00:05 |
|---|
| 842 | * [2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.999999999] |
|---|
| 843 | * intersected with |
|---|
| 844 | * [2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.999999999] |
|---|
| 845 | * is |
|---|
| 846 | * [2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.999999999] |
|---|
| 847 | */ |
|---|
| 848 | |
|---|
| 849 | #include "boost/date_time/posix_time/posix_time.hpp" |
|---|
| 850 | #include <iostream> |
|---|
| 851 | |
|---|
| 852 | using namespace boost::posix_time; |
|---|
| 853 | using namespace boost::gregorian; |
|---|
| 854 | |
|---|
| 855 | //Create a simple period class to contain all the times in a day |
|---|
| 856 | class day_period : public time_period |
|---|
| 857 | { |
|---|
| 858 | public: |
|---|
| 859 | day_period(date d) : time_period(ptime(d),//midnight |
|---|
| 860 | ptime(d,hours(24))) |
|---|
| 861 | {} |
|---|
| 862 | |
|---|
| 863 | }; |
|---|
| 864 | |
|---|
| 865 | int |
|---|
| 866 | main() |
|---|
| 867 | { |
|---|
| 868 | |
|---|
| 869 | date d(2002,Feb,1); //an arbitrary date |
|---|
| 870 | //a period that represents a day |
|---|
| 871 | day_period dp(d); |
|---|
| 872 | ptime t(d, hours(3)+seconds(5)); //an arbitray time on that day |
|---|
| 873 | if (dp.contains(t)) { |
|---|
| 874 | std::cout << to_simple_string(dp) << " contains " |
|---|
| 875 | << to_simple_string(t) << std::endl; |
|---|
| 876 | } |
|---|
| 877 | //a period that represents part of the day |
|---|
| 878 | time_period part_of_day(ptime(d, hours(0)), t); |
|---|
| 879 | //intersect the 2 periods and print the results |
|---|
| 880 | if (part_of_day.intersects(dp)) { |
|---|
| 881 | time_period result = part_of_day.intersection(dp); |
|---|
| 882 | std::cout << to_simple_string(dp) << " intersected with\n" |
|---|
| 883 | << to_simple_string(part_of_day) << " is \n" |
|---|
| 884 | << to_simple_string(result) << std::endl; |
|---|
| 885 | } |
|---|
| 886 | |
|---|
| 887 | |
|---|
| 888 | return 0; |
|---|
| 889 | } |
|---|
| 890 | |
|---|
| 891 | |
|---|
| 892 | </pre> |
|---|
| 893 | </div> |
|---|
| 894 | <div class="section" lang="en"> |
|---|
| 895 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 896 | <a name="date_time.examples.simple_time_zone"></a>Simple Time Zones</h3></div></div></div> |
|---|
| 897 | <p> |
|---|
| 898 | Example usage of custom_time_zone as well as posix_time_zone. |
|---|
| 899 | </p> |
|---|
| 900 | <pre class="programlisting"> |
|---|
| 901 | |
|---|
| 902 | /* A simple example for using a custom_time_zone and a posix_time_zone. |
|---|
| 903 | */ |
|---|
| 904 | |
|---|
| 905 | #include "boost/date_time/local_time/local_time.hpp" |
|---|
| 906 | #include <iostream> |
|---|
| 907 | |
|---|
| 908 | int |
|---|
| 909 | main() |
|---|
| 910 | { |
|---|
| 911 | using namespace boost; |
|---|
| 912 | using namespace local_time; |
|---|
| 913 | using namespace gregorian; |
|---|
| 914 | using posix_time::time_duration; |
|---|
| 915 | |
|---|
| 916 | /***** custom_time_zone *****/ |
|---|
| 917 | |
|---|
| 918 | // create the dependent objects for a custom_time_zone |
|---|
| 919 | time_zone_names tzn("Eastern Standard Time", "EST", |
|---|
| 920 | "Eastern Daylight Time", "EDT"); |
|---|
| 921 | time_duration utc_offset(-5,0,0); |
|---|
| 922 | dst_adjustment_offsets adj_offsets(time_duration(1,0,0), |
|---|
| 923 | time_duration(2,0,0), |
|---|
| 924 | time_duration(2,0,0)); |
|---|
| 925 | // rules for this zone are: |
|---|
| 926 | // start on first Sunday of April at 2 am |
|---|
| 927 | // end on last Sunday of October at 2 am |
|---|
| 928 | // so we use a first_last_dst_rule |
|---|
| 929 | first_day_of_the_week_in_month start_rule(Sunday, Apr); |
|---|
| 930 | last_day_of_the_week_in_month end_rule(Sunday, Oct); |
|---|
| 931 | shared_ptr<dst_calc_rule> nyc_rules(new first_last_dst_rule(start_rule, |
|---|
| 932 | end_rule)); |
|---|
| 933 | // create more dependent objects for a non-dst custom_time_zone |
|---|
| 934 | time_zone_names tzn2("Mountain Standard Time", "MST", |
|---|
| 935 | "", ""); // no dst means empty dst strings |
|---|
| 936 | time_duration utc_offset2(-7,0,0); |
|---|
| 937 | dst_adjustment_offsets adj_offsets2(time_duration(0,0,0), |
|---|
| 938 | time_duration(0,0,0), |
|---|
| 939 | time_duration(0,0,0)); |
|---|
| 940 | // no dst means we need a null pointer to the rules |
|---|
| 941 | shared_ptr<dst_calc_rule> phx_rules; |
|---|
| 942 | |
|---|
| 943 | // create the custom_time_zones |
|---|
| 944 | time_zone_ptr nyc_1(new custom_time_zone(tzn, utc_offset, |
|---|
| 945 | adj_offsets, nyc_rules)); |
|---|
| 946 | time_zone_ptr phx_1(new custom_time_zone(tzn2, utc_offset2, |
|---|
| 947 | adj_offsets2, phx_rules)); |
|---|
| 948 | |
|---|
| 949 | /***** posix_time_zone *****/ |
|---|
| 950 | |
|---|
| 951 | // create posix_time_zones that are the duplicates of the |
|---|
| 952 | // custom_time_zones created above. See posix_time_zone documentation |
|---|
| 953 | // for details on full zone names. |
|---|
| 954 | std::string nyc_string, phx_string; |
|---|
| 955 | nyc_string = "EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00"; |
|---|
| 956 | // nyc_string = "EST-05EDT,M4.1.0,M10.5.0"; // shorter when defaults used |
|---|
| 957 | phx_string = "MST-07"; // no-dst |
|---|
| 958 | time_zone_ptr nyc_2(new posix_time_zone(nyc_string)); |
|---|
| 959 | time_zone_ptr phx_2(new posix_time_zone(phx_string)); |
|---|
| 960 | |
|---|
| 961 | |
|---|
| 962 | /***** show the sets are equal *****/ |
|---|
| 963 | |
|---|
| 964 | std::cout << "The first zone is in daylight savings from:\n " |
|---|
| 965 | << nyc_1->dst_local_start_time(2004) << " through " |
|---|
| 966 | << nyc_1->dst_local_end_time(2004) << std::endl; |
|---|
| 967 | |
|---|
| 968 | std::cout << "The second zone is in daylight savings from:\n " |
|---|
| 969 | << nyc_2->dst_local_start_time(2004) << " through " |
|---|
| 970 | << nyc_2->dst_local_end_time(2004) << std::endl; |
|---|
| 971 | |
|---|
| 972 | std::cout << "The third zone (no daylight savings):\n " |
|---|
| 973 | << phx_1->std_zone_abbrev() << " and " |
|---|
| 974 | << phx_1->base_utc_offset() << std::endl; |
|---|
| 975 | |
|---|
| 976 | std::cout << "The fourth zone (no daylight savings):\n " |
|---|
| 977 | << phx_2->std_zone_abbrev() << " and " |
|---|
| 978 | << phx_2->base_utc_offset() << std::endl; |
|---|
| 979 | |
|---|
| 980 | return 0; |
|---|
| 981 | } |
|---|
| 982 | |
|---|
| 983 | </pre> |
|---|
| 984 | </div> |
|---|
| 985 | <div class="section" lang="en"> |
|---|
| 986 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 987 | <a name="date_time.examples.calc_rules"></a>Daylight Savings Calc Rules</h3></div></div></div> |
|---|
| 988 | <p> |
|---|
| 989 | Example of creating various Daylight Savings Calc Rule objects. |
|---|
| 990 | </p> |
|---|
| 991 | <pre class="programlisting"> |
|---|
| 992 | |
|---|
| 993 | /* A simple example for creating various dst_calc_rule instances |
|---|
| 994 | */ |
|---|
| 995 | |
|---|
| 996 | #include "boost/date_time/gregorian/gregorian.hpp" |
|---|
| 997 | #include "boost/date_time/local_time/local_time.hpp" |
|---|
| 998 | #include <iostream> |
|---|
| 999 | |
|---|
| 1000 | int |
|---|
| 1001 | main() |
|---|
| 1002 | { |
|---|
| 1003 | using namespace boost; |
|---|
| 1004 | using namespace local_time; |
|---|
| 1005 | using namespace gregorian; |
|---|
| 1006 | |
|---|
| 1007 | /***** create the necessary date_generator objects *****/ |
|---|
| 1008 | // starting generators |
|---|
| 1009 | first_day_of_the_week_in_month fd_start(Sunday, May); |
|---|
| 1010 | last_day_of_the_week_in_month ld_start(Sunday, May); |
|---|
| 1011 | nth_day_of_the_week_in_month nkd_start(nth_day_of_the_week_in_month::third, |
|---|
| 1012 | Sunday, May); |
|---|
| 1013 | partial_date pd_start(1, May); |
|---|
| 1014 | // ending generators |
|---|
| 1015 | first_day_of_the_week_in_month fd_end(Sunday, Oct); |
|---|
| 1016 | last_day_of_the_week_in_month ld_end(Sunday, Oct); |
|---|
| 1017 | nth_day_of_the_week_in_month nkd_end(nth_day_of_the_week_in_month::third, |
|---|
| 1018 | Sunday, Oct); |
|---|
| 1019 | partial_date pd_end(31, Oct); |
|---|
| 1020 | |
|---|
| 1021 | /***** create the various dst_calc_rule objects *****/ |
|---|
| 1022 | dst_calc_rule_ptr pdr(new partial_date_dst_rule(pd_start, pd_end)); |
|---|
| 1023 | dst_calc_rule_ptr flr(new first_last_dst_rule(fd_start, ld_end)); |
|---|
| 1024 | dst_calc_rule_ptr llr(new last_last_dst_rule(ld_start, ld_end)); |
|---|
| 1025 | dst_calc_rule_ptr nlr(new nth_last_dst_rule(nkd_start, ld_end)); |
|---|
| 1026 | dst_calc_rule_ptr ndr(new nth_day_of_the_week_in_month_dst_rule(nkd_start, |
|---|
| 1027 | nkd_end)); |
|---|
| 1028 | |
|---|
| 1029 | return 0; |
|---|
| 1030 | } |
|---|
| 1031 | |
|---|
| 1032 | |
|---|
| 1033 | </pre> |
|---|
| 1034 | </div> |
|---|
| 1035 | <div class="section" lang="en"> |
|---|
| 1036 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 1037 | <a name="date_time.examples.flight"></a>Flight Time Example</h3></div></div></div> |
|---|
| 1038 | <p>This example shows a program that calculates the arrival time of a plane that flys from Phoenix to New York. During the flight New York shifts into daylight savings time (Phoenix doesn't because Arizona doesn't use dst).</p> |
|---|
| 1039 | <pre class="programlisting"> |
|---|
| 1040 | <code class="literal"> |
|---|
| 1041 | <span class="preprocessor">#include</span><span class="string"> "boost/date_time/local_time/local_time.hpp"</span><span class="preprocessor"> |
|---|
| 1042 | #include</span><span class="special"> <</span><span class="identifier">iostream</span><span class="special">></span><span class="comment"> |
|---|
| 1043 | |
|---|
| 1044 | /* This example shows a program that calculates the arrival time of a plane |
|---|
| 1045 | * that flys from Phoenix to New York. During the flight New York shifts |
|---|
| 1046 | * into daylight savings time (Phoenix doesn't because Arizona doesn't use |
|---|
| 1047 | * dst). |
|---|
| 1048 | * |
|---|
| 1049 | * |
|---|
| 1050 | */</span><span class="keyword"> |
|---|
| 1051 | |
|---|
| 1052 | int</span><span class="identifier"> main</span><span class="special">()</span><span class="special"> |
|---|
| 1053 | {</span><span class="keyword"> |
|---|
| 1054 | using</span><span class="keyword"> namespace</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">gregorian</span><span class="special">;</span><span class="keyword"> |
|---|
| 1055 | using</span><span class="keyword"> namespace</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">local_time</span><span class="special">;</span><span class="keyword"> |
|---|
| 1056 | using</span><span class="keyword"> namespace</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">posix_time</span><span class="special">;</span><span class="comment"> |
|---|
| 1057 | |
|---|
| 1058 | |
|---|
| 1059 | //setup some timezones for creating and adjusting local times |
|---|
| 1060 | //This user editable file can be found in libs/date_time/data. |
|---|
| 1061 | </span><span class="identifier"> tz_database</span><span class="identifier"> tz_db</span><span class="special">;</span><span class="identifier"> |
|---|
| 1062 | tz_db</span><span class="special">.</span><span class="identifier">load_from_file</span><span class="special">(</span><span class="string">"date_time_zonespec.csv"</span><span class="special">);</span><span class="identifier"> |
|---|
| 1063 | time_zone_ptr</span><span class="identifier"> nyc_tz</span><span class="special"> =</span><span class="identifier"> tz_db</span><span class="special">.</span><span class="identifier">time_zone_from_region</span><span class="special">(</span><span class="string">"America/New_York"</span><span class="special">);</span><span class="comment"> |
|---|
| 1064 | //Use a |
|---|
| 1065 | </span><span class="identifier"> time_zone_ptr</span><span class="identifier"> phx_tz</span><span class="special">(</span><span class="keyword">new</span><span class="identifier"> posix_time_zone</span><span class="special">(</span><span class="string">"MST-07:00:00"</span><span class="special">));</span><span class="comment"> |
|---|
| 1066 | |
|---|
| 1067 | //local departure time in phoenix is 11 pm on april 2 2005 |
|---|
| 1068 | // (ny changes to dst on apr 3 at 2 am) |
|---|
| 1069 | </span><span class="identifier"> local_date_time</span><span class="identifier"> phx_departure</span><span class="special">(</span><span class="identifier">date</span><span class="special">(</span><span class="number">2005</span><span class="special">,</span><span class="identifier"> Apr</span><span class="special">,</span><span class="number"> 2</span><span class="special">),</span><span class="identifier"> hours</span><span class="special">(</span><span class="number">23</span><span class="special">),</span><span class="identifier"> |
|---|
| 1070 | phx_tz</span><span class="special">,</span><span class="identifier"> |
|---|
| 1071 | local_date_time</span><span class="special">::</span><span class="identifier">NOT_DATE_TIME_ON_ERROR</span><span class="special">);</span><span class="identifier"> |
|---|
| 1072 | |
|---|
| 1073 | time_duration</span><span class="identifier"> flight_length</span><span class="special"> =</span><span class="identifier"> hours</span><span class="special">(</span><span class="number">4</span><span class="special">)</span><span class="special"> +</span><span class="identifier"> minutes</span><span class="special">(</span><span class="number">30</span><span class="special">);</span><span class="identifier"> |
|---|
| 1074 | local_date_time</span><span class="identifier"> phx_arrival</span><span class="special"> =</span><span class="identifier"> phx_departure</span><span class="special"> +</span><span class="identifier"> flight_length</span><span class="special">;</span><span class="identifier"> |
|---|
| 1075 | local_date_time</span><span class="identifier"> nyc_arrival</span><span class="special"> =</span><span class="identifier"> phx_arrival</span><span class="special">.</span><span class="identifier">local_time_in</span><span class="special">(</span><span class="identifier">nyc_tz</span><span class="special">);</span><span class="identifier"> |
|---|
| 1076 | |
|---|
| 1077 | std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> <<</span><span class="string"> "departure phx time: "</span><span class="special"> <<</span><span class="identifier"> phx_departure</span><span class="special"> <<</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="identifier"> |
|---|
| 1078 | std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> <<</span><span class="string"> "arrival phx time: "</span><span class="special"> <<</span><span class="identifier"> phx_arrival</span><span class="special"> <<</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="identifier"> |
|---|
| 1079 | std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> <<</span><span class="string"> "arrival nyc time: "</span><span class="special"> <<</span><span class="identifier"> nyc_arrival</span><span class="special"> <<</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="special"> |
|---|
| 1080 | |
|---|
| 1081 | }</span> |
|---|
| 1082 | </code> |
|---|
| 1083 | </pre> |
|---|
| 1084 | </div> |
|---|
| 1085 | <div class="section" lang="en"> |
|---|
| 1086 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 1087 | <a name="date_time.examples.seconds_since_epoch"></a>Seconds Since Epoch</h3></div></div></div> |
|---|
| 1088 | <p> |
|---|
| 1089 | Example of calculating seconds elapsed since epoch (1970-Jan-1) using local_date_time. |
|---|
| 1090 | </p> |
|---|
| 1091 | <pre class="programlisting"> |
|---|
| 1092 | |
|---|
| 1093 | /* This example demonstrates the use of the time zone database and |
|---|
| 1094 | * local time to calculate the number of seconds since the UTC |
|---|
| 1095 | * time_t epoch 1970-01-01 00:00:00. Note that the selected timezone |
|---|
| 1096 | * could be any timezone supported in the time zone database file which |
|---|
| 1097 | * can be modified and updated as needed by the user. |
|---|
| 1098 | * |
|---|
| 1099 | * To solve this problem the following steps are required: |
|---|
| 1100 | * 1) Get a timezone from the tz database for the local time |
|---|
| 1101 | * 2) Construct a local time using the timezone |
|---|
| 1102 | * 3) Construct a posix_time::ptime for the time_t epoch time |
|---|
| 1103 | * 4) Convert the local_time to utc and subtract the epoch time |
|---|
| 1104 | * |
|---|
| 1105 | */ |
|---|
| 1106 | |
|---|
| 1107 | #include "boost/date_time/local_time/local_time.hpp" |
|---|
| 1108 | #include <iostream> |
|---|
| 1109 | |
|---|
| 1110 | int main() |
|---|
| 1111 | { |
|---|
| 1112 | using namespace boost::gregorian; |
|---|
| 1113 | using namespace boost::local_time; |
|---|
| 1114 | using namespace boost::posix_time; |
|---|
| 1115 | |
|---|
| 1116 | tz_database tz_db; |
|---|
| 1117 | try { |
|---|
| 1118 | tz_db.load_from_file("../data/date_time_zonespec.csv"); |
|---|
| 1119 | }catch(data_not_accessible dna) { |
|---|
| 1120 | std::cerr << "Error with time zone data file: " << dna.what() << std::endl; |
|---|
| 1121 | exit(EXIT_FAILURE); |
|---|
| 1122 | }catch(bad_field_count bfc) { |
|---|
| 1123 | std::cerr << "Error with time zone data file: " << bfc.what() << std::endl; |
|---|
| 1124 | exit(EXIT_FAILURE); |
|---|
| 1125 | } |
|---|
| 1126 | |
|---|
| 1127 | time_zone_ptr nyc_tz = tz_db.time_zone_from_region("America/New_York"); |
|---|
| 1128 | date in_date(2004,10,04); |
|---|
| 1129 | time_duration td(12,14,32); |
|---|
| 1130 | // construct with local time value |
|---|
| 1131 | // create not-a-date-time if invalid (eg: in dst transition) |
|---|
| 1132 | local_date_time nyc_time(in_date, |
|---|
| 1133 | td, |
|---|
| 1134 | nyc_tz, |
|---|
| 1135 | local_date_time::NOT_DATE_TIME_ON_ERROR); |
|---|
| 1136 | |
|---|
| 1137 | std::cout << nyc_time << std::endl; |
|---|
| 1138 | |
|---|
| 1139 | ptime time_t_epoch(date(1970,1,1)); |
|---|
| 1140 | std::cout << time_t_epoch << std::endl; |
|---|
| 1141 | |
|---|
| 1142 | // first convert nyc_time to utc via the utc_time() |
|---|
| 1143 | // call and subtract the ptime. |
|---|
| 1144 | time_duration diff = nyc_time.utc_time() - time_t_epoch; |
|---|
| 1145 | |
|---|
| 1146 | //Expected 1096906472 |
|---|
| 1147 | std::cout << "Seconds diff: " << diff.total_seconds() << std::endl; |
|---|
| 1148 | |
|---|
| 1149 | } |
|---|
| 1150 | |
|---|
| 1151 | </pre> |
|---|
| 1152 | </div> |
|---|
| 1153 | </div> |
|---|
| 1154 | <table width="100%"><tr> |
|---|
| 1155 | <td align="left"></td> |
|---|
| 1156 | <td align="right"><small>Copyright © 2001-2005 CrystalClear Software, Inc</small></td> |
|---|
| 1157 | </tr></table> |
|---|
| 1158 | <hr> |
|---|
| 1159 | <div class="spirit-nav"> |
|---|
| 1160 | <a accesskey="p" href="details.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../date_time.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="doxy.html"><img src="../images/next.png" alt="Next"></a> |
|---|
| 1161 | </div> |
|---|
| 1162 | </body> |
|---|
| 1163 | </html> |
|---|