|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <Arduino.h> |
| 4 | +#include <Arduino_EdgeControl.h> |
| 5 | +#include <mbed_mktime.h> |
| 6 | + |
| 7 | +// Convert build time to UNIX time |
| 8 | +time_t getBuildDateTime(bool local_time = true, int tz = 0) |
| 9 | +{ |
| 10 | + char s_month[5]; |
| 11 | + int year; |
| 12 | + |
| 13 | + tm t; |
| 14 | + time_t seconds; |
| 15 | + |
| 16 | + static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; |
| 17 | + |
| 18 | + sscanf(__DATE__, "%s %d %d", s_month, &t.tm_mday, &year); |
| 19 | + sscanf(__TIME__, "%2d %*c %2d %*c %2d", &t.tm_hour, &t.tm_min, &t.tm_sec); |
| 20 | + |
| 21 | + // Find where is s_month in month_names. Deduce month value. |
| 22 | + t.tm_mon = (strstr(month_names, s_month) - month_names) / 3 + 1; |
| 23 | + t.tm_year = year - 1900; |
| 24 | + _rtc_maketime(&t, &seconds, RTC_FULL_LEAP_YEAR_SUPPORT); |
| 25 | + |
| 26 | + if (!local_time) { |
| 27 | + if (tz > 200) { |
| 28 | + tz = 0x100 - tz; // Handle negative values |
| 29 | + seconds += (3600UL) * tz; |
| 30 | + } else { |
| 31 | + seconds -= (3600UL) * tz; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return seconds; |
| 36 | +} |
| 37 | + |
| 38 | +String getRTCDate() |
| 39 | +{ |
| 40 | + // APIs to get date fields. |
| 41 | + auto years = RealTimeClock.getYears(); |
| 42 | + auto months = RealTimeClock.getMonths(); |
| 43 | + auto days = RealTimeClock.getDays(); |
| 44 | + |
| 45 | + char buf[12] {}; |
| 46 | + |
| 47 | + snprintf(buf, 11, "20%02d-%02d-%02d", years, months, days); |
| 48 | + |
| 49 | + return String(buf); |
| 50 | +} |
| 51 | + |
| 52 | +String getRTCTime() |
| 53 | +{ |
| 54 | + // APIs to get time fields. |
| 55 | + auto hours = RealTimeClock.getHours(); |
| 56 | + auto minutes = RealTimeClock.getMinutes(); |
| 57 | + auto seconds = RealTimeClock.getSeconds(); |
| 58 | + |
| 59 | + char buf[11] {}; |
| 60 | + |
| 61 | + snprintf(buf, 10, "%02d:%02d:%02d", hours, minutes, seconds); |
| 62 | + |
| 63 | + return String(buf); |
| 64 | +} |
| 65 | + |
| 66 | +String getRTCDateTime() |
| 67 | +{ |
| 68 | + auto date = getRTCDate(); |
| 69 | + auto time = getRTCTime(); |
| 70 | + |
| 71 | + auto dateTime = date + ' ' + time; |
| 72 | + |
| 73 | + return dateTime; |
| 74 | +} |
| 75 | + |
| 76 | +String getLocaltime() |
| 77 | +{ |
| 78 | + char buffer[32]; |
| 79 | + tm t; |
| 80 | + _rtc_localtime(time(NULL), &t, RTC_FULL_LEAP_YEAR_SUPPORT); |
| 81 | + strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t); |
| 82 | + return String(buffer); |
| 83 | +} |
| 84 | + |
| 85 | +String getLocaltime(const time_t& build_time) |
| 86 | +{ |
| 87 | + char buffer[32]; |
| 88 | + tm t; |
| 89 | + _rtc_localtime(build_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT); |
| 90 | + strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t); |
| 91 | + return String(buffer); |
| 92 | +} |
| 93 | + |
| 94 | +String getLocaltime(const char* fmt, bool local_time = true, int tz = 0) |
| 95 | +{ |
| 96 | + char buffer[64]; |
| 97 | + time_t tmp_time = time(NULL); |
| 98 | + tm t; |
| 99 | + |
| 100 | + if (!local_time) { |
| 101 | + if (tz > 200) { |
| 102 | + tz = 0x100 - tz; // Handle negative values |
| 103 | + tmp_time -= (3600UL) * tz; |
| 104 | + } else { |
| 105 | + tmp_time += (3600UL) * tz; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + _rtc_localtime(tmp_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT); |
| 110 | + strftime(buffer, 64, fmt, &t); |
| 111 | + return String(buffer); |
| 112 | +} |
| 113 | + |
| 114 | +String getLocaltime(const time_t build_time, const char* fmt, bool local_time = true, int tz = 0) |
| 115 | +{ |
| 116 | + char buffer[64]; |
| 117 | + time_t tmp_time = build_time; |
| 118 | + tm t; |
| 119 | + |
| 120 | + if (!local_time) { |
| 121 | + if (tz > 200) { |
| 122 | + tz = 0x100 - tz; // Handle negative values |
| 123 | + tmp_time -= (3600UL) * tz; |
| 124 | + } else { |
| 125 | + tmp_time += (3600UL) * tz; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + _rtc_localtime(tmp_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT); |
| 130 | + strftime(buffer, 64, fmt, &t); |
| 131 | + return String(buffer); |
| 132 | +} |
0 commit comments