47#ifndef OPENSWMM_ENGINE_DATETIME_HPP
48#define OPENSWMM_ENGINE_DATETIME_HPP
59static constexpr int DateDelta = 693594;
60static constexpr double SecsPerDay = 86400.0;
61static constexpr double OneSecond = 1.1574074e-5;
76inline void divMod(
int n,
int d,
int* result,
int* remainder) {
77 if (d == 0) { *result = 0; *remainder = 0; }
78 else { *result = n / d; *remainder = n - d * (*result); }
85 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
93 static constexpr int DaysPerMonth[2][12] = {
94 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
95 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
97 if (year >= 1 && year <= 9999 && month >= 1 && month <= 12
98 && day >= 1 && day <= DaysPerMonth[i][month - 1]) {
99 for (
int j = 0; j < month - 1; ++j) day += DaysPerMonth[i][j];
101 return y * 365 +
y / 4 -
y / 100 +
y / 400 + day - DateDelta;
111 if (hour >= 0 && minute >= 0 && second >= 0) {
112 int s = hour * 3600 + minute * 60 + second;
113 return static_cast<double>(s) / SecsPerDay;
123 static constexpr int DaysPerMonth[2][12] = {
124 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
125 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
126 constexpr int D1 = 365, D4 = 1461, D100 = 36524, D400 = 146097;
128 int t =
static_cast<int>(std::floor(date)) + DateDelta;
129 if (t <= 0) { year = 0; month = 1; day = 1;
return; }
133 while (t >= D400) { t -= D400;
y += 400; }
135 if (i == 4) { i--; d += D100; }
140 if (i == 4) { i--; d += D1; }
146 i = DaysPerMonth[k][m - 1];
151 year =
y; month = m; day = d + 1;
162 double fracDay = (time - std::floor(time)) * SecsPerDay;
163 int secs =
static_cast<int>(std::floor(fracDay + 0.5));
164 if (secs >= 86400) secs = 86399;
180 double d = std::floor(date);
183 return d + (3600.0 * h + 60.0 * m + s + seconds) / SecsPerDay;
191 double d1 = std::floor(date1);
192 double d2 = std::floor(date2);
195 long s1 = 3600L * h + 60L * m + s;
197 long s2 = 3600L * h + 60L * m + s;
198 long secs =
static_cast<long>(std::floor((d1 - d2) * SecsPerDay + 0.5));
219 return static_cast<int>(std::floor(date - startOfYear)) + 1;
Definition DateTime.hpp:53
void decodeDate(DateTime date, int &year, int &month, int &day)
Decode DateTime to year-month-day.
Definition DateTime.hpp:122
DateTime encodeTime(int hour, int minute, int second)
Encode hour:minute:second to fractional day.
Definition DateTime.hpp:110
DateTime addSeconds(DateTime date, double seconds)
Add seconds to a DateTime — numerically identical to legacy.
Definition DateTime.hpp:179
double DateTime
Definition DateTime.hpp:67
int monthOfYear(DateTime date)
Get month of year (1..12) from DateTime.
Definition DateTime.hpp:206
bool isLeapYear(int year)
Check if year is a leap year.
Definition DateTime.hpp:84
void divMod(int n, int d, int *result, int *remainder)
Integer divmod — matching legacy divMod().
Definition DateTime.hpp:76
DateTime encodeDate(int year, int month, int day)
Encode year-month-day to DateTime.
Definition DateTime.hpp:92
long timeDiff(DateTime date1, DateTime date2)
Compute difference in seconds between two DateTimes.
Definition DateTime.hpp:190
void decodeTime(DateTime time, int &h, int &m, int &s)
Decode DateTime to hour:minute:second.
Definition DateTime.hpp:161
int dayOfYear(DateTime date)
Get day of year (1..365/366) from DateTime.
Definition DateTime.hpp:215
Definition NodeCoupling.cpp:16
double * y
Definition odesolve.c:28