days_alive.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. #include "boost/date_time/gregorian/gregorian.hpp"
  6. #include <iostream>
  7. int
  8. main()
  9. {
  10. using namespace boost::gregorian;
  11. std::string s;
  12. std::cout << "Enter birth day YYYY-MM-DD (eg: 2002-02-01): ";
  13. std::cin >> s;
  14. try {
  15. date birthday(from_simple_string(s));
  16. date today = day_clock::local_day();
  17. days days_alive = today - birthday;
  18. days one_day(1);
  19. if (days_alive == one_day) {
  20. std::cout << "Born yesterday, very funny" << std::endl;
  21. }
  22. else if (days_alive < days(0)) {
  23. std::cout << "Not born yet, hmm: " << days_alive.days()
  24. << " days" <<std::endl;
  25. }
  26. else {
  27. std::cout << "Days alive: " << days_alive.days() << std::endl;
  28. }
  29. }
  30. catch(...) {
  31. std::cout << "Bad date entered: " << s << std::endl;
  32. }
  33. return 0;
  34. }
  35. /* Copyright 2001-2004: CrystalClear Software, Inc
  36. * http://www.crystalclearsoftware.com
  37. *
  38. * Subject to the Boost Software License, Version 1.0.
  39. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  40. */