SWMMVis  6.0.0-alpha.4
Qt6/C++ GIS-based graphical user interface for the SWMMVis engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
swmmdatetime.h
Go to the documentation of this file.
1
28#ifndef OPENSWMMVIS_CORE_SWMMDATETIME_H
29#define OPENSWMMVIS_CORE_SWMMDATETIME_H
30
32
33#include <openswmm/engine/openswmm_datetime.h>
34
35#include <QDate>
36#include <QDateTime>
37#include <QTime>
38
39#include <cmath>
40#include <limits>
41
43
49inline QDateTime swmmDateTimeToQDateTime(double value)
50{
51 if (!std::isfinite(value))
52 return {};
53
54 int y = 0, mo = 0, d = 0, h = 0, mi = 0, s = 0;
55 swmm_datetime_decode_date(value, &y, &mo, &d);
56 swmm_datetime_decode_time(value, &h, &mi, &s);
57
58 const QDate date(y, mo, d);
59 if (!date.isValid())
60 return {};
61 return QDateTime(date, QTime(h, mi, s), Qt::UTC);
62}
63
70inline double qDateTimeToSwmmDateTime(const QDateTime& dt)
71{
72 if (!dt.isValid())
73 return std::numeric_limits<double>::quiet_NaN();
74
75 const QDate d = dt.date();
76 const QTime t = dt.time();
77
78 double datePart = 0.0;
79 swmm_datetime_encode_date(d.year(), d.month(), d.day(), &datePart);
80
81 // Round to the nearest whole second (engine resolution). Clamp a rounded-up
82 // end-of-day back into [0, 86399] to match the engine's decodeTime clamp
83 // rather than spuriously rolling into the next calendar day.
84 const long secOfDay = std::lround(
85 (t.hour() * 3600 + t.minute() * 60 + t.second()) + t.msec() / 1000.0);
86 const int clamped = static_cast<int>(secOfDay >= 86400 ? 86399 : secOfDay);
87
88 double timePart = 0.0;
89 swmm_datetime_encode_time(clamped / 3600, (clamped % 3600) / 60, clamped % 60,
90 &timePart);
91 return datePart + timePart;
92}
93
94} // namespace openswmmvis::core
95
96#endif // OPENSWMMVIS_CORE_SWMMDATETIME_H
DiagramType t
Definition diagramspec.cpp:20
double s
Scene px per model length unit.
Definition inletdrawingview.cpp:82
int h
Definition mesh2dresultsexport.cpp:387
Definition swmmdatetime.h:42
double qDateTimeToSwmmDateTime(const QDateTime &dt)
QDateTime → SWMM DateTime double.
Definition swmmdatetime.h:70
QDateTime swmmDateTimeToQDateTime(double value)
SWMM DateTime double → UTC-labelled QDateTime (wall clock).
Definition swmmdatetime.h:49
The one date-time format string the GUI displays and edits SWMM times in.