Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
TimeOfDay.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <cstdio>
6
7#include "nlohmann/json.hpp"
22{
23 uint8_t hour = 0;
24 uint8_t minute = 0;
25 uint8_t second = 0;
26
32 static TimeOfDay fromString(const char *hhmm) {
33 TimeOfDay t;
34 if (sscanf(hhmm, "%2hhu:%2hhu:%2hhu", &t.hour, &t.minute, &t.second) == 3) {
35 return t;
36 } else if (sscanf(hhmm, "%2hhu:%2hhu", &t.hour, &t.minute) == 2) {
37 t.second = 0;
38 return t;
39 }
40 return {0, 0, 0};
41 }
42
48 static std::string toString(const TimeOfDay &time) {
49 char buf[9];
50 if (time.second > 0) {
51 snprintf(buf, sizeof(buf), "%02u:%02u:%02u", time.hour, time.minute, time.second);
52 } else {
53 snprintf(buf, sizeof(buf), "%02u:%02u", time.hour, time.minute);
54 }
55 return std::string(buf);
56 }
57
58 // Comparison operators
59
60 bool operator==(const TimeOfDay &other) const {
61 return hour == other.hour && minute == other.minute && second == other.second;
62 }
63
64 bool operator!=(const TimeOfDay &other) const {
65 return !(*this == other);
66 }
67
68 bool operator<(const TimeOfDay &other) const {
69 if (hour != other.hour) return hour < other.hour;
70 if (minute != other.minute) return minute < other.minute;
71 return second < other.second;
72 }
73
74 bool operator<=(const TimeOfDay &other) const {
75 return !(*this > other);
76 }
77
78 bool operator>(const TimeOfDay &other) const {
79 return other < *this;
80 }
81
82 bool operator>=(const TimeOfDay &other) const {
83 return !(*this < other);
84 }
85
86};
A simple value type representing a time of day (hour, minute, second).
Definition TimeOfDay.h:22
bool operator>=(const TimeOfDay &other) const
Definition TimeOfDay.h:82
uint8_t hour
Definition TimeOfDay.h:23
static TimeOfDay fromString(const char *hhmm)
Parse a string in the form "HH:MM" or "HH:MM:SS".
Definition TimeOfDay.h:32
uint8_t minute
Definition TimeOfDay.h:24
bool operator==(const TimeOfDay &other) const
Definition TimeOfDay.h:60
bool operator<=(const TimeOfDay &other) const
Definition TimeOfDay.h:74
bool operator!=(const TimeOfDay &other) const
Definition TimeOfDay.h:64
uint8_t second
Definition TimeOfDay.h:25
bool operator<(const TimeOfDay &other) const
Definition TimeOfDay.h:68
static std::string toString(const TimeOfDay &time)
Format the time as a string.
Definition TimeOfDay.h:48
bool operator>(const TimeOfDay &other) const
Definition TimeOfDay.h:78