Convert Unix date/time from openweather Sunrise & Sunset

I have finally gotten the OpenWeatherMap program to output the data from my account. However, the Sunrise and Sunset data is output in the Unix format. I have been trying to figure out how to convert that output into a Date/Time format. The format that I am getting is Sunrise: 1674478460, Sunset: 1674478460. I have entered these two numbers into online conversion programs and they output the correct Date and Time. I am using an ESP32 -Wroom-32 board and have run the examples from the Time libraries to see if I can use the code from them. They display the correct Date and Time from the internet clocks. Unfortunately, I haven't had any success converting the sketch from those libraries over to my sketch. I have been trying many versions of other sketches this past week to get these two timestamps to convert to something usable. I'm looking for some help to get these two converted so that I may continue with my project. The code I'm using to print the two data times is

      Serial.print("Sunrise: ");
      Serial.println(myObject["sys"]["sunrise"]);
      Serial.print("Sunset: ");
      Serial.println(myObject["sys"]["sunrise"]);

Thank you for any assistance!
John

You can use standard C/C++ time functions

    time_t now = myObject["sys"]["sunrise"];
    struct tm tInfo = *localtime(&now);
    char str[30];
    strftime(str, sizeof(str), "%A %d/%m/%Y - %T", &tInfo);
    Serial.println(str);
1 Like

cotestatnt, that worked great. Is there a way that this code could be put into a Function so that I could send timestamps to it and have it return a string of the Date/time? I tried using this code for the Sunrise and the Sunset but gave an error that the variables "now" and "str" were already used. I just changed the variable names for the Sunset and it worked. I know my Time zone is -5:00, Where would I subtract the -18000 seconds to correct the Time output?
Again, many thanks for your help!!

John

I believe there are standard C/C++ time functions for setting both TZ and DST info.

Yes, easily. Study C/C++ function implementation.

This call sets TZ, DST, and sntp server all at the same time:

configTime(gmtOffsetSec, daylightOffsetSec, ntpServer);

Probably easier than messing around yourself with setenv() and tzset().

See:

For shure, check this example.

It contains also one of the possible way to configure your local timezone with ESP32.

gfvalvo, I had worked with that sketch for several hours and it works fine if reading the time from the internet. If I replace the ntpServer variable with my time variable it does not work. I believe it is looking for the day of the week in the ntpServer variable.

The only thing I wanted you to take from that example was how to use the configTime() function to set the TZ and DST info. After that, you'd use @cotestatnt's method of Post #2 to convert any Unix time you want into local time.

Ok, This is the first time I have gotten this deep into using the Time library as I have never worked with Unix time before. I will have to go back and look at the sketch again.
Thank you.

I would like to thank you all for helping me get this part of my sketch working. I now have the correct time zone and the correct output formatting for the Date and Time including the AM/PM indicators. I have learned something new today.

Thank you!!
John

gfvalvo, I did not realize that I needed to have the internet time server in the code along with my timezone offset and DST offset to get the correct time. Thank you for pointing that out!

 configTime(gmtOffset_sec, daylightOffset_sec, "pool.ntp.org");
                                                                                        ^^^^^^^ 

Well, you need it there because it's required by the function prototype of configTime(). I don't know if it's necessary for the ESP32 to actually connect to the NTP server just to do the time conversions you want.

It worked with it in there and everything is formatted correctly. I took it out and it did not work.
Maybe when it reads the data from openWeatherMap it uses that time from the internet to get the correct time needed for my data.
Thanks again for your help!

I don't know what "did not work". If you don't specify a 3rd argument to the function it won't even compile.

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