Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
PicoTime.cpp
Go to the documentation of this file.
1
15#include "time/PicoTime.h"
16#include <cstdio>
17#include <cstring>
18#include <sstream>
19#include <iomanip>
20
21#if defined(PICO_RP2040)
22#include "hardware/rtc.h"
23#elif defined(PICO_RP2350)
24#include "pico/aon_timer.h"
25#endif
26
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}
48
50struct tm PicoTime::nowTm()
51{
52 time_t t = now();
53 struct tm out;
54 localtime_r(&t, &out);
55 return out;
56}
57
59#if defined(PICO_RP2040)
60datetime_t PicoTime::nowDatetime()
61{
62 datetime_t dt;
63 rtc_get_datetime(&dt);
64 return dt;
65}
66#endif
67
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}
76
78struct tm PicoTime::todayAt(const struct tm *hhmmss)
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}
92
94time_t PicoTime::todayAtTimeT(const struct tm *hhmmss)
95{
96 struct tm t = todayAt(hhmmss);
97 return mktime(&t);
98}
99
101std::string PicoTime::todayHhMmSsString(const struct tm *hhmmss)
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}
108
111{
112 print(now());
113}
114
116void PicoTime::print(time_t t)
117{
118 struct tm tm;
119 localtime_r(&t, &tm);
120 print(&tm);
121}
122
124void PicoTime::print(const struct tm *t)
125{
126 char buf[32];
127 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", t);
128 printf("%s\n", buf);
129}
130
131std::string PicoTime::formatIso8601(time_t t)
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}
139
141#if defined(PICO_RP2040)
142void PicoTime::print(const datetime_t *dt)
143{
144 printf("%04d-%02d-%02d %02d:%02d:%02d\n",
145 dt->year, dt->month, dt->day,
146 dt->hour, dt->min, dt->sec);
147}
148#endif
Time utility functions for the Pico platform (RP2040/RP2350).
Cross-platform time utilities for RP2040 and RP2350.
Definition PicoTime.h:35
static void printNow()
Print the current time to stdout.
Definition PicoTime.cpp:110
static struct tm todayAt(const struct tm *hhmmss)
Construct today's date with a specific time-of-day.
Definition PicoTime.cpp:78
static std::string formatIso8601(time_t t)
Definition PicoTime.cpp:131
static struct tm nowTm()
Get the current local time as struct tm.
Definition PicoTime.cpp:50
static time_t now()
Get the current epoch time using platform RTC or AON.
Definition PicoTime.cpp:28
static time_t todayAtTimeT(const struct tm *hhmmss)
Convert a tm with today's date and time to epoch.
Definition PicoTime.cpp:94
static void print(time_t t)
Print a given UNIX timestamp to stdout.
Definition PicoTime.cpp:116
static std::string todayHhMmSsString(const struct tm *hhmmss)
Convert a time-of-day to a string for today.
Definition PicoTime.cpp:101
static std::string getNowHhMmSs()
Get the current time as a datetime_t.
Definition PicoTime.cpp:69