-
Notifications
You must be signed in to change notification settings - Fork 1
/
date_helper.h
102 lines (92 loc) · 3.4 KB
/
date_helper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#pragma once // Please format this file with clang before check-in to GitHub
/*
File: date_helper.h
Software: Barry Hansen, K7BWH, [email protected], Seattle, WA
Hardware: John Vanderbeck, KM7O, Seattle, WA
Purpose: This contains date and time conversion utilities:
date to string, elapsed time between dates, next minute, etc.
The goal is to collect these helpers together in one place,
because these techniques are the most likely to be reused or
rewritten into other programming languages.
*/
#include "constants.h" // Griduino constants and colors
#include "logger.h" // conditional printing to Serial port
// ========== class Grids =========================================
class Dates {
public:
// ======== date time helpers =================================
char *datetimeToString(char *msg, int len, time_t tm) {
// utility function to format date: "2020-9-27 17:18:19"
// Representation is general-to-specific (year first) for sorting
// Similar to ISO-8601 format but without "T" in front of time
// Example 1:
// char sDate[24];
// datetimeToString( sDate, sizeof(sDate), now(), " GMT" );
// Serial.println( sDate );
// --> "2022-12-3 08:07 GMT"
// Example 2:
// char sDate[24];
// Serial.print("The current date is ");
// Serial.print( datetimeToString(sDate, sizeof(sDate), now()) );
// Serial.println(" GMT");
snprintf(msg, len, "%04d-%d-%d %02d:%02d:%02d",
year(tm), month(tm), day(tm),
hour(tm), minute(tm), second(tm));
return msg;
}
char *datetimeToString(char *msg, int len, time_t tm, const char *suffix) {
datetimeToString(msg, len, tm);
strncat(msg, suffix, len - 1);
return msg;
}
char *dateToString(char *msg, int len, time_t tm) {
snprintf(msg, len, "%04d-%02d-%02d", year(tm), month(tm), day(tm));
return msg;
}
char *dateToString(char *msg, int len, time_t tm, const char *suffix) {
dateToString(msg, len, tm);
strncat(msg, suffix, len - 1);
return msg;
}
char *timeToString(char *msg, int len, time_t tm) {
snprintf(msg, len, "%02d:%02d:%02d", hour(tm), minute(tm), second(tm));
return msg;
}
char *timeToString(char *msg, int len, time_t tm, const char *suffix) {
timeToString(msg, len, tm);
strncat(msg, suffix, len - 1);
return msg;
}
// Did the GPS real-time clock report a valid date?
bool isDateValid(int yy, int mm, int dd) {
bool valid = true;
if (yy < 20) {
valid = false;
}
if (mm < 1 || mm > 12) {
valid = false;
}
if (dd < 1 || dd > 31) {
valid = false;
}
if (!valid) {
// debug - issue message to console to help track down timing problem in Baroduino view
// char msg[120];
// snprintf(msg, sizeof(msg), "Date ymd not valid: %d-%d-%d");
// Serial.println(msg);
}
return valid;
}
time_t nextOneSecondMark(time_t timestamp) {
return timestamp + 1;
}
time_t nextOneMinuteMark(time_t timestamp) {
return ((timestamp + 1 + SECS_PER_MIN) / SECS_PER_MIN) * SECS_PER_MIN;
}
time_t nextFiveMinuteMark(time_t timestamp) {
return ((timestamp + 1 + SECS_PER_5MIN) / SECS_PER_5MIN) * SECS_PER_5MIN;
}
time_t nextFifteenMinuteMark(time_t timestamp) {
return ((timestamp + 1 + SECS_PER_15MIN) / SECS_PER_15MIN) * SECS_PER_15MIN;
}
}; // end class Dates