-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdateTimeUtils.ino
110 lines (95 loc) · 3.29 KB
/
dateTimeUtils.ino
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
103
104
105
106
107
108
109
110
//
// dateTimeUtils.ino - A collection of utility functions for displaying date and time.
//
// API
// unsigned long getCurrentEpochTime()
// String elapsedMillisToString(unsigned long val) -> "5d 13h 42m 25s"
// String elapsedMillisToVerboseString(unsigned long val) -> "5 days, 13 hrs, 42 mins, 25 seconds"
// String getLocalTimeString() -> "Sat Jul 17 10:25:26"
// String elapsedMillisToDateString(unsigned long milliseconds) -> "Sat Jul 17 10:25:26"
// String epochTimeToDateString(unsigned long epochTime) -> "Sat Jul 17 10:25:26"
//
// macros from DateTime.h
#define SECS_PER_MIN (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY (SECS_PER_HOUR * 24L)
/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY)
String elapsedMillisToString(unsigned long val) {
char str[500];
String retStr;
val /= 1000;
int days = elapsedDays(val);
int hours = numberOfHours(val);
int minutes = numberOfMinutes(val);
int seconds = numberOfSeconds(val);
//hours += (24*days);
if (days > 0)
sprintf(str,"%dd %dh %dm %ds", days, hours, minutes, seconds);
else if (hours > 0)
sprintf(str,"%dh %dm %ds", hours, minutes, seconds);
else if (minutes > 0)
sprintf(str,"%dm %ds", minutes, seconds);
else
sprintf(str,"%ds", seconds);
retStr = (String) str;
return retStr;
}
String elapsedMillisToVerboseString(unsigned long val) {
static char str[500];
static String retStr;
val /= 1000;
int days = elapsedDays(val);
int hours = numberOfHours(val);
int minutes = numberOfMinutes(val);
int seconds = numberOfSeconds(val);
//hours += (24*days);
if (days > 0)
sprintf(str,"%d days, %d hrs, %d mins, %d seconds", days, hours, minutes, seconds);
else if (hours > 0)
sprintf(str,"%d hrs, %d mins, %d seconds", hours, minutes, seconds);
else if (minutes > 0)
sprintf(str,"%d minutes, %d seconds", minutes, seconds);
else
sprintf(str,"%2d seconds", seconds);
retStr = (String) str;
return retStr;
}
// Function that gets current epoch time
unsigned long getCurrentEpochTime() {
time_t now;
struct tm timeinfo;
//if (!localtime(&timeinfo)) {
// //Serial.println("Failed to obtain time");
// return(0);
//}
time(&now);
return now;
}
String getLocalTimeString() {
static char str[80];
static String retStr;
time_t timeSinceEpoch = (time_t) getCurrentEpochTime();
strftime(str, 80, "%H:%M:%S", localtime(&timeSinceEpoch));
retStr = (String) str;
return retStr;
}
String elapsedMillisToDateString(unsigned long milliseconds) {
static char str[80];
static String retStr;
time_t timeSinceEpoch = (time_t) (getCurrentEpochTime() - (milliseconds/1000));
strftime(str, 80, "%a %b %e %H:%M:%S", localtime(&timeSinceEpoch));
retStr = (String) str;
return retStr;
}
String epochTimeToDateString(unsigned long epochTime) {
static char str[80];
static String retStr;
time_t timeSinceEpoch = (time_t) epochTime;
strftime(str, 80, "%a %b %e %H:%M:%S", localtime(&timeSinceEpoch));
retStr = (String) str;
return retStr;
}