Skip to content

Commit 956fe44

Browse files
committed
Added utc offset for half hours, fixed negative offset issue
1 parent 77891b2 commit 956fe44

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

TimeClient.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ See more at http://blog.squix.ch
2525

2626
#include "TimeClient.h"
2727

28-
TimeClient::TimeClient(int utcOffset) {
28+
TimeClient::TimeClient(float utcOffset) {
2929
myUtcOffset = utcOffset;
3030
}
3131

@@ -60,8 +60,11 @@ void TimeClient::updateTime() {
6060
int parsedHours = line.substring(23, 25).toInt();
6161
int parsedMinutes = line.substring(26, 28).toInt();
6262
int parsedSeconds = line.substring(29, 31).toInt();
63+
Serial.println(String(parsedHours) + ":" + String(parsedMinutes) + ":" + String(parsedSeconds));
6364

6465
localEpoc = (parsedHours * 60 * 60 + parsedMinutes * 60 + parsedSeconds);
66+
Serial.println(localEpoc);
67+
localMillisAtUpdate = millis();
6568
}
6669
}
6770
}
@@ -72,7 +75,7 @@ String TimeClient::getHours() {
7275
if (localEpoc == 0) {
7376
return "--";
7477
}
75-
int hours = (getCurrentEpoch() % 86400L) / 3600 + myUtcOffset;
78+
int hours = ((getCurrentEpochWithUtcOffset() % 86400L) / 3600) % 24;
7679
if (hours < 10) {
7780
return "0" + String(hours);
7881
}
@@ -83,7 +86,7 @@ String TimeClient::getMinutes() {
8386
if (localEpoc == 0) {
8487
return "--";
8588
}
86-
int minutes = ((getCurrentEpoch() % 3600) / 60);
89+
int minutes = ((getCurrentEpochWithUtcOffset() % 3600) / 60);
8790
if (minutes < 10 ) {
8891
// In the first 10 minutes of each hour, we'll want a leading '0'
8992
return "0" + String(minutes);
@@ -94,7 +97,7 @@ String TimeClient::getSeconds() {
9497
if (localEpoc == 0) {
9598
return "--";
9699
}
97-
int seconds = getCurrentEpoch() % 60;
100+
int seconds = getCurrentEpochWithUtcOffset() % 60;
98101
if ( seconds < 10 ) {
99102
// In the first 10 seconds of each minute, we'll want a leading '0'
100103
return "0" + String(seconds);
@@ -110,3 +113,8 @@ long TimeClient::getCurrentEpoch() {
110113
return localEpoc + ((millis() - localMillisAtUpdate) / 1000);
111114
}
112115

116+
long TimeClient::getCurrentEpochWithUtcOffset() {
117+
return round(getCurrentEpoch() + 3600 * myUtcOffset + 86400L) % 86400L;
118+
}
119+
120+

TimeClient.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ See more at http://blog.squix.ch
2525
#pragma once
2626

2727
#include <ESP8266WiFi.h>
28-
#include <WiFiUdp.h>
2928

3029
#define NTP_PACKET_SIZE 48
3130

3231
class TimeClient {
3332

3433
private:
35-
int myUtcOffset = 0;
34+
float myUtcOffset = 0;
3635
long localEpoc = 0;
3736
long localMillisAtUpdate;
3837

@@ -43,14 +42,15 @@ class TimeClient {
4342

4443

4544
public:
46-
TimeClient(int utcOffset);
45+
TimeClient(float utcOffset);
4746
void updateTime();
4847

4948
String getHours();
5049
String getMinutes();
5150
String getSeconds();
5251
String getFormattedTime();
5352
long getCurrentEpoch();
53+
long getCurrentEpochWithUtcOffset();
5454

5555
};
5656

0 commit comments

Comments
 (0)