Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in time.localtime(t) CircuitPython? #3364

Open
powersoft opened this issue Sep 1, 2020 · 4 comments
Open

Bug in time.localtime(t) CircuitPython? #3364

powersoft opened this issue Sep 1, 2020 · 4 comments

Comments

@powersoft
Copy link

powersoft commented Sep 1, 2020

I wont to convert a unixtimestamp to localtime in circuit python.
This is the simple code:

unix_correction = 946684800 # correct to 01-01-2000
timezone = 7200
t=1598887049

def convertUnixTime(t,timezone):
dummy=time.localtime(t-unix_correction+timezone)
return "%4d-%02d-%02d %02d:%02d" % (dummy[0],dummy[1],dummy[2],dummy[3],dummy[4])

Running it I get the error message:

"timestamp out of range for platform time_t"

could not found a solution for this problem (it is working well in micropython!)

thank for any help.
cheers,
Jan

@tannewt tannewt added this to the Long term milestone Sep 1, 2020
@nk521
Copy link

nk521 commented Sep 4, 2020

This code works in micropython because there is no inbuilt "unix epoch correction" there.
time_localtime calls timeutils_seconds_since_2000_to_struct_time compare this with circuitpython's timeutils timeutils_seconds_since_epoch_to_struct_time function which gets called during localtime

void timeutils_seconds_since_epoch_to_struct_time(mp_uint_t t, timeutils_struct_time_t *tm) {
    t -= EPOCH1970_EPOCH2000_DIFF_SECS;
    timeutils_seconds_since_2000_to_struct_time(t, tm);
}

second line does what you're trying to do. So you can remove unix_correction from your code and it will work fine.

@garethhcoleman
Copy link

garethhcoleman commented Feb 27, 2021

@powersoft are you happy that the explanation / solution given closes this issue?

@adafruit-adabot adafruit-adabot added the Hacktoberfest Beginner friendly issues for Hacktoberfest event label Oct 26, 2021
@FoamyGuy FoamyGuy removed the Hacktoberfest Beginner friendly issues for Hacktoberfest event label Nov 8, 2021
@nk521
Copy link

nk521 commented Mar 30, 2022

I just noticed that this issue is still open. Can we close this issue as there's no activity going on here.

@DJDevon3
Copy link

DJDevon3 commented Aug 17, 2022

This is no longer an issue. You can easily convert Unix epoch timestamps and timezone offset manually with something like this:

import time
def _format_datetime(datetime):
    return "{:02}/{:02}/{} {:02}:{:02}:{:02}".format(
        datetime.tm_mon,
        datetime.tm_mday,
        datetime.tm_year,
        datetime.tm_hour,
        datetime.tm_min,
        datetime.tm_sec,
    )

unix_time = 1660764970 # Wed Aug 17 2022 19:36:10 GMT+0000
tz_offset_seconds = -14400  # NY Timezone

get_timestamp = int(unix_time + tz_offset_seconds)
current_unix_time = time.localtime(get_timestamp)
current_struct_time = time.struct_time(current_unix_time)
current_date = "{}".format(_format_datetime(current_struct_time))

print("Timestamp:", current_date)

code.py output:
Timestamp: 08/17/2022 15:36:10

Just because the syntax doesn’t work as it does in micropython doesn’t necessarily mean it’s a bug. Trying to help plug holes with stuff tagged as good first issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants