31#ifndef OPENSWMM_ENGINE_DATETIME_HPP
32#define OPENSWMM_ENGINE_DATETIME_HPP
43static constexpr int DateDelta = 693594;
44static constexpr double SecsPerDay = 86400.0;
45static constexpr double OneSecond = 1.1574074e-5;
60inline void divMod(
int n,
int d,
int* result,
int* remainder) {
61 if (d == 0) { *result = 0; *remainder = 0; }
62 else { *result = n / d; *remainder = n - d * (*result); }
69 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
77 static constexpr int DaysPerMonth[2][12] = {
78 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
79 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
81 if (year >= 1 && year <= 9999 && month >= 1 && month <= 12
82 && day >= 1 && day <= DaysPerMonth[i][month - 1]) {
83 for (
int j = 0; j < month - 1; ++j) day += DaysPerMonth[i][j];
85 return y * 365 +
y / 4 -
y / 100 +
y / 400 + day - DateDelta;
95 if (hour >= 0 && minute >= 0 && second >= 0) {
96 int s = hour * 3600 + minute * 60 + second;
97 return static_cast<double>(s) / SecsPerDay;
107 static constexpr int DaysPerMonth[2][12] = {
108 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
109 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
110 constexpr int D1 = 365, D4 = 1461, D100 = 36524, D400 = 146097;
112 int t =
static_cast<int>(std::floor(date)) + DateDelta;
113 if (t <= 0) { year = 0; month = 1; day = 1;
return; }
117 while (t >= D400) { t -= D400;
y += 400; }
119 if (i == 4) { i--; d += D100; }
124 if (i == 4) { i--; d += D1; }
130 i = DaysPerMonth[k][m - 1];
135 year =
y; month = m; day = d + 1;
146 double fracDay = (time - std::floor(time)) * SecsPerDay;
147 int secs =
static_cast<int>(std::floor(fracDay + 0.5));
148 if (secs >= 86400) secs = 86399;
164 double d = std::floor(date);
167 return d + (3600.0 * h + 60.0 * m + s + seconds) / SecsPerDay;
175 double d1 = std::floor(date1);
176 double d2 = std::floor(date2);
179 long s1 = 3600L * h + 60L * m + s;
181 long s2 = 3600L * h + 60L * m + s;
182 long secs =
static_cast<long>(std::floor((d1 - d2) * SecsPerDay + 0.5));
203 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:106
DateTime encodeTime(int hour, int minute, int second)
Encode hour:minute:second to fractional day.
Definition DateTime.hpp:94
DateTime addSeconds(DateTime date, double seconds)
Add seconds to a DateTime — numerically identical to legacy.
Definition DateTime.hpp:163
int monthOfYear(DateTime date)
Get month of year (1..12) from DateTime.
Definition DateTime.hpp:190
bool isLeapYear(int year)
Check if year is a leap year.
Definition DateTime.hpp:68
void divMod(int n, int d, int *result, int *remainder)
Integer divmod — matching legacy divMod().
Definition DateTime.hpp:60
DateTime encodeDate(int year, int month, int day)
Encode year-month-day to DateTime.
Definition DateTime.hpp:76
double DateTime
Definition DateTime.hpp:51
long timeDiff(DateTime date1, DateTime date2)
Compute difference in seconds between two DateTimes.
Definition DateTime.hpp:174
void decodeTime(DateTime time, int &h, int &m, int &s)
Decode DateTime to hour:minute:second.
Definition DateTime.hpp:145
int dayOfYear(DateTime date)
Get day of year (1..365/366) from DateTime.
Definition DateTime.hpp:199
Definition NodeCoupling.cpp:15
double * y
Definition odesolve.c:28