Remove micros_clock and measure all other clocks in microseconds

This commit is contained in:
2022-01-19 18:20:05 +01:00
parent c273edf278
commit 9c57959705
3 changed files with 41 additions and 49 deletions

View File

@@ -37,12 +37,14 @@ bool daylightSavingTime(local_clock::time_point _timeStamp)
//int previousSunday = day - (dow-1); //int previousSunday = day - (dow-1);
// -------------------- March --------------------------------------- // -------------------- March ---------------------------------------
//In march, we are DST if our previous Sunday was = to or after the 8th. //In march, we are DST if our previous Sunday was = to or after the 8th.
if (_tempDateTime.date.month() == March) { // in march, if previous Sunday is after the 8th, is DST if (_tempDateTime.date.month() == March) // in march, if previous Sunday is after the 8th, is DST
{
// unless Sunday and hour < 2am // unless Sunday and hour < 2am
if (previousSunday >= 8) { // Sunday = 1 if (previousSunday >= 8) // Sunday = 1
{
// return true if day > 14 or (dow == 1 and hour >= 2) // return true if day > 14 or (dow == 1 and hour >= 2)
return ((_tempDateTime.date.day() > 14_d) || return (_tempDateTime.date.day() > 14_d) ||
((_tempDateTime.dayOfWeek == 1 && _tempDateTime.hour >= 2) || _tempDateTime.dayOfWeek > 1)); ((_tempDateTime.dayOfWeek == 1 && _tempDateTime.hour >= 2) || _tempDateTime.dayOfWeek > 1);
} // end if ( previousSunday >= 8 && _dateTime.dayofWeek > 0 ) } // end if ( previousSunday >= 8 && _dateTime.dayofWeek > 0 )
else else
{ {
@@ -60,13 +62,13 @@ bool daylightSavingTime(local_clock::time_point _timeStamp)
if (previousSunday < 1) if (previousSunday < 1)
{ {
// is not true for Sunday after 2am or any day after 1st Sunday any time // is not true for Sunday after 2am or any day after 1st Sunday any time
return ((_tempDateTime.dayOfWeek == 1 && _tempDateTime.hour < 2) || (_tempDateTime.dayOfWeek > 1)); return (_tempDateTime.dayOfWeek == 1 && _tempDateTime.hour < 2) || (_tempDateTime.dayOfWeek > 1);
//return true; //return true;
} // end if (previousSunday < 1) } // end if (previousSunday < 1)
else else
{ {
// return false unless after first wk and dow = Sunday and hour < 2 // return false unless after first wk and dow = Sunday and hour < 2
return (_tempDateTime.date.day() < 8_d && _tempDateTime.dayOfWeek == 1 && _tempDateTime.hour < 2); return _tempDateTime.date.day() < 8_d && _tempDateTime.dayOfWeek == 1 && _tempDateTime.hour < 2;
} // end else } // end else
} }
} // namespace } // namespace
@@ -115,11 +117,13 @@ local_clock::time_point utcToLocal(utc_clock::time_point ts)
#endif #endif
namespace { namespace {
DateTime toDateTime(std::chrono::milliseconds ts) DateTime toDateTime(std::chrono::microseconds ts)
{ {
auto _time = ts.count(); auto _time = ts.count();
uint8_t millisecond(_time % 1000); uint16_t microsecond(_time % 1000);
_time /= 1000; // now it is milliseconds
uint16_t millisecond(_time % 1000);
_time /= 1000; // now it is seconds _time /= 1000; // now it is seconds
uint8_t second(_time % 60); uint8_t second(_time % 60);
_time /= 60; // now it is minutes _time /= 60; // now it is minutes
@@ -131,9 +135,8 @@ DateTime toDateTime(std::chrono::milliseconds ts)
date::year year{1970}; date::year year{1970};
unsigned long _days = 0; unsigned long _days = 0;
while ((unsigned)(_days += (year.is_leap() ? 366 : 365)) <= _time) { while ((unsigned)(_days += (year.is_leap() ? 366 : 365)) <= _time)
year++; year++;
}
_days -= year.is_leap() ? 366 : 365; _days -= year.is_leap() ? 366 : 365;
_time -= _days; // now it is days in this year, starting at 0 _time -= _days; // now it is days in this year, starting at 0
@@ -141,23 +144,23 @@ DateTime toDateTime(std::chrono::milliseconds ts)
_days = 0; _days = 0;
uint8_t _monthLength = 0; uint8_t _monthLength = 0;
date::month month = January; date::month month = January;
for (; month <= December; month++) { for (; month <= December; month++)
if (month == February) { // february {
if (year.is_leap()) { if (month == February) // february
{
if (year.is_leap())
_monthLength = 29; _monthLength = 29;
} else { else
_monthLength = 28; _monthLength = 28;
} }
} else { else
_monthLength = _monthDays[unsigned(month)-1]; _monthLength = _monthDays[unsigned(month)-1];
}
if (_time >= _monthLength) { if (_time >= _monthLength)
_time -= _monthLength; _time -= _monthLength;
} else { else
break; break;
} }
}
date::day day(_time + 1); // day of month date::day day(_time + 1); // day of month
DateTime dateTime{year_month_day{year, month, day}}; DateTime dateTime{year_month_day{year, month, day}};
@@ -165,6 +168,7 @@ DateTime toDateTime(std::chrono::milliseconds ts)
dateTime.minute = minute; dateTime.minute = minute;
dateTime.second = second; dateTime.second = second;
dateTime.millisecond = millisecond; dateTime.millisecond = millisecond;
dateTime.microsecond = microsecond;
dateTime.dayOfWeek = dayOfWeek; dateTime.dayOfWeek = dayOfWeek;
return dateTime; return dateTime;
} }
@@ -189,6 +193,7 @@ tl::expected<DateTime, std::string> parseDateTime(std::string_view str)
// 2020-11-10T21:31 // 2020-11-10T21:31
// 2020-11-10T21:31:10 // 2020-11-10T21:31:10
// 2020-11-10T21:31:10.001 // 2020-11-10T21:31:10.001
// 2020-11-10T21:31:10.001.002
int year; int year;
unsigned month; unsigned month;
@@ -197,9 +202,10 @@ tl::expected<DateTime, std::string> parseDateTime(std::string_view str)
uint8_t minute; uint8_t minute;
uint8_t second{}; uint8_t second{};
uint16_t millisecond{}; uint16_t millisecond{};
uint16_t microsecond{};
constexpr auto dateTimeFormat = "%4d-%2u-%2uT%2hhu:%2hhu:%2hhu.%3hu"; constexpr auto dateTimeFormat = "%4d-%2u-%2uT%2hhu:%2hhu:%2hhu.%3hu.%3hu";
if (const auto scanned = std::sscanf(str.data(), dateTimeFormat, &year, &month, &day, &hour, &minute, &second, &millisecond); scanned < 5) if (const auto scanned = std::sscanf(str.data(), dateTimeFormat, &year, &month, &day, &hour, &minute, &second, &millisecond, &microsecond); scanned < 5)
return tl::make_unexpected(fmt::format("invalid DateTime ({})", str)); return tl::make_unexpected(fmt::format("invalid DateTime ({})", str));
return DateTime{ return DateTime{
@@ -207,7 +213,8 @@ tl::expected<DateTime, std::string> parseDateTime(std::string_view str)
.hour=hour, .hour=hour,
.minute=minute, .minute=minute,
.second=second, .second=second,
.millisecond=millisecond .millisecond=millisecond,
.microsecond=microsecond
}; };
} }
@@ -227,18 +234,18 @@ tl::expected<std::chrono::seconds, std::string> parseDaypoint(std::string_view s
std::string toString(const DateTime &dateTime) std::string toString(const DateTime &dateTime)
{ {
return fmt::format("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}", return fmt::format("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}.{:03}",
int{dateTime.date.year()}, unsigned{dateTime.date.month()}, unsigned{dateTime.date.day()}, int{dateTime.date.year()}, unsigned{dateTime.date.month()}, unsigned{dateTime.date.day()},
dateTime.hour, dateTime.minute, dateTime.second, dateTime.millisecond); dateTime.hour, dateTime.minute, dateTime.second, dateTime.millisecond, dateTime.microsecond);
} }
std::string toString(const LocalDateTime &dateTime) std::string toString(const LocalDateTime &dateTime)
{ {
date::hh_mm_ss helper{dateTime.timezone.offset + hours32{dateTime.dst ? 1 : 0}}; date::hh_mm_ss helper{dateTime.timezone.offset + hours32{dateTime.dst ? 1 : 0}};
return fmt::format("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03} {}{:02}:{:02}", return fmt::format("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}.{:03} {}{:02}:{:02}",
int{dateTime.date.year()}, unsigned{dateTime.date.month()}, unsigned{dateTime.date.day()}, int{dateTime.date.year()}, unsigned{dateTime.date.month()}, unsigned{dateTime.date.day()},
dateTime.hour, dateTime.minute, dateTime.second, dateTime.millisecond, dateTime.hour, dateTime.minute, dateTime.second, dateTime.millisecond, dateTime.microsecond,
helper.is_negative() ? "-" : "+", uint8_t(helper.hours().count()), uint8_t(helper.minutes().count())); helper.is_negative() ? "-" : "+", uint8_t(helper.hours().count()), uint8_t(helper.minutes().count()));
} }
@@ -253,7 +260,7 @@ std::string toDaypointString(std::chrono::seconds seconds)
helper.seconds().count()); helper.seconds().count());
} }
std::chrono::milliseconds ago(millis_clock::time_point a) std::chrono::microseconds ago(millis_clock::time_point a)
{ {
return millis_clock::now() - a; return millis_clock::now() - a;
} }

View File

@@ -38,7 +38,7 @@ using hours32 = std::chrono::duration<int32_t, std::ratio<3600>>;
struct utc_clock struct utc_clock
{ {
typedef std::chrono::milliseconds duration; typedef std::chrono::microseconds duration;
typedef duration::rep rep; typedef duration::rep rep;
typedef duration::period period; typedef duration::period period;
typedef std::chrono::time_point<utc_clock, duration> time_point; typedef std::chrono::time_point<utc_clock, duration> time_point;
@@ -118,7 +118,7 @@ public:
struct local_clock struct local_clock
{ {
typedef std::chrono::milliseconds duration; typedef std::chrono::microseconds duration;
typedef duration::rep rep; typedef duration::rep rep;
typedef duration::period period; typedef duration::period period;
typedef local_time_point<local_clock, duration> time_point; typedef local_time_point<local_clock, duration> time_point;
@@ -134,23 +134,11 @@ struct local_clock
}; };
struct millis_clock struct millis_clock
{
typedef std::chrono::milliseconds duration;
typedef duration::rep rep;
typedef duration::period period;
typedef std::chrono::time_point<millis_clock, duration> time_point;
static constexpr bool is_steady = true;
static time_point now() noexcept;
};
struct micros_clock
{ {
typedef std::chrono::microseconds duration; typedef std::chrono::microseconds duration;
typedef duration::rep rep; typedef duration::rep rep;
typedef duration::period period; typedef duration::period period;
typedef std::chrono::time_point<micros_clock, duration> time_point; typedef std::chrono::time_point<millis_clock, duration> time_point;
static constexpr bool is_steady = true; static constexpr bool is_steady = true;
@@ -165,6 +153,7 @@ struct DateTime
uint8_t minute{}; uint8_t minute{};
uint8_t second{}; uint8_t second{};
uint16_t millisecond{}; uint16_t millisecond{};
uint16_t microsecond{};
enum DayOfWeek { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; enum DayOfWeek { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
DayOfWeek dayOfWeek{}; DayOfWeek dayOfWeek{};
@@ -176,6 +165,7 @@ struct DateTime
minute == other.minute && minute == other.minute &&
second == other.second && second == other.second &&
millisecond == other.millisecond && millisecond == other.millisecond &&
microsecond == other.microsecond &&
dayOfWeek == other.dayOfWeek; dayOfWeek == other.dayOfWeek;
} }
}; };
@@ -213,7 +203,7 @@ std::string toString(const LocalDateTime &dateTime);
std::string toDaypointString(std::chrono::seconds seconds); std::string toDaypointString(std::chrono::seconds seconds);
std::chrono::milliseconds ago(millis_clock::time_point a); std::chrono::microseconds ago(millis_clock::time_point a);
std::string toString(milliseconds32 val); std::string toString(milliseconds32 val);
std::string toString(seconds32 val); std::string toString(seconds32 val);

View File

@@ -21,11 +21,6 @@ auto espchrono::utc_clock::now() noexcept -> time_point
} }
auto espchrono::millis_clock::now() noexcept -> time_point auto espchrono::millis_clock::now() noexcept -> time_point
{
return time_point{std::chrono::floor<duration>(std::chrono::microseconds{esp_timer_get_time()})};
}
auto espchrono::micros_clock::now() noexcept -> time_point
{ {
return time_point{std::chrono::microseconds{esp_timer_get_time()}}; return time_point{std::chrono::microseconds{esp_timer_get_time()}};
} }