Page 1 of 1

[Solved / Workaround] Unix Timestamp

Posted: Mon Feb 04, 2019 9:50 pm
by Redostrike
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?

Re: Unix Timestamp

Posted: Tue Feb 05, 2019 12:56 am
by dhylands
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)

Re: Unix Timestamp

Posted: Tue Feb 05, 2019 7:39 am
by Redostrike
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?

Re: Unix Timestamp

Posted: Tue Feb 05, 2019 6:20 pm
by dhylands
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.

Re: Unix Timestamp

Posted: Tue Feb 05, 2019 7:45 pm
by Redostrike
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.