-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-chrono.cc
99 lines (87 loc) · 2.59 KB
/
07-chrono.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
95
96
97
98
99
//
// Program
// Determine the day of the week from a date
// - This example is discussed in
// Book # Modern C++ Programming Cookbook by Marius Bancila
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++20 -o 07-chrono 07-chrono.cc
//
// Execution
// ./07-chrono
//
#include <iostream>
#include <chrono>
using namespace std::chrono;
// Compiler (C++20) is not having stream operator<<
inline static std::ostream &operator<<(std::ostream &os, const month& m) {
switch (static_cast<unsigned>(m))
{
case 1: os << "January"; break;
case 2: os << "February"; break;
case 3: os << "March"; break;
case 4: os << "April"; break;
case 5: os << "May"; break;
case 6: os << "June"; break;
case 7: os << "July"; break;
case 8: os << "August"; break;
case 9: os << "September"; break;
case 10: os << "October"; break;
case 11: os << "November"; break;
case 12: os << "December"; break;
default: os << "<invalid month: " << static_cast<unsigned>(m) << ">";break;
}
return os;
}
inline static std::ostream &operator<<(std::ostream &os, const sys_days& sd) {
auto ymd = year_month_day(sd);
os << static_cast<unsigned>(ymd.day())
<< '/'
<< ymd.month()
<< '/'
<< static_cast<int>(ymd.year());
return os;
}
inline static std::ostream &operator<<(std::ostream &os, const weekday& wd) {
switch (wd.c_encoding())
{
case 0: os << "Sunday"; break;
case 1: os << "Monday"; break;
case 2: os << "Tuesday"; break;
case 3: os << "Wednesday"; break;
case 4: os << "Thursday"; break;
case 5: os << "Friday"; break;
case 6: os << "Saturday"; break;
default: os << "<invalid day: " << wd.c_encoding() << ">";break;
}
return os;
}
inline static std::ostream &operator<<(std::ostream &os, const weekday_indexed& wi) {
os << wi.weekday() << "(" << wi.index() << ")";
return os;
}
static unsigned int week_day(const sys_days& date) {
year_month_weekday ymw {date};
return ymw.weekday_indexed().index();
}
//
// Entry function
//
int main() {
std::cout << "--- Day of the week ---" << '\n';
{
year_month_weekday ymw {2018y/October/Friday[last]};
std::cout << ymw << '\n'
<< " - " << ymw.weekday_indexed() << '\n'
<< " - " << ymw.weekday_indexed().weekday() << '\n'
<< " - " << ymw.weekday_indexed().index() << '\n'
<< '\n';
}
{
sys_days d1 {2018y/October/13};
sys_days d2 {2018y/November/1};
std::cout << d1 << " week day is " << week_day(d1) << '\n';
std::cout << d2 << " week day is " << week_day(d2) << '\n';
}
return 0;
}