/*
*
* time_lib.ino
*
*
* https://wokwi.com/projects/363350134511883265
*
*
* library <time.h> ==> details: https://sourceware.org/newlib/libc.html#Timefns
* #include <time.h> <<<=== BY DEFAULT, this library is always included
*
*/
const char ary_DOW[7][10] = { {"Sunday"}, {"Monday"}, {"Tuesday"}, {"Wednesday"}, {"Thursday"}, {"Friday"}, {"Saturday"} };
void Time_01(uint32_t time32) {
// Convert from epoch to human-readable date
//
Serial.println("\nTime_01() - from seconds (epoch) to time-stamp date:");
// library <time.h> details: https://sourceware.org/newlib/libc.html#Timefns
//
time_t rawtime = time32; // EPOCH ==> 1262304000;
struct tm ts;
char buf[30];
// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
ts = *localtime(&rawtime);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
Serial.println(buf);
//
strftime(buf, 30, "%Y-%m-%d_T_%H:%M:%S", &ts);
String s_Timestamp = String(buf); // e.g.: "2010-01-02_T_11:22:33"
// remove any leading and trailing whitespaces
s_Timestamp.trim();
Serial.println(s_Timestamp);
//
strftime(buf, 5, "%u", &ts);
uint8_t iDow = atoi(buf);
Serial.println(iDow);
}
uint32_t Time_02() {
// Convert from date to epoch
//
Serial.println("\nTime_02() - from time-stamp date to seconds (epoch):");
// library <time.h> details: https://sourceware.org/newlib/libc.html#Timefns
//
struct tm t;
time_t t_of_day;
t.tm_year = 2037-1900; // Year - 1900
t.tm_mon = 10; // Month: 0->jan, 1->feb, 2->mar, ...
t.tm_mday = 16; // Day of the month
t.tm_hour = 07;
t.tm_min = 19;
t.tm_sec = 59;
t.tm_isdst = 0; // Is DST on? 1 = yes, 0 = no, -1 = unknown
t_of_day = mktime(&t);
//
uint32_t Today32 = ((uint32_t) t_of_day);
//
Serial.printf("NOW TIME seconds since the Epoch: ");
Serial.println(Today32);
//
// returns -NOW TIME- in seconds
return Today32;
//
} // Time_02()
void get_DayOfweek(uint32_t time32) {
// Convert from epoch to Day of the Week
//
Serial.println("\nget_DayOfweek() - from seconds (epoch) to Day of the Week:");
// library <time.h> details: https://sourceware.org/newlib/libc.html#Timefns
//
time_t rawtime = time32; // EPOCH
struct tm t;
char buf[12];
t = *localtime(&rawtime);
strftime(buf, 12, "%A", &t);
String week_day = String(buf); // Day of the week
Serial.printf("Day of the week: ");
Serial.println(week_day);
strftime(buf, 5, "%a", &t);
week_day = String(buf); // Day of the week
Serial.printf("Day of the week: ");
Serial.println(week_day);
//
strftime(buf, 5, "%u", &t);
uint8_t iDow = atoi(buf);
Serial.printf("Day of the week (0-6): ");
Serial.println(iDow);
}
/*
void epoch2timestamp(uint32_t time32, String &s_Timestamp, String &s_EvntDay) {
// convert from seconds (epoch) to timestamp date
//
// library <time.h> details: https://sourceware.org/newlib/libc.html#Timefns
//
time_t rawtime = time32; // EPOCH ==> 1262304000;
struct tm ts;
char buf[30];
//
// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
ts = *localtime(&rawtime);
strftime(buf, 30, "%Y-%m-%d_T_%H:%M:%S", &ts);
s_Timestamp = String(buf); // e.g.: "2010-01-02_T_11:22:33"
// remove any leading and trailing whitespaces
s_Timestamp.trim();
//
strftime(buf, 12, "%A", &ts);
s_EvntDay = String(buf); // Day of the week
s_EvntDay.trim();
//
} // epoch2timestamp()
*/
void epoch2timestamp(time_t rawtime, String &s_Timestamp, String &s_EvntDay, uint8_t &dayOfWk8) {
// convert from seconds (epoch) to timestamp date
// [Note: EPOCH time start from 1970-01-01 00:00:00, and a max of 2147472000 seconds (==>2038-01-19 00:00:00)]
//
// library <time.h> details: https://sourceware.org/newlib/libc.html#Timefns
//
//time_t rawtime = time32; // EPOCH (seconds) ==> 1262304000 from 1970-01-01 00:00:00
struct tm ts;
char buf[30];
//
// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
ts = *localtime(&rawtime);
strftime(buf, 30, "%Y-%m-%d_T_%H:%M:%S", &ts);
s_Timestamp = String(buf); // e.g.: "2024-02-01_T_01:02:03"
// remove any leading and trailing whitespaces
s_Timestamp.trim();
//
// %A --> tm_wday --> Day of week, between ‘Sunday’ (0), ‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’ (6)
strftime(buf, 12, "%A", &ts);
s_EvntDay = String(buf); // Day of the Event (e.g.: Moday, Saturday...)
s_EvntDay.trim(); // remove any leading and trailing whitespaces
/*
// OK!!!!
// %w --> tm_wday --> The weekday as a number, from 0 (Sunday) to 6 (Saturday)
strftime(buf, 12, "%w", &ts);
String s_DayOfWk = String(buf);
s_DayOfWk.trim(); // remove any leading and trailing whitespaces
dayOfWk8 = s_DayOfWk.toInt();
*/
// %w --> tm_wday --> The weekday as a number, from 0 (Sunday) to 6 (Saturday)
strftime(buf, 5, "%w", &ts);
dayOfWk8 = atoi(buf);
//
} // epoch2timestamp()
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//
delay(10);
//
Serial.println("\n\n\nHello, ESP32!\n");
//
//uint32_t setDate32 = 1682783900;//2023-04-29 15:58:20 1682783900 Sat 15:58:20 GMT
//
// 2024-02-17T07:19:59 1708154399 Sat, 17 Feb 2024 07:19:5
uint32_t setDate32 = 1708154399; //2024-02-17 07:19:59 1708154399 Sat 07:19:59 GMT
//
Serial.print("** setup() - setDate32 (seconds) = ");Serial.println(setDate32);
//
Time_01(setDate32); // Sat 2023-04-29 15:58:20 GMT
//
setDate32 = Time_02(); // seconds since the Epoch: 1682783900
//
Time_01(setDate32); // GET the TIMESTAMP of TODAY
//
get_DayOfweek(setDate32); //
//
//
uint8_t dayOfWk8 = 9;
String s_Timestamp = "", s_EvntDay = "";
epoch2timestamp(setDate32, s_Timestamp, s_EvntDay, dayOfWk8);
Serial.print("** setup() - setDate32 (seconds) = ");Serial.println(setDate32);
Serial.print("** setup() - s_EvntDay = ");Serial.println(s_EvntDay);
Serial.print("** setup() - s_Timestamp = ");Serial.println(s_Timestamp);
Serial.print("** setup() - dayOfWk8 (0-6) = ");Serial.println(dayOfWk8);
//
Serial.println();
for (uint8_t i = 0; i < 7; i++) Serial.println(String(ary_DOW[i]));
//
}
void loop() {
// put your main code here, to run repeatedly:
//
delay(100);
//
}
/*
Output:
Hello, ESP32!
** setup() - setDate32 (seconds) = 1708154399
Time_01() - from seconds (epoch) to time-stamp date:
Sat 2024-02-17 07:19:59 GMT
2024-02-17_T_07:19:59
6
Time_02() - from time-stamp date to seconds (epoch):
NOW TIME seconds since the Epoch: 2141968799
Time_01() - from seconds (epoch) to time-stamp date:
Mon 2037-11-16 07:19:59 GMT
2037-11-16_T_07:19:59
1
get_DayOfweek() - from seconds (epoch) to Day of the Week:
Day of the week: Monday
Day of the week: Mon
Day of the week (0-6): 1
** setup() - setDate32 (seconds) = 2141968799
** setup() - s_EvntDay = Monday
** setup() - s_Timestamp = 2037-11-16_T_07:19:59
** setup() - dayOfWk8 (0-6) = 1
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
*/