Unix time out by 30 years

Based on something coppolino wrote I have modified it as follows

#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <time.h>

const char *ssid     = "!@#$%^&*";
const char *password = "*&^%$#@!():";'";
char TimeString[80];

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
int TimeOffsetForJhb = 72000;

void setup() {
  Serial.begin(9600);
  while(WiFi.status() != WL_CONNECTED){
    WiFi.begin(ssid,password);
    delay(1000);
    Serial.println("Failed to connect...");
  }

  timeClient.begin();

}
void loop() {
  timeClient.update();

  time_t now = timeClient.getEpochTime()+TimeOffsetForJhb;
  struct tm timeInfo = *localtime(&now);
  strftime(TimeString, sizeof(TimeString), "%d/%m/%Y -- %H:%M:%S", &timeInfo );
  Serial.println(timeClient.getEpochTime());
  Serial.println(TimeString);

  delay(5000);
}

but find that the year is 30 years in the future. Please tell me what I have done wrong.

The epoch is originally a 32-bit counter, starting from January 1, 1970.
Sometimes it starts from the year 2000 and sometimes it is a 64-bit counter.

Wikipedia: Epoch (computing) - Wikipedia

Which Time library do you use ? It has probably a number for those 30 years.

As by default I would choose to use linux time which library should I use.
Looking at the Wikipaedia link it would seem that 30 years could relate to 2053(+30) or 1993(-30) and I can find no relationship to those years in Wikipaedia.

As this is the common Arduino forum, you missed to tell us what board you are using.

Anyway, please note at first JHB is UTC+02 so the offset shouldn't be 72000 but 7200 (3600*2).
Then, I loaded this sketch (derived from yours one) to a WeMos D1 I have on my desk now, I know it's a different board and CPU, but it works like a charm:

#include <SPI.h>

//#include <WiFiNINA.h>
// ESP added code
  #include <ESP8266WiFi.h>

#include <WiFiUdp.h>
#include <NTPClient.h>
#include <time.h>

const char* ssid     = "myssid";
const char* password = "mypw";
char TimeString[80];

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
int TimeOffsetForJhb = 7200; // Not 72000!

void setup() {
  Serial.begin(115200);
  // ESP added code
  WiFi.mode(WIFI_STA);
  
  // Not necessary, you need to call begin() first and once, and then check
  //if (WiFi.status() != WL_CONNECTED) {
  WiFi.begin(ssid, password);
  // Added loop to wait...
  while (WiFi.status() != WL_CONNECTED) {
      Serial.print(".");
      delay(500);
  }
  timeClient.begin();
}

void loop() {
  timeClient.update();

  time_t now = timeClient.getEpochTime()+TimeOffsetForJhb;
  struct tm timeInfo = *localtime(&now);
  strftime(TimeString, sizeof(TimeString), "%d/%m/%Y -- %H:%M:%S", &timeInfo );
  Serial.println(timeClient.getEpochTime());
  Serial.println(TimeString);

  delay(5000);
}

Sorry I should have told you that the board is an Arduino Uno wifi rev2.

I am still getting the year returned as 2053 on my Arduino Uno wifi rev2 IDE 2.1.0

What is the epoch time shown by your sketch? Is it 30 years off, or is it correct and the text in TimeString wrong?

An example is timeClient.update returns
1686224281
and strftime gives
07/06/2053 -- 13:38:01

Ok, that is correct for 2023. Looks like the error is with time.h, print out now and localtime(&now) and see if either of those is introducing the 30-year offset.

< edit > Have a look at this discussion, it appears time.h used 2000 as the reference instead of 1970
https://forum.arduino.cc/t/result-of-strftime-is-30-years-off/658302/6

2 Likes

This then might be another occurance of "Use the standard" and I answer "Which one" and the reply is "There are plenty to choose from, use one".

1 Like

One line of code fixed it thanks to david_2018
after the time_t now = timeClient.getEpochTime()+TimeOffsetForJhb; line use the following to correct year.
now = now - UNIX_OFFSET;

1 Like

Does this print 72000? Or 6464?

Serial.print(TimeOffsetForJhb);

It prints 7200.

My original ```
TimeOffsetForJhb

was incorrect and shoud have been 7200 not 72000 as pointed out by docdoc.
1 Like

Honestly, one way to get this to work would be to just try 1993 to get 2023. I know this isn’t very helpful, but hey, it works!

Please explain what you mean.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.