-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDate.cpp
128 lines (102 loc) · 2.45 KB
/
Date.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
//
// Date.cpp
// Maw Kit
//
// Created by Lluís Ulzurrun de Asanza Sàez on 29/02/16.
//
//
#include "Date.hpp"
namespace MK {
typedef Date Self;
const Self Self::Today()
{
std::time_t rawtime;
std::time( &rawtime );
return Self( rawtime );
}
const Self Self::EasterStart()
{
auto end = Self::EasterEnd();
unsigned short day = end.getDay();
auto month = end.getMonth();
if ( day < 15 ) {
month = static_cast<Self::Month>( static_cast<int>( month ) - 1 );
day = 31 - 15 + day;
}
else {
day -= 15;
}
return Date( day, month, end.getYear() );
}
const Self Self::EasterEnd()
{
auto today = Self::Today();
int A = today.getYear() % 19;
int B = int( today.getYear() / 100 );
int C = today.getYear() % 100;
int D = B / 4;
int E = B % 4;
int F = ( B + 8 ) / 25;
int G = ( B - F + 1 ) / 3;
int H = ( 19 * A + B - D - G + 15 ) % 30;
int I = C / 4;
int K = C % 4;
int L = ( 32 + 2 * E + 2 * I - H - K ) % 7;
int M = ( A + 11 * H + 22 * L ) / 451;
int N = H + L - 7 * M + 114;
int month = N / 31;
int day = 1 + ( N % 31 );
return Date( day, static_cast<Self::Month>( month ), today.getYear() );
}
const Self Self::ChristmasStart()
{
return Date( 10, Self::Month::December, Self::Today().getYear() );
}
const Self Self::ChristmasEnd()
{
return Date( 31, Self::Month::December, Self::Today().getYear() );
}
Self::Date( std::time_t timestamp )
{
this->unixTimestamp = timestamp;
}
Self::Date( unsigned short day, Month month, unsigned long long year )
{
std::tm lDate;
lDate.tm_sec = 0;
lDate.tm_min = 0;
lDate.tm_hour = 0;
lDate.tm_mday = day;
lDate.tm_mon = static_cast<int>( month );
lDate.tm_year = static_cast<int>( year ) - 1900;
std::time_t lTimeEpoch = std::mktime( &lDate );
this->unixTimestamp = lTimeEpoch;
}
const unsigned short Self::getDay() const
{
auto lDate = std::gmtime( &( this->unixTimestamp ) );
return lDate->tm_mday;
}
const Self::Month Self::getMonth() const
{
auto lDate = std::gmtime( &( this->unixTimestamp ) );
return static_cast<Self::Month>( lDate->tm_mon );
}
const unsigned long long Self::getYear() const
{
auto lDate = std::gmtime( &( this->unixTimestamp ) );
return 1900 + lDate->tm_year;
}
bool Self::operator<( const Self &rhs ) const
{
return this->getTimestamp() < rhs.getTimestamp();
}
bool Self::operator==( const Self &rhs ) const
{
return this->getTimestamp() == rhs.getTimestamp();
}
std::time_t Self::getTimestamp() const
{
return this->unixTimestamp;
}
}; // namespace MK