OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
DateTime.hpp
Go to the documentation of this file.
1
22#ifndef OPENSWMM_ENGINE_DATETIME_HPP
23#define OPENSWMM_ENGINE_DATETIME_HPP
24
25#include <cmath>
26
27namespace openswmm {
28namespace datetime {
29
30// ============================================================================
31// Constants — matching legacy datetime.c exactly
32// ============================================================================
33
34static constexpr int DateDelta = 693594;
35static constexpr double SecsPerDay = 86400.0;
36static constexpr double OneSecond = 1.1574074e-5;
37
38// ============================================================================
39// Date/time type — same as legacy DateTime (double)
40// ============================================================================
41
42using DateTime = double;
43
44// ============================================================================
45// Core functions — numerically identical to legacy datetime.c
46// ============================================================================
47
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); }
54}
55
59inline bool isLeapYear(int year) {
60 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
61}
62
67inline DateTime encodeDate(int year, int month, int day) {
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}};
71 int i = isLeapYear(year) ? 1 : 0;
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];
75 int y = year - 1;
76 return y * 365 + y / 4 - y / 100 + y / 400 + day - DateDelta;
77 }
78 return -DateDelta;
79}
80
85inline DateTime encodeTime(int hour, int minute, int second) {
86 if (hour >= 0 && minute >= 0 && second >= 0) {
87 int s = hour * 3600 + minute * 60 + second;
88 return static_cast<double>(s) / SecsPerDay;
89 }
90 return 0.0;
91}
92
97inline void decodeDate(DateTime date, int& year, int& month, int& day) {
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;
102
103 int t = static_cast<int>(std::floor(date)) + DateDelta;
104 if (t <= 0) { year = 0; month = 1; day = 1; return; }
105
106 t--;
107 int y = 1, i, d;
108 while (t >= D400) { t -= D400; y += 400; }
109 divMod(t, D100, &i, &d);
110 if (i == 4) { i--; d += D100; }
111 y += i * 100;
112 divMod(d, D4, &i, &d);
113 y += i * 4;
114 divMod(d, D1, &i, &d);
115 if (i == 4) { i--; d += D1; }
116 y += i;
117
118 int k = isLeapYear(y) ? 1 : 0;
119 int m = 1;
120 for (;;) {
121 i = DaysPerMonth[k][m - 1];
122 if (d < i) break;
123 d -= i;
124 m++;
125 }
126 year = y; month = m; day = d + 1;
127}
128
136inline void decodeTime(DateTime time, int& h, int& m, int& s) {
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;
140 divMod(secs, 60, &m, &s); // mins, secs
141 int mins = m;
142 divMod(mins, 60, &h, &m); // hours, mins
143 if (h > 23) h = 0;
144}
145
154inline DateTime addSeconds(DateTime date, double seconds) {
155 double d = std::floor(date);
156 int h, m, s;
157 decodeTime(date, h, m, s);
158 return d + (3600.0 * h + 60.0 * m + s + seconds) / SecsPerDay;
159}
160
165inline long timeDiff(DateTime date1, DateTime date2) {
166 double d1 = std::floor(date1);
167 double d2 = std::floor(date2);
168 int h, m, s;
169 decodeTime(date1, h, m, s);
170 long s1 = 3600L * h + 60L * m + s;
171 decodeTime(date2, h, m, s);
172 long s2 = 3600L * h + 60L * m + s;
173 long secs = static_cast<long>(std::floor((d1 - d2) * SecsPerDay + 0.5));
174 secs += (s1 - s2);
175 return secs;
176}
177
181inline int monthOfYear(DateTime date) {
182 int y, m, d;
183 decodeDate(date, y, m, d);
184 return m;
185}
186
190inline int dayOfYear(DateTime date) {
191 int y, m, d;
192 decodeDate(date, y, m, d);
193 DateTime startOfYear = encodeDate(y, 1, 1);
194 return static_cast<int>(std::floor(date - startOfYear)) + 1;
195}
196
197} // namespace datetime
198} // namespace openswmm
199
200#endif // OPENSWMM_ENGINE_DATETIME_HPP
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