calendar.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #include <boost/locale.hpp>
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <ctime>
  12. int main()
  13. {
  14. using namespace boost::locale;
  15. generator gen;
  16. std::locale::global(gen(""));
  17. std::cout.imbue(std::locale());
  18. // Setup environment
  19. boost::locale::date_time now;
  20. date_time start=now;
  21. // Set the first day of the first month of this year
  22. start.set(period::month(),now.minimum(period::month()));
  23. start.set(period::day(),start.minimum(period::day()));
  24. int current_year = period::year(now);
  25. // Display current year
  26. std::cout << format("{1,ftime='%Y'}") % now << std::endl;
  27. //
  28. // Run forward untill current year is the date
  29. //
  30. for(now=start; period::year(now) == current_year;) {
  31. // Print heading of month
  32. if(calendar().is_gregorian())
  33. std::cout << format("{1,ftime='%B'}") % now <<std::endl;
  34. else
  35. std::cout << format("{1,ftime='%B'} ({1,ftime='%Y-%m-%d',locale=en} - {2,locale=en,ftime='%Y-%m-%d'})")
  36. % now
  37. % date_time(now,now.maximum(period::day())*period::day()) << std::endl;
  38. int first = calendar().first_day_of_week();
  39. // Print weeks days
  40. for(int i=0;i<7;i++) {
  41. date_time tmp(now,period::day_of_week() * (first + i));
  42. std::cout << format("{1,w=8,ftime='%a'} ") % tmp;
  43. }
  44. std::cout << std::endl;
  45. int current_month = now / period::month();
  46. int skip = now / period::day_of_week_local() - 1;
  47. for(int i=0;i<skip*9;i++)
  48. std::cout << ' ';
  49. for(;now / period::month() == current_month ;now += period::day()) {
  50. std::cout << format("{1,w=8,ftime='%e'} ") % now;
  51. if(now / period::day_of_week_local() == 7)
  52. std::cout << std::endl;
  53. }
  54. std::cout << std::endl;
  55. }
  56. }
  57. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4