Adding robust date calculation to WMO header

This commit is contained in:
Dan Paulat 2025-02-17 23:39:52 -06:00
parent c00016cb69
commit 7e9895e002
3 changed files with 178 additions and 72 deletions

View file

@ -119,71 +119,11 @@ std::chrono::system_clock::time_point Segment::event_begin() const
// If event begin is 000000T0000Z
if (eventBegin == std::chrono::system_clock::time_point {})
{
using namespace std::chrono;
// Determine event end from P-VTEC string
system_clock::time_point eventEnd =
std::chrono::system_clock::time_point eventEnd =
header_->vtecString_[0].pVtec_.event_end();
auto endDays = floor<days>(eventEnd);
year_month_day endDate {endDays};
// Determine WMO date/time
std::string wmoDateTime = wmoHeader_->date_time();
bool wmoDateTimeValid = false;
unsigned int dayOfMonth = 0;
unsigned long beginHour = 0;
unsigned long beginMinute = 0;
try
{
// WMO date time is in the format DDHHMM
dayOfMonth =
static_cast<unsigned int>(std::stoul(wmoDateTime.substr(0, 2)));
beginHour = std::stoul(wmoDateTime.substr(2, 2));
beginMinute = std::stoul(wmoDateTime.substr(4, 2));
wmoDateTimeValid = true;
}
catch (const std::exception&)
{
logger_->warn("Malformed WMO date/time: {}", wmoDateTime);
}
if (wmoDateTimeValid)
{
// Combine end date year and month with WMO date time
eventBegin =
sys_days {endDate.year() / endDate.month() / day {dayOfMonth}} +
hours {beginHour} + minutes {beginMinute};
// If the begin date is after the end date, assume the start time
// was the previous month (give a 1 day grace period for expiring
// events in the past)
if (eventBegin > eventEnd + 24h)
{
// If the current end month is January
if (endDate.month() == January)
{
// The begin month must be December of last year
eventBegin =
sys_days {
year {static_cast<int>((endDate.year() - 1y).count())} /
December / day {dayOfMonth}} +
hours {beginHour} + minutes {beginMinute};
}
else
{
// Back up one month
eventBegin =
sys_days {endDate.year() /
month {static_cast<unsigned int>(
(endDate.month() - month {1}).count())} /
day {dayOfMonth}} +
hours {beginHour} + minutes {beginMinute};
}
}
}
eventBegin = wmoHeader_->GetDateTime(eventEnd);
}
}