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);
}
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.
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;