Boost.Locale
calendar.cpp

Example of using date_time functions for generating calendar for current year.

//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/locale.hpp>
#include <ctime>
#include <iomanip>
#include <iostream>
int main()
{
using namespace boost::locale;
generator gen;
std::locale::global(gen(""));
std::cout.imbue(std::locale());
// Setup environment
date_time start = now;
// Set the first day of the first month of this year
start.set(period::month(), now.minimum(period::month()));
start.set(period::day(), start.minimum(period::day()));
const int current_year = period::year(now);
// Display current year
std::cout << format("{1,ftime='%Y'}") % now << std::endl;
// Run forward until current year is the date
for(now = start; period::year(now) == current_year;) {
// Print heading of month
std::cout << format("{1,ftime='%B'}") % now << std::endl;
else
std::cout << format("{1,ftime='%B'} ({1,ftime='%Y-%m-%d',locale=en} - {2,locale=en,ftime='%Y-%m-%d'})")
% now % date_time(now, now.maximum(period::day()) * period::day())
<< std::endl;
const int first = calendar().first_day_of_week();
// Print week days
for(int i = 0; i < 7; i++) {
date_time tmp(now, period::day_of_week() * (first + i));
std::cout << format("{1,w=8,ftime='%a'} ") % tmp;
}
std::cout << std::endl;
const int current_month = now / period::month();
const int skip = now / period::day_of_week_local() - 1;
for(int i = 0; i < skip * 9; i++)
std::cout << ' ';
for(; now / period::month() == current_month; now += period::day()) {
std::cout << format("{1,w=8,ftime='%e'} ") % now;
if(now / period::day_of_week_local() == 7)
std::cout << std::endl;
}
std::cout << std::endl;
}
}
a printf like class that allows type-safe and locale aware message formatting
Definition: format.hpp:181
this class provides an access to general calendar information.
Definition: date_time.hpp:466
bool is_gregorian() const
Check if the calendar is Gregorian.
int first_day_of_week() const
Get first day of week for specific calendar, for example for US it is 1 - Sunday for France it is 2 -...
this class represents a date time and allows to perform various operation according to the locale set...
Definition: date_time.hpp:558
void set(period::period_type f, int v)
set specific period f value to v
int minimum(period::period_type f) const
Get minimal possible value for *this time point for a period f.
int maximum(period::period_type f) const
the major class used for locale generation
Definition: generator.hpp:101
This is the main namespace that encloses all localization classes.
Definition: boundary_point.hpp:13