utc_clock now in milliseconds precision
This commit is contained in:
@@ -108,10 +108,12 @@ local_clock::time_point utcToLocal(utc_clock::time_point ts)
|
|||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
DateTime toDateTime(seconds32 ts)
|
DateTime toDateTime(std::chrono::milliseconds ts)
|
||||||
{
|
{
|
||||||
auto _time = ts.count();
|
auto _time = ts.count();
|
||||||
|
|
||||||
|
uint8_t millisecond(_time % 1000);
|
||||||
|
_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
|
||||||
uint8_t minute(_time % 60);
|
uint8_t minute(_time % 60);
|
||||||
@@ -155,6 +157,7 @@ DateTime toDateTime(seconds32 ts)
|
|||||||
dateTime.hour = hour;
|
dateTime.hour = hour;
|
||||||
dateTime.minute = minute;
|
dateTime.minute = minute;
|
||||||
dateTime.second = second;
|
dateTime.second = second;
|
||||||
|
dateTime.millisecond = millisecond;
|
||||||
dateTime.dayOfWeek = dayOfWeek;
|
dateTime.dayOfWeek = dayOfWeek;
|
||||||
return dateTime;
|
return dateTime;
|
||||||
}
|
}
|
||||||
@@ -178,6 +181,7 @@ std::optional<DateTime> parseDateTime(std::string_view str)
|
|||||||
// both valid:
|
// both valid:
|
||||||
// 2020-11-10T21:31
|
// 2020-11-10T21:31
|
||||||
// 2020-11-10T21:31:10
|
// 2020-11-10T21:31:10
|
||||||
|
// 2020-11-10T21:31:10.001
|
||||||
|
|
||||||
int year;
|
int year;
|
||||||
unsigned month;
|
unsigned month;
|
||||||
@@ -185,20 +189,22 @@ std::optional<DateTime> parseDateTime(std::string_view str)
|
|||||||
uint8_t hour;
|
uint8_t hour;
|
||||||
uint8_t minute;
|
uint8_t minute;
|
||||||
uint8_t second{};
|
uint8_t second{};
|
||||||
|
uint16_t millisecond{};
|
||||||
|
|
||||||
constexpr auto dateTimeFormat = "%4d-%2u-%2uT%2hhu:%2hhu:%2hhu";
|
constexpr auto dateTimeFormat = "%4d-%2u-%2uT%2hhu:%2hhu:%2hhu.%3hu";
|
||||||
if (const auto scanned = std::sscanf(str.data(), dateTimeFormat, &year, &month, &day, &hour, &minute, &second); scanned < 5)
|
if (const auto scanned = std::sscanf(str.data(), dateTimeFormat, &year, &month, &day, &hour, &minute, &second, &millisecond); scanned < 5)
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
|
|
||||||
return DateTime{
|
return DateTime{
|
||||||
.date=date::year_month_day{date::year{year}, date::month{month}, date::day{day}},
|
.date=date::year_month_day{date::year{year}, date::month{month}, date::day{day}},
|
||||||
.hour=hour,
|
.hour=hour,
|
||||||
.minute=minute,
|
.minute=minute,
|
||||||
.second=second
|
.second=second,
|
||||||
|
.millisecond=millisecond
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<seconds32> parseDaypoint(std::string_view str)
|
std::optional<std::chrono::seconds> parseDaypoint(std::string_view str)
|
||||||
{
|
{
|
||||||
int8_t hour, minute, second{};
|
int8_t hour, minute, second{};
|
||||||
|
|
||||||
@@ -209,35 +215,35 @@ std::optional<seconds32> parseDaypoint(std::string_view str)
|
|||||||
if (hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59)
|
if (hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59)
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
|
|
||||||
return hours32{hour} + minutes32{minute} + seconds32{second};
|
return std::chrono::hours{hour} + std::chrono::minutes{minute} + std::chrono::seconds{second};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string toString(const DateTime &dateTime)
|
std::string toString(const DateTime &dateTime)
|
||||||
{
|
{
|
||||||
char buf[27];
|
char buf[31];
|
||||||
|
|
||||||
std::sprintf(buf, "%04i-%02u-%02uT%02hhu:%02hhu:%02hhu",
|
std::sprintf(buf, "%04i-%02u-%02uT%02hhu:%02hhu:%02hhu.%03hu",
|
||||||
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.hour, dateTime.minute, dateTime.second, dateTime.millisecond);
|
||||||
|
|
||||||
return std::string{buf};
|
return std::string{buf};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string toString(const LocalDateTime &dateTime)
|
std::string toString(const LocalDateTime &dateTime)
|
||||||
{
|
{
|
||||||
char buf[34];
|
char buf[35];
|
||||||
|
|
||||||
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}};
|
||||||
|
|
||||||
std::sprintf(buf, "%04i-%02u-%02uT%02hhu:%02hhu:%02hhu %s%02hhu:%02hhu",
|
std::sprintf(buf, "%04i-%02u-%02uT%02hhu:%02hhu:%02hhu.%03hu %s%02hhu:%02hhu",
|
||||||
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.hour, dateTime.minute, dateTime.second, dateTime.millisecond,
|
||||||
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()));
|
||||||
|
|
||||||
return std::string{buf};
|
return std::string{buf};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string toDaypointString(seconds32 seconds)
|
std::string toDaypointString(std::chrono::seconds seconds)
|
||||||
{
|
{
|
||||||
date::hh_mm_ss helper(seconds);
|
date::hh_mm_ss helper(seconds);
|
||||||
char buf[10];
|
char buf[10];
|
||||||
|
@@ -24,7 +24,7 @@ using hours32 = std::chrono::duration<int32_t, std::ratio<3600>>;
|
|||||||
|
|
||||||
struct utc_clock
|
struct utc_clock
|
||||||
{
|
{
|
||||||
typedef seconds32 duration;
|
typedef std::chrono::milliseconds 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;
|
||||||
@@ -100,7 +100,7 @@ public:
|
|||||||
|
|
||||||
struct local_clock
|
struct local_clock
|
||||||
{
|
{
|
||||||
typedef seconds32 duration;
|
typedef std::chrono::milliseconds 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,6 +134,7 @@ struct DateTime
|
|||||||
uint8_t hour{};
|
uint8_t hour{};
|
||||||
uint8_t minute{};
|
uint8_t minute{};
|
||||||
uint8_t second{};
|
uint8_t second{};
|
||||||
|
uint16_t millisecond{};
|
||||||
|
|
||||||
enum DayOfWeek { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
|
enum DayOfWeek { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
|
||||||
DayOfWeek dayOfWeek{};
|
DayOfWeek dayOfWeek{};
|
||||||
@@ -144,6 +145,7 @@ struct DateTime
|
|||||||
hour == other.hour &&
|
hour == other.hour &&
|
||||||
minute == other.minute &&
|
minute == other.minute &&
|
||||||
second == other.second &&
|
second == other.second &&
|
||||||
|
millisecond == other.millisecond &&
|
||||||
dayOfWeek == other.dayOfWeek;
|
dayOfWeek == other.dayOfWeek;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -173,13 +175,13 @@ LocalDateTime toDateTime(local_clock::time_point ts);
|
|||||||
std::optional<DateTime> parseDateTime(std::string_view str);
|
std::optional<DateTime> parseDateTime(std::string_view str);
|
||||||
|
|
||||||
//! Returns null if string cannot be parsed
|
//! Returns null if string cannot be parsed
|
||||||
std::optional<seconds32> parseDaypoint(std::string_view str);
|
std::optional<std::chrono::seconds> parseDaypoint(std::string_view str);
|
||||||
|
|
||||||
std::string toString(const DateTime &dateTime);
|
std::string toString(const DateTime &dateTime);
|
||||||
|
|
||||||
std::string toString(const LocalDateTime &dateTime);
|
std::string toString(const LocalDateTime &dateTime);
|
||||||
|
|
||||||
std::string toDaypointString(seconds32 seconds);
|
std::string toDaypointString(std::chrono::seconds seconds);
|
||||||
|
|
||||||
std::chrono::milliseconds ago(millis_clock::time_point a);
|
std::chrono::milliseconds ago(millis_clock::time_point a);
|
||||||
|
|
||||||
|
@@ -15,8 +15,9 @@ auto espchrono::utc_clock::now() noexcept -> time_point
|
|||||||
{
|
{
|
||||||
timeval tv;
|
timeval tv;
|
||||||
gettimeofday(&tv, NULL);
|
gettimeofday(&tv, NULL);
|
||||||
seconds32 seconds{tv.tv_sec};
|
std::chrono::seconds seconds{tv.tv_sec};
|
||||||
return time_point{seconds};
|
std::chrono::microseconds microseconds{tv.tv_usec};
|
||||||
|
return time_point{std::chrono::floor<duration>(seconds+microseconds)};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto espchrono::millis_clock::now() noexcept -> time_point
|
auto espchrono::millis_clock::now() noexcept -> time_point
|
||||||
|
Reference in New Issue
Block a user