#```py

1 messages · Page 1 of 1 (latest)

rugged kiln
#

so back to the original question, how to get epoch from a ISO string (with CPY)? what im trying to do is fetching Adafruit IO data and display it on a pyportal. i read the last feed data for the ISO string, and i want to reload the display 10 minutes after the last data was published (rather than 10 minutes after reset on the pyportal) but it seems like a trivial and common use, but my fundamentals fail me..

#

mktime works..

#

i just need to make a time tuple from the string, without strptime?

devout hull
#

that would give you the time since the date in the string

d = datetime.fromisoformat('2000-01-01T00:30:27')
now = datetime.now()
delta = (now - d).total_seconds()
#

in seconds

#

you add that to time.monotonic() or time.time()

rugged kiln
#

NameError: name 'datetime' is not defined

#

'''

devout hull
#

with from adafruit_datetime import datetime as above

rugged kiln
#

aha:

#

ImportError: no module named 'datetime'

#

how do i do the add code thingy here?

devout hull
#

I mean adafruit_datetime

#

sorry

rugged kiln
#

import adafruit_datetime

d = datetime.fromisoformat('2000-01-01T00:30:27')
now = datetime.now()
delta = (now - d).total_seconds()

#

NameError: name 'datetime' is not defined

devout hull
#
from adafruit_datetime import datetime
d = datetime.fromisoformat('2000-01-01T00:30:27')
now = datetime.now()
delta = (now - d).total_seconds()
rugged kiln
#

sorry neradoc, dont even know what #welcome is..

devout hull
#

that's a channel on discord, click on it !

rugged kiln
#

thats on line 3 btw

#

the import works

#

d=...

devout hull
#

from adafruit_datetime import datetime

rugged kiln
#
code.py output:
Traceback (most recent call last):
  File "code.py", line 3, in <module>
NameError: name 'datetime' is not defined
#

is this a python thing im missing?

#

but shouldnt adafruit_datetime be able to solve my original problem?

#

oops

#

missed the import...

#

Works!
ok, so this is the lesson to be learned is i dont knwo the difference btw

from adafruit_datetime import datetime

and

import adafruit_datetime
```
i thought  it included everything then
#

great, you attacked the original problem too. gonna try it now. but for principles sake:

#

how to get the epoch time?

devout hull
#

well if you want to know if was 10 minutes from the last time you don't need that, you have the delta here, provided you setup the time on the board using NTP, depending on which board it is

#

a datetime object has a timestamp property

#

we usually measure relative time with the time.monotonic() function which gives a time in seconds since boot (it loses precision after many hours because it's a float, but for measuring minutes it's fine), or time.monotonic_ns() which is an int (no loss of precision)

#

when you setup the time, you have time.time() which gives you the epoch as an int

rugged kiln
#

yep. well the data on adafruit io is not updated aat boot time intervals 🙂 so one has to immidiately check what the last timestamp was to sync it. thats what im trying to do.. and time.time()gave me epoch, and AIO gave me the string, so thats a quite common issues i should say

rugged kiln
#

thanks a bunch @devout hull! been sitting for hours now..