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