Page 1 of 1

Convert Time Epoch in Date ,OpenWeather

Posted: Wed Jun 29, 2022 5:56 pm
by ixbo
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

Re: Convert Time Epoch in Date ,OpenWeather

Posted: Wed Jun 29, 2022 11:49 pm
by KJM
upython uses a different start time to the usual unix epoch used by python, subtract 946684800 before converting to human readable.

Re: Convert Time Epoch in Date ,OpenWeather

Posted: Thu Jun 30, 2022 7:39 am
by ixbo
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