1 /* Short example that calculates the number of days since user was born. 2 * Demonstrates comparisons of durations, use of the day_clock, 3 * and parsing a date from a string. 4 */ 5 6 #include "boost/date_time/gregorian/gregorian.hpp" 7 #include <iostream> 8 9 int main()10main() 11 { 12 13 using namespace boost::gregorian; 14 std::string s; 15 std::cout << "Enter birth day YYYY-MM-DD (eg: 2002-02-01): "; 16 std::cin >> s; 17 try { 18 date birthday(from_simple_string(s)); 19 date today = day_clock::local_day(); 20 days days_alive = today - birthday; 21 days one_day(1); 22 if (days_alive == one_day) { 23 std::cout << "Born yesterday, very funny" << std::endl; 24 } 25 else if (days_alive < days(0)) { 26 std::cout << "Not born yet, hmm: " << days_alive.days() 27 << " days" <<std::endl; 28 } 29 else { 30 std::cout << "Days alive: " << days_alive.days() << std::endl; 31 } 32 33 } 34 catch(...) { 35 std::cout << "Bad date entered: " << s << std::endl; 36 } 37 return 0; 38 } 39 40 41 /* Copyright 2001-2004: CrystalClear Software, Inc 42 * http://www.crystalclearsoftware.com 43 * 44 * Subject to the Boost Software License, Version 1.0. 45 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) 46 */ 47 48