Skip to content

Commit

Permalink
Interpret date YYYY/MM/DD prepended to METAR string
Browse files Browse the repository at this point in the history
  • Loading branch information
fboes committed May 13, 2019
1 parent f6005e7 commit b9925f9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
52 changes: 43 additions & 9 deletions src/MetarParserSimple/MetarParserSimple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@
#include <iterator>
#include <regex>

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down
8 changes: 7 additions & 1 deletion src/MetarParserSimple/MetarParserSimple.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
class METARPARSERSIMPLE_API MetarParserSimple
{
private:
// Set date to current date
void initializeDate();

// Keep time and date in bounds
void fixTimeDate();

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit b9925f9

Please sign in to comment.