From b9925f923f344b41e057ac23df05a1738d34dfe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bo=C3=ABs?= Date: Mon, 13 May 2019 18:56:21 +0200 Subject: [PATCH] Interpret date `YYYY/MM/DD` prepended to METAR string --- CHANGELOG.md | 1 + src/MetarParserSimple/MetarParserSimple.cpp | 52 +++++++++++++++++---- src/MetarParserSimple/MetarParserSimple.h | 8 +++- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 931dcf5..0ba4ece 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Change log ========== * 🎁 Add sample ICAO airport codes to combobox +* 🎁 Interpret date `YYYY/MM/DD` prepended to METAR string 1.2.2 ----- diff --git a/src/MetarParserSimple/MetarParserSimple.cpp b/src/MetarParserSimple/MetarParserSimple.cpp index 6cc368f..3341c49 100644 --- a/src/MetarParserSimple/MetarParserSimple.cpp +++ b/src/MetarParserSimple/MetarParserSimple.cpp @@ -6,6 +6,25 @@ #include #include +void MetarParserSimple::initializeDate() +{ + if (this->observed.year != 1970) { + return; + } + + time_t rawtime; + struct tm ptm; + + time(&rawtime); + + gmtime_s(&ptm, &rawtime); + this->observed.year = ptm.tm_year + 1900; + this->observed.month = ptm.tm_mon + 1; + this->observed.day = ptm.tm_mday; + this->observed.hours = ptm.tm_hour; + this->observed.minutes = ptm.tm_min; +} + void MetarParserSimple::fixTimeDate() { // This is a naive approach, but it does the job @@ -124,6 +143,14 @@ bool MetarParserSimple::convert(std::string metarString) strcpy_s(this->icao, 8, metarPart.c_str()); parsingMode = 1; } + // Non-standard date prepended, parsing it anyway + else if (std::regex_match(metarPart, match, std::regex("([1-2]\\d{3})\\D([0-1]\\d)\\D([0-3]\\d)"))) { + this->setDate2( + std::stoi(match[1].str()), + std::stoi(match[2].str()), + std::stoi(match[3].str()) + ); + } break; case 1: // Observed Date @@ -309,17 +336,24 @@ double MetarParserSimple::getHumidity() void MetarParserSimple::setDate(short day, short hours, short minutes) { - time_t rawtime; - struct tm ptm; + this->initializeDate(); - time(&rawtime); + // Day may be in preceeding month + if (day > this->observed.day) { + this->observed.month -= 1; + } + this->observed.day = day; + this->observed.hours = hours; + this->observed.minutes = minutes; + this->fixTimeDate(); +} - gmtime_s(&ptm, &rawtime); - this->observed.year = ptm.tm_year + 1900; - this->observed.month = ptm.tm_mon + 1; - this->observed.day = (day >= 0) ? day : ptm.tm_mday; - this->observed.hours = (hours >= 0) ? hours : ptm.tm_hour; - this->observed.minutes = (minutes >= 0) ? minutes : ptm.tm_min; +void MetarParserSimple::setDate2(int year, short month, short day) +{ + this->initializeDate(); + this->observed.year = year; + this->observed.month = month; + this->observed.day = day; this->fixTimeDate(); } diff --git a/src/MetarParserSimple/MetarParserSimple.h b/src/MetarParserSimple/MetarParserSimple.h index 4518551..74135e0 100644 --- a/src/MetarParserSimple/MetarParserSimple.h +++ b/src/MetarParserSimple/MetarParserSimple.h @@ -18,6 +18,9 @@ class METARPARSERSIMPLE_API MetarParserSimple { private: + // Set date to current date + void initializeDate(); + // Keep time and date in bounds void fixTimeDate(); @@ -113,9 +116,12 @@ class METARPARSERSIMPLE_API MetarParserSimple // Get humidity in percent 0..1 double getHumidity(); - // Set date / time to current UTC time, but allow for modification. + // Set date / time, but allow for modification. void setDate(short day = -1, short hours = -1, short minutes = -1); + // Set date, but allow for modification. + void setDate2(int year, short month, short day); + // Change parsed time by hoursOffset. void addHours(int hoursOffset);