22#ifndef OPENSWMM_ENGINE_DATETIME_HPP
23#define OPENSWMM_ENGINE_DATETIME_HPP
34static constexpr int DateDelta = 693594;
35static constexpr double SecsPerDay = 86400.0;
36static constexpr double OneSecond = 1.1574074e-5;
51inline void divMod(
int n,
int d,
int* result,
int* remainder) {
52 if (d == 0) { *result = 0; *remainder = 0; }
53 else { *result = n / d; *remainder = n - d * (*result); }
60 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
68 static constexpr int DaysPerMonth[2][12] = {
69 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
70 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
72 if (year >= 1 && year <= 9999 && month >= 1 && month <= 12
73 && day >= 1 && day <= DaysPerMonth[i][month - 1]) {
74 for (
int j = 0; j < month - 1; ++j) day += DaysPerMonth[i][j];
76 return y * 365 +
y / 4 -
y / 100 +
y / 400 + day - DateDelta;
86 if (hour >= 0 && minute >= 0 && second >= 0) {
87 int s = hour * 3600 + minute * 60 + second;
88 return static_cast<double>(s) / SecsPerDay;
98 static constexpr int DaysPerMonth[2][12] = {
99 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
100 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
101 constexpr int D1 = 365, D4 = 1461, D100 = 36524, D400 = 146097;
103 int t =
static_cast<int>(std::floor(date)) + DateDelta;
104 if (t <= 0) { year = 0; month = 1; day = 1;
return; }
108 while (t >= D400) { t -= D400;
y += 400; }
110 if (i == 4) { i--; d += D100; }
115 if (i == 4) { i--; d += D1; }
121 i = DaysPerMonth[k][m - 1];
126 year =
y; month = m; day = d + 1;
137 double fracDay = (time - std::floor(time)) * SecsPerDay;
138 int secs =
static_cast<int>(std::floor(fracDay + 0.5));
139 if (secs >= 86400) secs = 86399;
155 double d = std::floor(date);
158 return d + (3600.0 * h + 60.0 * m + s + seconds) / SecsPerDay;
166 double d1 = std::floor(date1);
167 double d2 = std::floor(date2);
170 long s1 = 3600L * h + 60L * m + s;
172 long s2 = 3600L * h + 60L * m + s;
173 long secs =
static_cast<long>(std::floor((d1 - d2) * SecsPerDay + 0.5));
194 return static_cast<int>(std::floor(date - startOfYear)) + 1;
const double OneSecond
Definition gage.c:31
void decodeDate(DateTime date, int &year, int &month, int &day)
Decode DateTime to year-month-day.
Definition DateTime.hpp:97
DateTime encodeTime(int hour, int minute, int second)
Encode hour:minute:second to fractional day.
Definition DateTime.hpp:85
DateTime addSeconds(DateTime date, double seconds)
Add seconds to a DateTime — numerically identical to legacy.
Definition DateTime.hpp:154
int monthOfYear(DateTime date)
Get month of year (1..12) from DateTime.
Definition DateTime.hpp:181
bool isLeapYear(int year)
Check if year is a leap year.
Definition DateTime.hpp:59
void divMod(int n, int d, int *result, int *remainder)
Integer divmod — matching legacy divMod().
Definition DateTime.hpp:51
DateTime encodeDate(int year, int month, int day)
Encode year-month-day to DateTime.
Definition DateTime.hpp:67
double DateTime
Definition DateTime.hpp:42
long timeDiff(DateTime date1, DateTime date2)
Compute difference in seconds between two DateTimes.
Definition DateTime.hpp:165
void decodeTime(DateTime time, int &h, int &m, int &s)
Decode DateTime to hour:minute:second.
Definition DateTime.hpp:136
int dayOfYear(DateTime date)
Get day of year (1..365/366) from DateTime.
Definition DateTime.hpp:190
Definition Controls.cpp:24
double * y
Definition odesolve.c:28