GeekMagicClock / smalltv-ultra

Ultra version firmware of GeekMagic smalltv
112 stars 9 forks source link

Request: Sunrise/Sunset screen #77

Open Duberry opened 2 weeks ago

Duberry commented 2 weeks ago

openweather JSON includes the sunrise and sunset as UNIX epoch time.

 "sys": {
    "type": 2,
    "id": 2075663,
    "country": "IT",
    "sunrise": 1661834187,
    "sunset": 1661882248
  },

Request to add a simple sunrise and sunset time screen?

GeekMagicClock commented 2 weeks ago

Hi, thank you, how to convert UNIX sunrise/sunset time to your local time hours and minutes?

Duberry commented 2 weeks ago

Hi, thank you, how to convert UNIX sunrise/sunset time to your local time hours and minutes?

 "sys": {
    "type": 2,
    "id": 2075663,
    "country": "IT",
    "sunrise": 1661834187,
    "sunset": 1661882248
  },

// assume you use time.h and setup the device time something like this
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
struct tm timeinfo;

time_t sunrise_timestamp = 1661834187; // Example UNIX time
time_t sunset_timestamp = 1661882248; // Example UNIX time

 struct tm sunrise_timeinfo;
 localtime_r(&sunrise_timestamp , &timeinfo);  //<-- This does the main work

 Serial.printf("Converted local time: %02d:%02d:%02d\n",
                sunrise_timeinfo.tm_hour,
                sunrise_timeinfo.tm_min,
                sunrise_timeinfo.tm_sec);
}

 struct tm sunset_timeinfo ;
 localtime_r(&sunset_timestamp , &timeinfo);  //<-- This does the main work

 Serial.printf("Converted local time: %02d:%02d:%02d\n",
               sunset_timeinfo.tm_hour,
               sunset_timeinfo.tm_min,
               sunset_timeinfo.tm_sec);
}