remove_chrono #3
3 changed files with 43 additions and 15 deletions
|
@ -1,7 +1,6 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
@ -83,11 +82,12 @@ int main() {
|
||||||
|
|
||||||
rtc_get_datetime(&dt);
|
rtc_get_datetime(&dt);
|
||||||
|
|
||||||
chrono::year_month_day tomorrowYMD(chrono::year{dt.year},
|
datetime_t tomorrowYMD = dt;
|
||||||
chrono::month{static_cast<unsigned>(dt.month)},
|
add_one_day(tomorrowYMD);
|
||||||
chrono::day{static_cast<unsigned>(dt.day + 1)});
|
|
||||||
auto it = std::find_if(dates.begin(), dates.end(), [&tomorrowYMD](const WasteDate &date) {
|
auto it = std::find_if(dates.begin(), dates.end(), [&tomorrowYMD](const WasteDate &x) {
|
||||||
return date.date == tomorrowYMD;
|
return tomorrowYMD.year == x.date.year && tomorrowYMD.month == x.date.month &&
|
||||||
|
tomorrowYMD.day == x.date.day;
|
||||||
});
|
});
|
||||||
|
|
||||||
int8_t hour = isDST(dt) ? dt.hour + 1 : dt.hour;
|
int8_t hour = isDST(dt) ? dt.hour + 1 : dt.hour;
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include "pico/sleep.h"
|
|
||||||
#include "pico/stdlib.h"
|
|
||||||
#include "hardware/clocks.h"
|
#include "hardware/clocks.h"
|
||||||
#include "hardware/rosc.h"
|
#include "hardware/rosc.h"
|
||||||
#include "hardware/structs/scb.h"
|
#include "hardware/structs/scb.h"
|
||||||
|
#include "pico/sleep.h"
|
||||||
|
#include "pico/stdlib.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -35,17 +37,42 @@ bool isDST(const datetime_t &dt) {
|
||||||
return previousSunday < 25;
|
return previousSunday < 25;
|
||||||
}
|
}
|
||||||
|
|
||||||
chrono::year_month_day stringToDate(const std::string &dateStr) {
|
int8_t calcDotw(int year, int month, int day) {
|
||||||
|
int mon;
|
||||||
|
if (month > 2)
|
||||||
|
mon = month; // for march to december month code is same as month
|
||||||
|
else {
|
||||||
|
mon = (12 + month); // for Jan and Feb, month code will be 13 and 14
|
||||||
|
year--; // decrease year for month Jan and Feb
|
||||||
|
}
|
||||||
|
int y = year % 100; // last two digit
|
||||||
|
int c = year / 100; // first two digit
|
||||||
|
int w = (day + floor((13 * (mon + 1)) / 5) + y + floor(y / 4) + floor(c / 4) - (2 * c));
|
||||||
|
w = (w % 7) - 1;
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
datetime_t stringToDate(const std::string &dateStr) {
|
||||||
istringstream partss(dateStr);
|
istringstream partss(dateStr);
|
||||||
string day_str, month_str, year_str;
|
string day_str, month_str, year_str;
|
||||||
|
|
||||||
getline(partss, day_str, '.');
|
getline(partss, day_str, '.');
|
||||||
getline(partss, month_str, '.');
|
getline(partss, month_str, '.');
|
||||||
getline(partss, year_str, '.');
|
getline(partss, year_str, '.');
|
||||||
|
|
||||||
int day = atoi(day_str.c_str());
|
int day = atoi(day_str.c_str());
|
||||||
int month = atoi(month_str.c_str());
|
int month = atoi(month_str.c_str());
|
||||||
int year = atoi(year_str.c_str());
|
int year = atoi(year_str.c_str());
|
||||||
chrono::year_month_day date{chrono::year{year}, chrono::month{(uint)month},
|
|
||||||
chrono::day{(uint)day}};
|
datetime_t date;
|
||||||
|
date.year = year;
|
||||||
|
date.month = month;
|
||||||
|
date.day = day;
|
||||||
|
date.dotw = calcDotw(year, month, day);
|
||||||
|
date.hour = 0;
|
||||||
|
date.min = 0;
|
||||||
|
date.sec = 0;
|
||||||
|
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,8 +109,10 @@ std::vector<WasteDate> parseCsv(const std::string &csv) {
|
||||||
|
|
||||||
// Take existing date or create a new one.
|
// Take existing date or create a new one.
|
||||||
std::vector<WasteDate>::iterator it;
|
std::vector<WasteDate>::iterator it;
|
||||||
it = std::find_if(wasteDates.begin(), wasteDates.end(),
|
it = std::find_if(wasteDates.begin(), wasteDates.end(), [&date](const WasteDate &x) {
|
||||||
[&date](const WasteDate &x) { return date == x.date; });
|
return date.year == x.date.year && date.month == x.date.month &&
|
||||||
|
date.day == x.date.day;
|
||||||
|
});
|
||||||
if (it == wasteDates.end()) {
|
if (it == wasteDates.end()) {
|
||||||
WasteDate wd;
|
WasteDate wd;
|
||||||
wd.date = date;
|
wd.date = date;
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -15,7 +14,7 @@ enum class Waste {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WasteDate {
|
struct WasteDate {
|
||||||
std::chrono::year_month_day date;
|
datetime_t date;
|
||||||
std::vector<Waste> wasteTypes;
|
std::vector<Waste> wasteTypes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue