[Solved / Workaround] Unix Timestamp

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Redostrike
Posts: 6
Joined: Mon Feb 04, 2019 9:37 pm

[Solved / Workaround] Unix Timestamp

Post by Redostrike » Mon Feb 04, 2019 9:50 pm

Hi,

I'm pretty new to programming python. For an api call i'm in need of an unix timestamp. I've figured out i can use ntptime to get the right time registered on my esp8266.


[code]import time
from ntptime import settime
settime()
print(time.time())[/code]

Now if i print time.time() i'm seeing diffrent results than i should get. Somewhere in the range of 60200000 (60 mil) but the real unix timestamp should be something in te 1540000000 (1.540 bil) What am i missing doing wrong?
Last edited by Redostrike on Tue Feb 05, 2019 9:37 pm, edited 4 times in total.

Online
User avatar
dhylands
Posts: 3014
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Unix Timestamp

Post by dhylands » Tue Feb 05, 2019 12:56 am

linux uses an epoch of Jan 1, 1970. Some of the micropython bare metal ports use an epoch of Jan 1, 2000.

Furthermore, the pyboard maintains its time in localtime, but the ESP32 and linux ports maintain their time as GMT.

You can determine the epoch being used by your board by doing:

Code: Select all

import time
time.gmtime(0)

Redostrike
Posts: 6
Joined: Mon Feb 04, 2019 9:37 pm

Re: Unix Timestamp

Post by Redostrike » Tue Feb 05, 2019 7:39 am

Not sure what happend to this topic. :?: :?: :?:

Anyways. When i try using your code i'm getting an error:

AttributeError: 'module' object has no attribute 'gmtime'

It seems gmtime is not implemented?

Online
User avatar
dhylands
Posts: 3014
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Unix Timestamp

Post by dhylands » Tue Feb 05, 2019 6:20 pm

Redostrike wrote:
Tue Feb 05, 2019 7:39 am
Not sure what happend to this topic. :?: :?: :?:

Anyways. When i try using your code i'm getting an error:

AttributeError: 'module' object has no attribute 'gmtime'

It seems gmtime is not implemented?
I took a look at rshell again and you're right. It uses a try/except block because gmtime isn't implemented. Using time.localtime(0) should work if gmtime isn't implemented. Using localtime(0) will give incorrect results if timezones are supported, but for those environments time.gmtime(0) should be implemented.

Redostrike
Posts: 6
Joined: Mon Feb 04, 2019 9:37 pm

Re: Unix Timestamp

Post by Redostrike » Tue Feb 05, 2019 7:45 pm

Thanks but that does not really solve my problem. I'm looking for an Unix timestamp.

More info here https://www.unixtimestamp.com/

time.localtime(0) returns a UTC format not an unix timestamp format. Unix timestamp is actually just the seconds till a given date starting from 01-01-1970 00:00.

So what i need is something in the ballpark of 1556829480

CPhyton retuns a float for this around this number by doing time.time()

Alas microphyton returns somewhere in the ballpark of 602710877. It seems that Microphyton does not start counting from 01-01-1970. I think it starts counting from 01-01-2000 00:00 which is giving me wrong results for my api call.

EDIT:
Ah browsing the docs i found what i was afraid of:

Time Epoch: Unix port uses standard for POSIX systems epoch of 1970-01-01 00:00:00 UTC. However, embedded ports use epoch of 2000-01-01 00:00:00 UTC.

Now that i know this info i can just add 946,684,800 seconds to the time.time() result to get my desired number.

Post Reply