Cross-platform time utilities for RP2040 and RP2350.
More...
#include <PicoTime.h>
|
static time_t | now () |
| Get the current epoch time using platform RTC or AON.
|
|
static struct tm | nowTm () |
| Get the current local time as struct tm .
|
|
static std::string | getNowHhMmSs () |
| Get the current time as a datetime_t .
|
|
static struct tm | todayAt (const struct tm *hhmmss) |
| Construct today's date with a specific time-of-day.
|
|
static time_t | todayAtTimeT (const struct tm *hhmmss) |
| Convert a tm with today's date and time to epoch.
|
|
static std::string | todayHhMmSsString (const struct tm *hhmmss) |
| Convert a time-of-day to a string for today.
|
|
static void | printNow () |
| Print the current time to stdout.
|
|
static void | print (time_t t) |
| Print a given UNIX timestamp to stdout.
|
|
static void | print (const struct tm *t) |
| Print a struct tm to stdout.
|
|
static TimeOfDay | toTimeOfDay (uint32_t timestamp) |
| Print a datetime_t to stdout.
|
|
static DaysOfWeek | dayOfWeekBitmask (uint32_t timestamp) |
|
static Day | dayOfWeek (uint32_t timestamp) |
|
static std::string | formatIso8601 (time_t t) |
|
Definition at line 34 of file PicoTime.h.
◆ dayOfWeek()
static Day PicoTime::dayOfWeek |
( |
uint32_t |
timestamp | ) |
|
|
inlinestatic |
Definition at line 122 of file PicoTime.h.
122 {
123 uint32_t days = timestamp / 86400;
124 uint8_t weekday = (days + 4) % 7;
125 return static_cast<Day>(1u << weekday);
126 }
Day
Enum for days of the week as bitmask flags.
◆ dayOfWeekBitmask()
static DaysOfWeek PicoTime::dayOfWeekBitmask |
( |
uint32_t |
timestamp | ) |
|
|
inlinestatic |
Definition at line 116 of file PicoTime.h.
116 {
117 uint32_t days = timestamp / 86400;
118 uint8_t weekday = (days + 4) % 7;
119 return static_cast<DaysOfWeek>(1u << weekday);
120 }
uint8_t DaysOfWeek
Type alias for a set of days (bitmask).
◆ formatIso8601()
std::string PicoTime::formatIso8601 |
( |
time_t |
t | ) |
|
|
static |
Definition at line 131 of file PicoTime.cpp.
132{
133 struct tm tm;
134 localtime_r(&t, &tm);
135 char buf[32];
136 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &tm);
137 return std::string(buf);
138}
◆ getNowHhMmSs()
std::string PicoTime::getNowHhMmSs |
( |
| ) |
|
|
static |
- Returns
- Time in
datetime_t
format.
Get a string with the current time formatted as HH:MM:SS.
- Returns
- Formatted time string.
Get the current time as a datetime_t
.
- Returns
- Time in
datetime_t
format.
Get a string with the current time formatted as HH:MM:SS.
- Returns
- Formatted time string.
Definition at line 69 of file PicoTime.cpp.
70{
71 struct tm t =
nowTm();
72 char buf[9];
73 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", t.tm_hour, t.tm_min, t.tm_sec);
74 return std::string(buf);
75}
static struct tm nowTm()
Get the current local time as struct tm.
References nowTm().
◆ now()
◆ nowTm()
struct tm PicoTime::nowTm |
( |
| ) |
|
|
static |
Get the current local time as struct tm
.
- Returns
- Current time in
tm
format.
- Returns
- Current time in
tm
format.
Definition at line 50 of file PicoTime.cpp.
51{
53 struct tm out;
54 localtime_r(&t, &out);
55 return out;
56}
static time_t now()
Get the current epoch time using platform RTC or AON.
Referenced by getNowHhMmSs().
◆ print() [1/2]
void PicoTime::print |
( |
const struct tm * |
t | ) |
|
|
static |
Print a struct tm
to stdout.
- Parameters
-
- Parameters
-
Definition at line 124 of file PicoTime.cpp.
125{
126 char buf[32];
127 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", t);
128 printf("%s\n", buf);
129}
◆ print() [2/2]
void PicoTime::print |
( |
time_t |
t | ) |
|
|
static |
Print a given UNIX timestamp to stdout.
- Parameters
-
- Parameters
-
Definition at line 116 of file PicoTime.cpp.
117{
118 struct tm tm;
119 localtime_r(&t, &tm);
121}
static void print(time_t t)
Print a given UNIX timestamp to stdout.
References print().
Referenced by print(), and printNow().
◆ printNow()
void PicoTime::printNow |
( |
| ) |
|
|
static |
◆ todayAt()
struct tm PicoTime::todayAt |
( |
const struct tm * |
hhmmss | ) |
|
|
static |
Construct today's date with a specific time-of-day.
- Parameters
-
hhmmss | Pointer to a tm with hour/min/sec set. |
- Returns
- Combined
tm
with today's date and given time.
- Parameters
-
hhmmss | Pointer to a tm with hour/min/sec set. |
- Returns
- Combined
tm
with today's date and given time.
Definition at line 78 of file PicoTime.cpp.
79{
81 struct tm result = {
82 .tm_sec = hhmmss->tm_sec,
83 .tm_min = hhmmss->tm_min,
84 .tm_hour = hhmmss->tm_hour,
85 .tm_mday =
now.tm_mday,
87 .tm_year =
now.tm_year,
88 .tm_isdst = -1};
89 mktime(&result);
90 return result;
91}
Referenced by todayAtTimeT(), and todayHhMmSsString().
◆ todayAtTimeT()
time_t PicoTime::todayAtTimeT |
( |
const struct tm * |
hhmmss | ) |
|
|
static |
Convert a tm
with today's date and time to epoch.
- Parameters
-
hhmmss | Pointer to tm with time-of-day. |
- Returns
- Time as UNIX timestamp.
- Parameters
-
hhmmss | Pointer to tm with time-of-day. |
- Returns
- Time as UNIX timestamp.
Definition at line 94 of file PicoTime.cpp.
95{
97 return mktime(&t);
98}
static struct tm todayAt(const struct tm *hhmmss)
Construct today's date with a specific time-of-day.
References todayAt().
◆ todayHhMmSsString()
std::string PicoTime::todayHhMmSsString |
( |
const struct tm * |
hhmmss | ) |
|
|
static |
Convert a time-of-day to a string for today.
- Parameters
-
hhmmss | Pointer to tm with hour/min/sec. |
- Returns
- Formatted string including date and time.
- Parameters
-
hhmmss | Pointer to tm with hour/min/sec. |
- Returns
- Formatted string including date and time.
Definition at line 101 of file PicoTime.cpp.
102{
104 char buf[32];
105 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &t);
106 return std::string(buf);
107}
References todayAt().
◆ toTimeOfDay()
static TimeOfDay PicoTime::toTimeOfDay |
( |
uint32_t |
timestamp | ) |
|
|
inlinestatic |
- Parameters
-
dt | Pointer to datetime_t to print. |
Definition at line 109 of file PicoTime.h.
109 {
110 time_t t = static_cast<time_t>(timestamp);
111 struct tm tm;
112 localtime_r(&t, &tm);
113 return TimeOfDay{
static_cast<uint8_t
>(tm.tm_hour),
static_cast<uint8_t
>(tm.tm_min),
static_cast<uint8_t
>(tm.tm_sec) };
114 }
A simple value type representing a time of day (hour, minute, second).
The documentation for this class was generated from the following files: