Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is an old version of boost. Click here for the latest version's documentation home page.

days_alive.cpp

Calculate the number of days you have been living using durations and dates.

#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/date_parsing.hpp"
#include <iostream>

int
main() 
{
  
  using namespace boost::gregorian;
  std::string s;
  std::cout << "Enter birth day YYYY-MM-DD (eg: 2002-02-01): ";
  std::cin >> s;
  try {
    date birthday(boost::date_time::parse_date<date>(s));
    //date birthday(2002,Jan,1);
    date today = day_clock::local_day();
    date_duration days_alive = today - birthday;
    date_duration one_day(1);
    if (days_alive == one_day) {
      std::cout << "Born yesterday, very funny" << std::endl;
    }
    else if (days_alive < date_duration(0)) {
      std::cout << "Not born yet, hmm: " << days_alive.days() 
          << " days" <<std::endl;
    }
    else {
      std::cout << "Days alive: " << days_alive.days() << std::endl;
    }

  }
  catch(...) {
    std::cout << "Bad date entered: " << s << std::endl;
  }
  return 0;
};

Generated Wed Aug 21 16:54:33 2002 by Doxygen for CrystalClear Software © 2000-2002