-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.cc
94 lines (81 loc) · 2.99 KB
/
calendar.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// Created by pawel on 18/02/16.
//
#include "calendar.h"
#include <boost/format.hpp>
namespace Calendar {
Calendar::Calendar(unsigned short year) :
year_(year),
yearRange_(day_iterator(date(greg_year(year_), 1, 1)), day_iterator(date(greg_year(year_), 12, 31))) {
}
Calendar::~Calendar() {
}
void Calendar::display(std::ostream &str) {
auto months = getMonths();
for(auto month : months) {
str << "\n" << getMonthName(month) << "\n";
auto weeks = getWeeks(month);
for(auto week : weeks) {
str << formatWeek(week) << "\n";
}
}
}
void Calendar::displayInAGrid(std::ostream &str) {
auto months = getMonths();
for(auto row = 0; row < 4; ++row) {
str << getMonthName(months.at(3 * row)) << " "
<< getMonthName(months.at(3 * row + 1)) << " "
<< getMonthName(months.at(3 * row + 2)) << "\n";
auto weeks1 = getWeeks(months.at(3 * row));
auto weeks2 = getWeeks(months.at(3 * row + 1));
auto weeks3 = getWeeks(months.at(3 * row + 2));
for(auto line = 1; line <= 6; ++line) {
line <= weeks1.size() ? str << formatWeek(weeks1.at(line - 1)) : str << boost::str(boost::format("%|22t|"));
str << " ";
line <= weeks2.size() ? str << formatWeek(weeks2.at(line - 1)) : str << boost::str(boost::format("%|22t|"));
str << " ";
line <= weeks3.size() ? str << formatWeek(weeks3.at(line - 1)) : str << boost::str(boost::format("%|22t|"));
str << "\n";
}
}
}
std::vector<DateRange> Calendar::getMonths() {
std::vector<DateRange> months;
auto monthBegin = yearRange_.first;
for(auto day = monthBegin; day <= *yearRange_.second;) {
auto monthEnd = day;
if(day->month() != (++day)->month()) {
months.push_back(DateRange(monthBegin, monthEnd));
monthBegin = day;
}
}
return months;
}
std::vector<DateRange> Calendar::getWeeks(DateRange &month) {
std::vector<DateRange> weeks;
auto weekBegin = month.first;
auto weekEnd = weekBegin;
for(auto day = weekBegin; day <= *month.second;) {
weekEnd = day;
if(day->week_number() != (++day)->week_number()) {
weeks.push_back(DateRange(weekBegin, weekEnd));
weekBegin = day;
}
}
weeks.push_back(DateRange(weekBegin, weekEnd));
return weeks;
}
std::string Calendar::getMonthName(DateRange &month) {
return boost::str(boost::format("%|=22|") % month.first->month().as_long_string());
}
std::string Calendar::formatDay(const date &day) {
return boost::str(boost::format("%|3|") % day.day());
}
std::string Calendar::formatWeek(DateRange &week) {
std::string str;
for(auto day = week.first; day <= *week.second; ++day)
str += formatDay(*day);
auto dayNum = week.first->day_of_week();
return boost::str(boost::format("%1%%2%%|22t|") % std::string((dayNum == Sunday ? 6 : dayNum - 1) * 3, ' ') % str);
}
} // namespace Calendar