Skip to content

Commit ca9f286

Browse files
committed
Added localtime support in addition to UTC
Enables schedules to be handled in local time or UTC as needed. System clock is always UTC but now the time offset enabes modules to know local time as well.
1 parent f190bf5 commit ca9f286

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

src/MI/ModuleInterface.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ static void dummy_notification_function(NotificationType /*notification_type*/,
6868

6969
#define UTC_FIRST_ACCEPTED 1483228800ul
7070

71-
// The length of a status packet
72-
#define MI_STATUS_LEN 7
71+
// The length of a status packet
72+
#define MI_STATUS_LEN 7
7373

7474
/* Description of principle for bidirectional sync of settings:
7575
1. All modules get new settings from the master which retrieves them from a database regularly.
@@ -125,6 +125,7 @@ class ModuleInterface {
125125
time_utc_incremented_ms = 0, // Last millis() when time_utc_s was incremented
126126
time_utc_received_s = 0, // UTC time received last time, not incremented.
127127
time_utc_startup_s = 0; // Startup time, for calculating uptime
128+
int16_t time_offset_m = 0; // Current offset from GMT in minues, including DST
128129
#endif
129130
#endif
130131

@@ -494,6 +495,8 @@ class ModuleInterface {
494495
update_time();
495496
return time_utc_received_s ? time_utc_s : 0;
496497
}
498+
// Get offset in minutes from GMT, including DST and time zone
499+
int16_t get_time_offset_m() { return time_offset_m; }
497500
#endif // !NO_TIME_SYNC
498501
#endif // !IS_MASTER
499502

@@ -568,16 +571,19 @@ friend class ModuleInterfaceSet;
568571
// Receiving time sync from master
569572
void set_time(const uint8_t *message, const uint8_t length) {
570573
#ifndef NO_TIME_SYNC
571-
if (length == 4) {
574+
if (length >= 4) {
572575
uint32_t t;
576+
int16_t offset = 0;
573577
memcpy(&t, message, sizeof t);
578+
if (length >= 6) memcpy(&offset, &message[4], sizeof offset); // Take localtime offset if present (v5+)
574579
if (t > UTC_FIRST_ACCEPTED) {
575580
#ifdef DEBUG_PRINT
576581
uint32_t initial_time = get_time_utc_s();
577582
#endif
578583
time_utc_incremented_ms = millis(); // Remember when it was received so it can be auto-incremented
579584
time_utc_s = t;
580585
time_utc_received_s = time_utc_s; // Remember what time was received last
586+
time_offset_m = offset;
581587
status_bits &= ~MISSING_TIME; // Clear the missing-time bit
582588
#ifdef DEBUG_PRINT
583589
dname(); DPRINT(F("Time adjusted by ")); DPRINT((uint32_t) (time_utc_s - initial_time));
@@ -593,7 +599,6 @@ friend class ModuleInterfaceSet;
593599
void set_status(const uint8_t *message, const uint8_t length) {
594600
if (length == 6) {
595601
last_alive = millis(); if (last_alive == 0) last_alive = 1;
596-
// status_received_time = millis();
597602
comm_failures = 0;
598603
status_bits = message[0];
599604
out_of_memory = message[1];

src/MI_PJON/PJONModuleInterface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ friend class PJONModuleInterfaceSet;
109109
}
110110

111111
void update() {
112-
// Listen for packets for 1ms
113-
pjon->receive(1000);
112+
// Listen for packets
113+
pjon->receive();
114114

115115
// Module-initiated event support
116116
send_output_events();

src/MI_PJON/PJONModuleInterfaceSet.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,10 @@ class PJONModuleInterfaceSet : public ModuleInterfaceSet {
258258
char buf[5];
259259
buf[0] = (char) mcSetTime;
260260
uint32_t t = miTime::Get();
261+
int16_t offset = miTime::GetTimeZoneOffsetMinutes();
261262
memcpy(&buf[1], &t, 4);
262-
pjon->send_packet(id, bus_id, buf, 5, MI_REDUCED_SEND_TIMEOUT);
263+
memcpy(&buf[5], &offset, 2);
264+
pjon->send_packet(id, bus_id, buf, 7, MI_REDUCED_SEND_TIMEOUT);
263265
pjon->receive(); // Just called regularly to be responsive to events
264266
}
265267
#endif

src/utils/MITime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
uint32_t miTimeS = 0, // current time in UTC seconds, maintained by update()
44
miLastUpdatedTimeMs = 0, // system time in ms for last update()
55
miLastSyncedMs = 0; // system time in ms() for last setTime() call from external time source
6-
6+
int16_t miTimeOffsetMinutes = 0; // Time zone + DST current offset from UTC

src/utils/MITime.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
extern uint32_t miTimeS, // current time in UTC seconds, maintained by update()
99
miLastUpdatedTimeMs, // system time in ms for last update()
1010
miLastSyncedMs; // system time in ms() for last setTime() call from external time source
11-
11+
extern int16_t miTimeOffsetMinutes; // Time zone + DST current offset from UTC
1212

1313
struct miTime {
1414

@@ -40,6 +40,23 @@ extern uint32_t miTimeS, // current time in UTC seconds, maintained
4040
#endif
4141
}
4242

43+
// Return second count since 1970 local time
44+
static uint32_t GetLocal() { return Get() + GetTimeZoneOffsetMinutes()*60L; }
45+
46+
static void SetTimeZoneOffsetMinutes(int16_t offset_minutes) { miTimeOffsetMinutes = offset_minutes; }
47+
48+
static int16_t GetTimeZoneOffsetMinutes() {
49+
#ifdef MI_USE_SYSTEMTIME
50+
// Do not keep track of time, just use the system time functions
51+
time_t t = time(NULL);
52+
struct tm lt;
53+
localtime_r(&t, &lt);
54+
return (int16_t) (lt.tm_gmtoff/60);
55+
#else
56+
return miTimeOffsetMinutes;
57+
#endif
58+
}
59+
4360
static bool IsSynced() {
4461
#ifdef MI_USE_SYSTEMTIME
4562
return MI_SECONDS_YEAR_2017 < time(0);

0 commit comments

Comments
 (0)