From c576c3905e242079eefea2955524d56bd803c8ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bo=C3=ABs?= Date: Mon, 7 Oct 2024 17:39:33 +0200 Subject: [PATCH] Improved date parameter in URL --- CHANGELOG.md | 2 +- src/WettergeraetDesktop/Frame.cpp | 49 ++++++++++++++++--------------- src/WettergeraetDesktop/Frame.h | 2 +- src/WettergeraetLib/FetchUrl.cpp | 5 ++++ 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28c4229..36b5367 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - 🎁 Keeping time intact while importing from `main.mcf` or METAR info - 🎁 Added ICAO flight settings - +- 🎁 Improved date parameter in URL ## 1.7.1 diff --git a/src/WettergeraetDesktop/Frame.cpp b/src/WettergeraetDesktop/Frame.cpp index 47436d7..1403413 100644 --- a/src/WettergeraetDesktop/Frame.cpp +++ b/src/WettergeraetDesktop/Frame.cpp @@ -282,26 +282,29 @@ wxString Frame::getIcaoFromInput() // -------------------------------------------------------------------------------------- -void Frame::fromObjectToInput() +void Frame::fromObjectToInput(bool ignoreDate = false) { this->icaoInput->SetValue(wxString(this->aerofly.nearestAirport)); - // Discard date from Aerofly date, instead set it to today... - this->utcDateValue = wxDateTime::Now().MakeUTC(); + if (!ignoreDate) { + // Discard date from Aerofly date, instead set it to today... + this->utcDateValue = wxDateTime::Now().MakeUTC(); - // ...but keep the time - auto hour = floor(this->aerofly.hours); - this->utcDateValue.SetHour(hour); - this->utcDateValue.SetMinute(round((this->aerofly.hours - hour) * 60)); - this->utcDateValue.SetSecond(0); + // ...but keep the time + auto hour = floor(this->aerofly.hours); + this->utcDateValue.SetHour(hour); + this->utcDateValue.SetMinute(round((this->aerofly.hours - hour) * 60)); + this->utcDateValue.SetSecond(0); - if (this->utcDateValue.IsLaterThan(wxDateTime::Now().MakeUTC())) { - // Substract one day if current datetime is in future - this->utcDateValue = this->utcDateValue.Subtract(wxDateSpan::Day()); + if (this->utcDateValue.IsLaterThan(wxDateTime::Now().MakeUTC())) { + // Substract one day if current datetime is in future + this->utcDateValue = this->utcDateValue.Subtract(wxDateSpan::Day()); + } + + this->utcTimeInput->SetValue(this->utcDateValue); + this->utcDateInput->SetValue(this->utcDateValue); } - this->utcTimeInput->SetValue(this->utcDateValue); - this->utcDateInput->SetValue(this->utcDateValue); this->windDirectionInput->SetValue(this->aerofly.windDirection); this->windStrengthInput->SetValue(ceil(this->aerofly.getWindKts())); @@ -446,19 +449,17 @@ void Frame::actionFetch(wxCommandEvent& WXUNUSED(event)) return; } - auto date = this->utcDateInput->GetValue(); - auto year = date.GetYear(); - auto month = date.GetMonth() + 1; - auto day = date.GetDay(); - - auto time = this->utcTimeInput->GetValue(); - auto hour = time.GetHour(); - auto minute = time.GetMinute(); + auto date = this->utcDateInput->GetValue().FormatISODate().mb_str().data(); + auto time = this->utcTimeInput->GetValue().FormatISOTime().mb_str().data(); - // yyyymmdd_hhmmZ // yyyy-mm-ddThh:mm:ssZ char searchDate[25]; - sprintf(searchDate, "%04d%02d%02d_%02d%02dZ", year, month, day, hour, minute); + sprintf( + searchDate, + "%sT%sZ", + date, + time + ); this->metarInput->SetValue("Loading..."); FetchUrl urlFetcher; @@ -482,7 +483,7 @@ void Frame::actionParse(wxCommandEvent& WXUNUSED(event)) } try { this->aerofly.setFromMetarString(metarInput); - this->fromObjectToInput(); + this->fromObjectToInput(true); } catch (std::invalid_argument& e) { wxLogError(e.what()); diff --git a/src/WettergeraetDesktop/Frame.h b/src/WettergeraetDesktop/Frame.h index 73c4ad5..60c389f 100644 --- a/src/WettergeraetDesktop/Frame.h +++ b/src/WettergeraetDesktop/Frame.h @@ -70,7 +70,7 @@ class Frame : public wxFrame virtual wxString getIcaoFromInput(); // Copy state of objects to form - virtual void fromObjectToInput(); + virtual void fromObjectToInput(bool ignoreDate); // Copy state of form to objects virtual void fromInputToObject(); diff --git a/src/WettergeraetLib/FetchUrl.cpp b/src/WettergeraetLib/FetchUrl.cpp index 387a85d..a0ba29e 100644 --- a/src/WettergeraetLib/FetchUrl.cpp +++ b/src/WettergeraetLib/FetchUrl.cpp @@ -129,6 +129,11 @@ std::string FetchUrl::fetch(std::string url, std::string icaoCode, std::string d std::regex("[^a-zA-z0-9]"), "" ); + date = std::regex_replace( + date, + std::regex(":"), + std::string("%3A") + ); if (lowercase) { transform(icaoCode.begin(), icaoCode.end(), icaoCode.begin(), ::tolower); }