4

I have a question on how to convert EPOCH timestamps, which I receive as char*, into DD.MM.YYYY and HH:MM:SS format separately?

Here is more information on my Arduino project: The Arduino is receiving through an ESP8266 module three different EPOCH timestamps from the OpenWeatherMap API for sunrise, sunset and the current date. I want these 3 pieces of information to be displayed on a 1,3" OLED display, the date in DD.MM.YYYY and the other two timestamps in HH:MM format.

I was looking in several time libraries like TimeLib and RTC for a solution, but I wasn't able to find one.

That's my code fragment on receiving the timestamps:

DynamicJsonBuffer jsonBuffer(4096);
JsonObject& root = jsonBuffer.parseObject(client);
const char* date = root["dt"];
const char* sunrise = root["sys"]["sunrise"];
const char* sunset = root["sys"]["sunset"];

How to convert those timestamps?

Rohit Gupta
6122 gold badges5 silver badges18 bronze badges
asked Feb 14, 2019 at 14:39
0

2 Answers 2

3

Epoch are seconds from 1970年01月01日. Get it from JSON as unsigned long. They are a number in the returned JSON (no ")

from https://openweathermap.org/ doc:

"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},


to convert it to string with TimeLib.h:

#include <TimeLib.h>
#include <ArduinoJson.h>
void setup() {
 Serial.begin(115200);
 const char* json = "{\"country\":\"JP\",\"sunrise\":1369769524,\"sunset\":1369821049}";
 DynamicJsonBuffer jsonBuffer;
 JsonObject& root = jsonBuffer.parseObject(json);
 unsigned long t = root["sunrise"];
 char buff[32];
 sprintf(buff, "%02d.%02d.%02d %02d:%02d:%02d", day(t), month(t), year(t), hour(t), minute(t), second(t));
 Serial.println(buff);
}
void loop() {
}
answered Feb 14, 2019 at 15:07
6
  • note you might need to divide the timestamp by 1000 if it includes ms, like JS's Dates for example. Commented Feb 14, 2019 at 18:31
  • @dandavis, I took the sample JSON from the openweathermap site Commented Feb 14, 2019 at 18:39
  • For sure, I'm just pointing out that there's two "epoc"s to be aware of from web services, in general. Commented Feb 14, 2019 at 18:41
  • For this example, it's not necessary to divide by 1000. Thank you very much for the help :) Commented Feb 14, 2019 at 18:43
  • @Juraj I have one more question to this topic: Can TimeLib also convert EPOCH timestamps to day names?(Monday, etc.) Commented Feb 15, 2019 at 10:53
0

I'm was having a similar challenge with my custom Wifi library m_wifi..cpp for the ESP32 ( see here )

After reading and googling I ended up coding the solution below:

ESP32Time rtc(0);
long int epochTime;
...
rtc.setTime( epochTime );
Serial.println( rtc.getDateTime(true) );
answered Jun 29, 2023 at 12:07
2
  • can it be used on a non esp32 Arduino as in the question? Commented Jun 29, 2023 at 12:17
  • yes. you can replicate the code to use on other than ESP32. Just need to find the compatible library for your specific MCU. Commented Jun 30, 2023 at 9:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.