Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
PicoTime Class Reference

Cross-platform time utilities for RP2040 and RP2350. More...

#include <PicoTime.h>

+ Collaboration diagram for PicoTime:

Static Public Member Functions

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)
 

Detailed Description

Definition at line 34 of file PicoTime.h.

Member Function Documentation

◆ 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; // Sunday = 0
125 return static_cast<Day>(1u << weekday);
126 }
Day
Enum for days of the week as bitmask flags.
Definition DaysOfWeek.h:18

◆ 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; // Sunday = 0
119 return static_cast<DaysOfWeek>(1u << weekday);
120 }
uint8_t DaysOfWeek
Type alias for a set of days (bitmask).
Definition DaysOfWeek.h:31

◆ 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.
Definition PicoTime.cpp:50

References nowTm().

+ Here is the call graph for this function:

◆ now()

time_t PicoTime::now ( )
static

Get the current epoch time using platform RTC or AON.

Returns
Current UNIX timestamp.
Returns
Current UNIX timestamp.

Definition at line 28 of file PicoTime.cpp.

29{
30#if defined(PICO_RP2040)
31 datetime_t dt;
32 rtc_get_datetime(&dt);
33 struct tm t = {};
34 t.tm_year = dt.year - 1900;
35 t.tm_mon = dt.month - 1;
36 t.tm_mday = dt.day;
37 t.tm_hour = dt.hour;
38 t.tm_min = dt.min;
39 t.tm_sec = dt.sec;
40 return mktime(&t);
41#elif defined(PICO_RP2350)
42 // Use AON timer for RP2350
43 timespec t;
44 aon_timer_get_time(&t);
45 return t.tv_sec; // Return seconds since epoch
46#endif
47}

Referenced by TimeManager::currentTimeForTrace(), TimeManager::formatTimeWithZone(), Logger::getTimeString(), printNow(), TimerService::scheduleAt(), TimerService::scheduleCallbackAt(), TimerService::scheduleDailyAt(), and TimerService::scheduleDuration().

+ Here is the caller graph for this function:

◆ 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{
52 time_t t = now();
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.
Definition PicoTime.cpp:28

Referenced by getNowHhMmSs().

+ Here is the caller graph for this function:

◆ print() [1/2]

void PicoTime::print ( const struct tm *  t)
static

Print a struct tm to stdout.

Parameters
tPointer to tm to print.
Parameters
tPointer to tm to print.

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
tUNIX time value.
Parameters
tUNIX time value.

Definition at line 116 of file PicoTime.cpp.

117{
118 struct tm tm;
119 localtime_r(&t, &tm);
120 print(&tm);
121}
static void print(time_t t)
Print a given UNIX timestamp to stdout.
Definition PicoTime.cpp:116

References print().

Referenced by print(), and printNow().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ printNow()

void PicoTime::printNow ( )
static

Print the current time to stdout.

Definition at line 110 of file PicoTime.cpp.

111{
112 print(now());
113}

References now(), and print().

+ Here is the call graph for this function:

◆ todayAt()

struct tm PicoTime::todayAt ( const struct tm *  hhmmss)
static

Construct today's date with a specific time-of-day.

Parameters
hhmmssPointer to a tm with hour/min/sec set.
Returns
Combined tm with today's date and given time.
Parameters
hhmmssPointer 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{
80 struct tm now = nowTm();
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,
86 .tm_mon = now.tm_mon,
87 .tm_year = now.tm_year,
88 .tm_isdst = -1};
89 mktime(&result); // Normalize
90 return result;
91}

Referenced by todayAtTimeT(), and todayHhMmSsString().

+ Here is the caller graph for this function:

◆ todayAtTimeT()

time_t PicoTime::todayAtTimeT ( const struct tm *  hhmmss)
static

Convert a tm with today's date and time to epoch.

Parameters
hhmmssPointer to tm with time-of-day.
Returns
Time as UNIX timestamp.
Parameters
hhmmssPointer to tm with time-of-day.
Returns
Time as UNIX timestamp.

Definition at line 94 of file PicoTime.cpp.

95{
96 struct tm t = todayAt(hhmmss);
97 return mktime(&t);
98}
static struct tm todayAt(const struct tm *hhmmss)
Construct today's date with a specific time-of-day.
Definition PicoTime.cpp:78

References todayAt().

+ Here is the call graph for this function:

◆ todayHhMmSsString()

std::string PicoTime::todayHhMmSsString ( const struct tm *  hhmmss)
static

Convert a time-of-day to a string for today.

Parameters
hhmmssPointer to tm with hour/min/sec.
Returns
Formatted string including date and time.
Parameters
hhmmssPointer to tm with hour/min/sec.
Returns
Formatted string including date and time.

Definition at line 101 of file PicoTime.cpp.

102{
103 struct tm t = todayAt(hhmmss);
104 char buf[32];
105 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &t);
106 return std::string(buf);
107}

References todayAt().

+ Here is the call graph for this function:

◆ toTimeOfDay()

static TimeOfDay PicoTime::toTimeOfDay ( uint32_t  timestamp)
inlinestatic
Parameters
dtPointer 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).
Definition TimeOfDay.h:22

The documentation for this class was generated from the following files: