Convert Time Epoch in Date ,OpenWeather

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
ixbo
Posts: 36
Joined: Wed May 04, 2022 11:12 am
Location: France

Convert Time Epoch in Date ,OpenWeather

Post by ixbo » Wed Jun 29, 2022 5:56 pm

Hello
Always big problem
I use ESP32 with THONNY ( Micropython)
I have make this program witch work well , the connection to wifi is not represented

Code: Select all

[color=#008000]import urequests


APIKEY = "fb07531d1fe37a3effcdf3755be2f6a9"   #OpenWeather
API_call = "http://api.openweathermap.org/data/2.5/forecast?id=524901&lang=fr&appid="+APIKEY

reponse = urequests.get(API_call)
res = reponse.json()
print(res)
print(res.keys())

print(res['list'][0])
print('vent',res['list'][0]['wind']['speed'],'m/s')
print('direction',res['list'][0]['wind']['deg'],'degre')
print('date',res['list'][0]['dt_txt'])
print('')
print('niveau sol',res['list'][0]['main']['grnd_level'],'hPa')
print('temp mini',(res['list'][0]['main']['temp_min'])-273.15)
print('temp max',(res['list'][0]['main']['temp_max'])-273.15)
print('humidite',res['list'][0]['main']['humidity'],'%')
print('visibilite',res['list'][0]['visibility'],'m')
print('date',res['list'][0]['dt'])[/color]
The date is in "Time Epoch" format .....and when i try to convert this date i have an erroneous date in 2052....

Is there anybody witch can explain me how to convert this EPOCH date in conventional date

Thanks very much
best regards

KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

Re: Convert Time Epoch in Date ,OpenWeather

Post by KJM » Wed Jun 29, 2022 11:49 pm

upython uses a different start time to the usual unix epoch used by python, subtract 946684800 before converting to human readable.

ixbo
Posts: 36
Joined: Wed May 04, 2022 11:12 am
Location: France

Re: Convert Time Epoch in Date ,OpenWeather

Post by ixbo » Thu Jun 30, 2022 7:39 am

Thanks very much
Now my date is readable !!!!

OFFSET_DATE = 946684800
d = res['list'][0]['dt']
print('date',utime.gmtime(d-OFFSET_DATE))


Very nice !!!!!!!!!!
Best regards

Post Reply