/
date.cpp
163 lines (133 loc) · 3.54 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
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
/*
* The Doomsday Engine Project -- libcore
*
* Copyright © 2009-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/Date"
#include <chrono>
#include <ctime>
#include <c_plus/time.h>
namespace de {
using namespace std::chrono;
DE_PIMPL_NOREF(Date)
{
Time time;
iDate date;
Impl() { zap(date); }
Impl(const Time &time)
: time(time)
{
initSinceEpoch_Date(&date, time.toTime_t());
date.nsecs = (time.millisecondsSinceEpoch() % 1000) * 1000000L;
}
};
Date::Date() : d(new Impl)
{}
Date::Date(const Time &time) : d(new Impl(time))
{}
Date Date::fromJulianDayNumber(int julianDay) // static
{
// https://en.wikipedia.org/wiki/Julian_day#Converting_Gregorian_calendar_date_to_Julian_Day_Number
const int y = 4716;
const int j = 1401;
const int m = 2;
const int n = 12;
const int r = 4;
const int p = 1461;
const int v = 3;
const int u = 5;
const int s = 153;
const int w = 2;
const int B = 274277;
const int C = -38;
int f = julianDay + j + (((4 * julianDay + B) / 146097) * 3) / 4 + C;
int e = r * f + v;
int g = mod(e, p) / r;
int h = u * g + w;
int D = mod(h, s) / u + 1;
int M = mod(h / s + m, n) + 1;
int Y = (e / p) - y + (n + m - M) / n;
return Time(Y, M, D, 0, 0, 0);
}
int Date::year() const
{
return d->date.year;
}
int Date::month() const
{
return d->date.month;
}
int Date::dayOfMonth() const
{
return d->date.day;
}
int Date::dayOfYear() const
{
return d->date.dayOfYear;
}
int Date::julianDayNumber() const
{
// For reference, see
// https://en.wikipedia.org/wiki/Julian_day#Converting_Gregorian_calendar_date_to_Julian_Day_Number
const int Y = year();
const int M = month();
const int D = dayOfMonth();
return (1461 * (Y + 4800 + (M - 14)/12))/4 +
(367 * (M - 2 - 12 * ((M - 14)/12)))/12 -
(3 * ((Y + 4900 + (M - 14)/12)/100))/4 + D - 32075;
}
int Date::hours() const
{
return d->date.hour;
}
int Date::minutes() const
{
return d->date.minute;
}
ddouble Date::seconds() const
{
return d->date.second + double(d->date.nsecs) / 1.0e9;
}
int Date::daysTo(const Date &other) const
{
return duration_cast<std::chrono::minutes>(other.d->time.toTimePoint() - d->time.toTimePoint())
.count() / (60 * 24);
}
Time Date::asTime() const
{
return d->time;
}
Date Date::currentDate()
{
return Time().asDate();
}
Date Date::fromText(String const &text)
{
return Time::fromText(text, Time::HumanDate);
}
String Date::format(const char *format) const
{
const time_t tm = d->time.toTime_t();
char buf[256];
strftime(buf, sizeof(buf) - 1, format, localtime(&tm));
return buf;
}
std::ostream &operator<<(std::ostream &os, const Date &d)
{
os << d.asText().c_str();
return os;
}
} // namespace de