ex_days_alive.xml 6.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
  3. "../../../tools/boostbook/dtd/boostbook.dtd">
  4. <!-- Copyright (c) 2001-2004 CrystalClear Software, Inc.
  5. Subject to the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <section id="date_time.examples.days_alive">
  9. <title>Days Alive</title>
  10. <para>
  11. Calculate the number of days you have been living using durations and dates.
  12. </para>
  13. <programlisting>
  14. <literal>
  15. <phrase role="comment">/* Short example that calculates the number of days since user was born.
  16. * Demonstrates comparisons of durations, use of the day_clock,
  17. * and parsing a date from a string.
  18. */</phrase><phrase role="preprocessor">
  19. #include</phrase><phrase role="string"> &quot;boost/date_time/gregorian/gregorian.hpp&quot;</phrase><phrase role="preprocessor">
  20. #include</phrase><phrase role="special"> &lt;</phrase><phrase role="identifier">iostream</phrase><phrase role="special">&gt;</phrase><phrase role="keyword">
  21. int</phrase><phrase role="identifier">
  22. main</phrase><phrase role="special">()</phrase><phrase role="special">
  23. {</phrase><phrase role="keyword">
  24. using</phrase><phrase role="keyword"> namespace</phrase><phrase role="identifier"> boost</phrase><phrase role="special">::</phrase><phrase role="identifier">gregorian</phrase><phrase role="special">;</phrase><phrase role="identifier">
  25. std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase><phrase role="identifier"> s</phrase><phrase role="special">;</phrase><phrase role="identifier">
  26. std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase><phrase role="special"> &lt;&lt;</phrase><phrase role="string"> &quot;Enter birth day YYYY-MM-DD (eg: 2002-02-01): &quot;</phrase><phrase role="special">;</phrase><phrase role="identifier">
  27. std</phrase><phrase role="special">::</phrase><phrase role="identifier">cin</phrase><phrase role="special"> &gt;&gt;</phrase><phrase role="identifier"> s</phrase><phrase role="special">;</phrase><phrase role="keyword">
  28. try</phrase><phrase role="special"> {</phrase><phrase role="identifier">
  29. date</phrase><phrase role="identifier"> birthday</phrase><phrase role="special">(</phrase><phrase role="identifier">from_simple_string</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">));</phrase><phrase role="identifier">
  30. date</phrase><phrase role="identifier"> today</phrase><phrase role="special"> =</phrase><phrase role="identifier"> day_clock</phrase><phrase role="special">::</phrase><phrase role="identifier">local_day</phrase><phrase role="special">();</phrase><phrase role="identifier">
  31. days</phrase><phrase role="identifier"> days_alive</phrase><phrase role="special"> =</phrase><phrase role="identifier"> today</phrase><phrase role="special"> -</phrase><phrase role="identifier"> birthday</phrase><phrase role="special">;</phrase><phrase role="identifier">
  32. days</phrase><phrase role="identifier"> one_day</phrase><phrase role="special">(</phrase><phrase role="number">1</phrase><phrase role="special">);</phrase><phrase role="keyword">
  33. if</phrase><phrase role="special"> (</phrase><phrase role="identifier">days_alive</phrase><phrase role="special"> ==</phrase><phrase role="identifier"> one_day</phrase><phrase role="special">)</phrase><phrase role="special"> {</phrase><phrase role="identifier">
  34. std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase><phrase role="special"> &lt;&lt;</phrase><phrase role="string"> &quot;Born yesterday, very funny&quot;</phrase><phrase role="special"> &lt;&lt;</phrase><phrase role="identifier"> std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase><phrase role="special">
  35. }</phrase><phrase role="keyword">
  36. else</phrase><phrase role="keyword"> if</phrase><phrase role="special"> (</phrase><phrase role="identifier">days_alive</phrase><phrase role="special"> &lt;</phrase><phrase role="identifier"> days</phrase><phrase role="special">(</phrase><phrase role="number">0</phrase><phrase role="special">))</phrase><phrase role="special"> {</phrase><phrase role="identifier">
  37. std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase><phrase role="special"> &lt;&lt;</phrase><phrase role="string"> &quot;Not born yet, hmm: &quot;</phrase><phrase role="special"> &lt;&lt;</phrase><phrase role="identifier"> days_alive</phrase><phrase role="special">.</phrase><phrase role="identifier">days</phrase><phrase role="special">()</phrase><phrase role="special">
  38. &lt;&lt;</phrase><phrase role="string"> &quot; days&quot;</phrase><phrase role="special"> &lt;&lt;</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase><phrase role="special">
  39. }</phrase><phrase role="keyword">
  40. else</phrase><phrase role="special"> {</phrase><phrase role="identifier">
  41. std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase><phrase role="special"> &lt;&lt;</phrase><phrase role="string"> &quot;Days alive: &quot;</phrase><phrase role="special"> &lt;&lt;</phrase><phrase role="identifier"> days_alive</phrase><phrase role="special">.</phrase><phrase role="identifier">days</phrase><phrase role="special">()</phrase><phrase role="special"> &lt;&lt;</phrase><phrase role="identifier"> std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase><phrase role="special">
  42. }</phrase><phrase role="special">
  43. }</phrase><phrase role="keyword">
  44. catch</phrase><phrase role="special">(...)</phrase><phrase role="special"> {</phrase><phrase role="identifier">
  45. std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase><phrase role="special"> &lt;&lt;</phrase><phrase role="string"> &quot;Bad date entered: &quot;</phrase><phrase role="special"> &lt;&lt;</phrase><phrase role="identifier"> s</phrase><phrase role="special"> &lt;&lt;</phrase><phrase role="identifier"> std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase><phrase role="special">
  46. }</phrase><phrase role="keyword">
  47. return</phrase><phrase role="number"> 0</phrase><phrase role="special">;</phrase><phrase role="special">
  48. }</phrase>
  49. </literal>
  50. </programlisting>
  51. </section>