This repository has been archived by the owner on Nov 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.cpp
187 lines (164 loc) · 5.78 KB
/
calendar.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdexcept>
#include <ctime>
#include <iostream>
#include <assert.h>
#include "calendar.hpp"
#include "gregorian.hpp"
#include "julian.hpp"
namespace lab2 {
template <typename DateType>
Calendar<DateType>::Calendar() {
time_t unix_timestamp = time(0);
struct tm *date = gmtime(&unix_timestamp);
Gregorian g(date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
now = g;
}
template <typename DateType>
template<typename T>
Calendar<DateType>::Calendar(const Calendar<T> &src) {
copy_calendar(src);
}
template <typename DateType>
template <typename T>
void Calendar<DateType>::copy_calendar(const Calendar<T> &src) {
for (auto i = src.get_event_dates().begin(); i < src.get_event_dates().size(); ++i) {
event_dates.push_back(DateType(*i));
}
}
template <typename DateType>
std::vector<DateType> Calendar<DateType>::get_event_dates() const {
return event_dates;
}
template <typename DateType>
std::vector<std::string> Calendar<DateType>::get_event_names() const {
return event_names;
}
template <typename DateType>
DateType Calendar<DateType>::get_date_now() const {
return now;
}
template <typename DateType>
template <typename T>
void Calendar<DateType>::operator=(const Calendar<T> &src) {
copy_calendar(src);
}
template <typename DateType>
Calendar<DateType>::Calendar(DateType date) : now(date) {}
template <typename DateType>
bool Calendar<DateType>::set_date(int year, int month, int day) {
try {
now = DateType(year, month, day);
return true;
}
catch (std::out_of_range &e) {
return false;
}
}
template <typename DateType>
bool Calendar<DateType>::add_event(std::string event_name) {
return add_event( event_name, now.day(), now.month(), now.year() );
}
template <typename DateType>
bool Calendar<DateType>::add_event(std::string event_name, int day) {
return add_event( event_name, day, now.month(), now.year() );
}
template <typename DateType>
bool Calendar<DateType>::add_event(std::string event_name, int day, int month) {
return add_event( event_name, day, month, now.year() );
}
template <typename DateType>
bool Calendar<DateType>::add_event(std::string event_name, int day, int month, int year) {
try {
DateType date(year, month, day);
if ( calendar_contains_event(event_name, date) )
return false;
event_names.push_back( event_name );
event_dates.push_back( date );
return true;
}
catch (std::out_of_range &e) {
return false;
}
}
template <typename DateType>
bool Calendar<DateType>::calendar_contains_event(std::string &event_name, DateType &event_date) const {
auto i = event_names.begin();
auto j = event_dates.begin();
while (i != event_names.end()) {
if (*i == event_name && *j == event_date) {
return true;
}
++i;
++j;
}
return false;
}
template <typename DateType>
bool Calendar<DateType>::remove_event(std::string event_name) {
return remove_event( event_name, now.day(), now.month(), now.year() );
}
template <typename DateType>
bool Calendar<DateType>::remove_event(std::string event_name, int day) {
return remove_event( event_name, day, now.month(), now.year() );
}
template <typename DateType>
bool Calendar<DateType>::remove_event(std::string event_name, int day, int month) {
return remove_event( event_name, now.day(), month, now.year() );
}
template <typename DateType>
bool Calendar<DateType>::remove_event(std::string event_name, int day, int month, int year) {
try {
DateType date(year, month, day);
return remove_calendar_event(event_name, date);
}
catch (std::out_of_range &e) {
return false;
}
}
template <typename DateType>
bool Calendar<DateType>::remove_calendar_event(std::string &event_name, DateType &event_date) {
auto i = event_names.begin();
auto j = event_dates.begin();
while (i != event_names.end()) {
if (*i == event_name && *j == event_date) {
event_names.erase(i);
event_dates.erase(j);
return true;
}
++i;
++j;
}
return false;
}
template <typename DateType>
std::ostream& operator<<(std::ostream &os, const Calendar<DateType> &calendar) {
// https://en.wikipedia.org/wiki/ICalendar
os << "BEGIN:VCALENDAR" << std::endl;
os << "VERSION:2.0" << std::endl;
auto i = calendar.event_names.begin();
auto j = calendar.event_dates.begin();
while (i != calendar.event_names.end()) {
if (*j >= calendar.now) {
std::string event_name = *i;
DateType event_date = *j;
std::string event_year = event_date.year() < 10 ? "0" + std::to_string(event_date.year()) : std::to_string( event_date.year() );
std::string event_month = event_date.year() < 10 ? "0" + std::to_string(event_date.month()) : std::to_string( event_date.month() );
std::string event_day = event_date.day() < 10 ? "0" + std::to_string(event_date.day()) : std::to_string( event_date.day() );
std::string year_now = calendar.now.year() < 10 ? "0" + std::to_string(calendar.now.year()) : std::to_string( calendar.now.year() );
std::string month_now = calendar.now.month() < 10 ? "0" + std::to_string(calendar.now.month()) : std::to_string( calendar.now.month() );
std::string day_now = calendar.now.day() < 10 ? "0" + std::to_string(calendar.now.day()) : std::to_string( calendar.now.day() );
os << "BEGIN:VEVENT" << std::endl;
os << "DTSTAMP:" << year_now << month_now << day_now << "T120000Z" << std::endl;
os << "ORGANIZER;CN=Bastian Fredriksson:MAILTO:bastianf@kth.se" << std::endl;
os << "DTSTART:" << event_year << event_month << event_day << "T080000Z" << std::endl;
os << "DTEND:" << event_year << event_month << event_day << "T235959Z" << std::endl;
os << "SUMMARY:" << event_name << std::endl;
os << "END:VEVENT" << std::endl;
}
++i;
++j;
}
os << "END:VCALENDAR" << std::endl;
return os;
}
}