Converting given Epoch / Unix timestamp to GMT

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
iotmar
Posts: 8
Joined: Wed Sep 04, 2019 8:56 pm

Converting given Epoch / Unix timestamp to GMT

Post by iotmar » Tue Sep 24, 2019 9:18 pm

Hi,

As the subject indicates, i'm trying to convert a given epoch / unix timestamp to GMT.
I'm grabbing the timestamp from the web in JSON format.

I tried loading the datetime library with upip, but when I run the code, my board resets with a memory allocation failure.
Another attempt was: from datetime import datetime.datetime.fromtimestamp, not an happy outcome either.

So leaving the library for what it is I had a look at the time function, time.time / localtime, it can deal with epoch to utc / gmt but I could not get it past the hardware/rtc time.

I was thinking in the lines of:

Code: Select all

import time
xnow = urlreq.urlopen("http://api.open-notify.org/iss-now.json")
    return json.loads(xnow.read())

isstime = xnow['timestamp']
print(isstime)

named_tuple = isstime() # get struct_time
time_string = time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple)
print(time_string)
It prints nicely the timestamp, but I fail in injecting it the right way it seems. I tried different combo's with different error msg's, would be a long list to type them all down. :-)
So is it possible to convert a Epoch / Unix timestamp in JSON format to GMT? Preferable like Wednesday 25 september 23:15

btw. I'm having fun with the answers given in my first post, i'm able to extract exactly the informaton I want from an API like the open-notify one, only the time conversion is bugging me :-)
Martin

rpr
Posts: 75
Joined: Sat Oct 27, 2018 5:17 pm

Re: Converting given Epoch / Unix timestamp to GMT

Post by rpr » Tue Sep 24, 2019 10:42 pm

From what I see there is no strftime in the libs that come with the default firmware.

http://docs.micropython.org/en/latest/l ... utime.html

However, there is a separate micropython-lib called time which has the strftime which presumably can be installed via upip.

https://github.com/micropython/micropython-lib

https://github.com/micropython/micropyt ... aster/time

I don't have my board at the moment so I cannot try.

Edit to add: Unfortunately the above time library did not work on my ESP32. It may need POSIX port such as Linux etc.

rpr
Posts: 75
Joined: Sat Oct 27, 2018 5:17 pm

Re: Converting given Epoch / Unix timestamp to GMT

Post by rpr » Wed Sep 25, 2019 6:22 am

Code: Select all

# Following returns the time tuple taking into account the epoch start difference 1970 vs 2000
>>> x = time.localtime(isstime)
>>> print("%4d/%02d/%02d %02d:%02d:%02d " % x[:6])
2019/09/25 06:21:06
Not exactly your requested format but you could slice the tuple to achieve your desired print.

iotmar
Posts: 8
Joined: Wed Sep 04, 2019 8:56 pm

Re: Converting given Epoch / Unix timestamp to GMT

Post by iotmar » Wed Sep 25, 2019 3:26 pm

Ah! that is a nice start thanks!

I made this out of it:

Code: Select all

def whenloc():
    datawhen = urlreq.urlopen("http://api.open-notify.org/iss-pass.json?lat=53.0145&lon=6.5862")
    data_when = json.loads(datawhen.read())
    x = time.localtime(data_when["request"]["datetime"])
    print("%4d/%02d/%02d %02d:%02d:%02d " % x[:6])
whenloc()
What gave me the outcome:
2049/09/24 13:56:17

So I think it has to do with the 70 / 2000 difference, to check if it was the timestamp who caused the difference, I used timestamps from
different sources, gave me all the same outcome.
To see if settings on my board would influence the outcome I in en excluded the code below in boot.py, but it did not matter.

Code: Select all

from ntptime import settime
settime()
So for the moment i'm half way :-) What do I need to do to correct it?

Martin

rpr
Posts: 75
Joined: Sat Oct 27, 2018 5:17 pm

Re: Converting given Epoch / Unix timestamp to GMT

Post by rpr » Wed Sep 25, 2019 4:01 pm

Try:

Code: Select all

x = time.localtime(data_when["request"]["datetime"] - 946684800)

iotmar
Posts: 8
Joined: Wed Sep 04, 2019 8:56 pm

Re: Converting given Epoch / Unix timestamp to GMT

Post by iotmar » Wed Sep 25, 2019 4:29 pm

Aaaahh wonderful thank you! :D

Code: Select all

2019/09/25 15:58:22
I had no idea you could manipulate the outcome of the timestamp this way.
Very nice, can work on more passes now :-)

rpr
Posts: 75
Joined: Sat Oct 27, 2018 5:17 pm

Re: Converting given Epoch / Unix timestamp to GMT

Post by rpr » Wed Sep 25, 2019 6:28 pm

iotmar wrote:
Wed Sep 25, 2019 4:29 pm
I had no idea you could manipulate the outcome of the timestamp this way.
With Python, you can do anything. :)

Image

https://xkcd.com/353/

iotmar
Posts: 8
Joined: Wed Sep 04, 2019 8:56 pm

Re: Converting given Epoch / Unix timestamp to GMT

Post by iotmar » Sun Sep 29, 2019 7:53 pm

:D

So going on with the code, I tried to slice the outcome in to sections of year, month day to process it in a "nicely printed format"
like 29 september 2019 - 16:34 hours. My attempts gave (again :) ) some nice error messages, who'm I took to the search engines and read up on things. On those trial and search I stumbled upon another piece of code for MP what I applied to the risetimes (timestamps) it was written for:

Code: Select all

response = urlreq.urlopen("http://api.open-notify.org/iss-pass.json?lat=53.0145&lon=6.5862")
content = json.loads(response.read())
current_year, current_month, current_day, current_hour, current_minute, current_seconds, current_weekday, current_yearday = localtime()

if content['message'] == 'success':
    
    for overhead in content['response']:
        ts = overhead['risetime']
        duration = overhead['duration']
        year, month, day, hour, minute, seconds, weekday, yearday = localtime(ts)
        print("Datum: {}/{}   -   Tijdstip: {}:{} ".format(day, month, hour, minute,))
What gives the output printed: Datum: 28/9 - Tijdstip: 18:32
Nice! but when I tried to use the above code on the first part of the original message under the header "request" I got an error message: TypeError: string indices must be integers, not str
After trying and reading and the answers from here I finally got it working with:

Code: Select all

response = urlreq.urlopen("http://api.open-notify.org/iss-pass.json?lat=53.0145&lon=6.5862")
content = json.loads(response.read())
current_year, current_month, current_day, current_hour, current_minute, current_seconds, current_weekday, current_yearday = localtime()

dt = content['request']['datetime']
year, month, day, hour, minute, seconds, weekday, yearday = localtime(dt)
print('ISS zal weer zichtbaar zijn om:')
print("Datum: {}/{}   -   Tijdstip: {}:{} ".format(day, month, hour, minute,))
print("=====================================")
So what I don't understand completely is what makes it, that I need different code? I assume it's the source JSON (example below) what makes it that I need to use different code. What I did notice is that the "request" section is wrapped in { } and the response in [{ }]
Is the difference because the "datetime" is one result and "risetime" is a list of 5?

Code: Select all

{
  "message": "success", 
  "request": {
    "altitude": 100, 
    "datetime": 1569774892, 
    "latitude": 53.0145, 
    "longitude": 6.5862, 
    "passes": 5
  }, 
  "response": [
    {
      "duration": 639, 
      "risetime": 1569776155
    }, 
    {
      "duration": 648, 
      "risetime": 1569781945
    }, 
    {
      "duration": 635, 
      "risetime": 1569787744
    }, 
    {
      "duration": 537, 
      "risetime": 1569793558
    }, 
    {
      "duration": 512, 
      "risetime": 1569853953
    }
  ]
}
Below is my complete main.py, made from the first answers I got from here till now, so there is some progress :-)

Code: Select all

def computer_position():
    datapos = urlreq.urlopen("http://ip-api.com/json/")
    data_pos = json.loads(datapos.read())
    print("=====================================")
    print("Mijn lokatie is : ", data_pos["city"])
    print("=====================================")
    print("Breedte graad   : ", data_pos["lat"])
    print("Lengte graad    : ", data_pos["lon"])
    print("=====================================")
    print('-------------------------------------')
    return data_pos["lat"], data_pos["lon"]
computer_position()

def iss_position():
    dataiss = urlreq.urlopen("http://api.open-notify.org/iss-now.json")
    data_iss = json.loads(dataiss.read())
    print("=====================================")
    print("Huidige Positie ISS is:")
    print('-------------------------------------')
    print("Breedtegraad (Latitude) ISS:", data_iss["iss_position"]["latitude"])
    print("Lengtegraad (Longitude) ISS:", data_iss["iss_position"]["longitude"])
    print('-------------------------------------')
    print("=====================================")
    return data_iss["iss_position"]["latitude"], data_iss["iss_position"]["longitude"]
iss_position()

response = urlreq.urlopen("http://api.open-notify.org/iss-pass.json?lat=53.0145&lon=6.5862")
content = json.loads(response.read())
current_year, current_month, current_day, current_hour, current_minute, current_seconds, current_weekday, current_yearday = localtime()

dt = content['request']['datetime']
year, month, day, hour, minute, seconds, weekday, yearday = localtime(dt)
print('ISS zal weer zichtbaar zijn om:')
print("Datum: {}/{}   -   Tijdstip: {}:{} ".format(day, month, hour, minute,))
print("=====================================")


response = urlreq.urlopen("http://api.open-notify.org/iss-pass.json?lat=53.0145&lon=6.5862")
content = json.loads(response.read())
current_year, current_month, current_day, current_hour, current_minute, current_seconds, current_weekday, current_yearday = localtime()

if content['message'] == 'success':
    
    for overhead in content['response']:
        ts = overhead['risetime']
        duration = overhead['duration']
        year, month, day, hour, minute, seconds, weekday, yearday = localtime(ts)
        print("Datum: {}/{}   -   Tijdstip: {}:{} ".format(day, month, hour, minute,))
print("=====================================")
print('\n')
Still have some ideas to include and work on, It's nice that you get the longitude and latitude from the ISS, who you could plot on a map, but just like my own hometown gets displayed , I want to see if that is possible with the current position of the ISS. Also next to show all the relevant info on a small(oled) display, I have some neopixels who needs to be driven and do something fancy when the ISS is close to my location :-)

Post Reply