time module on Thonny/Pico

17 views
Skip to first unread message

Renaud

unread,
Aug 19, 2025, 8:13:44 AMAug 19
to thonny
Hi,
New here so hello to all and thanks to have me.
I'm using Thonny 4.1.7 on a Pico W 2040 on Mac.

When it runs:

import time
current_time = time.time()
print(current_time)
local_time = time.localtime(time.time())
print(local_time)
utc_time = time.gmtime(time.time())
print(utc_time)
secs = time.mktime(time.localtime())
print(secs)

All is well:
1755595433
(2025, 8, 19, 9, 23, 53, 1, 231)
(2025, 8, 19, 9, 23, 53, 1, 231)
1755595433
But:
import time
current_time = time.time()
print(current_time)
local_time = time.localtime(time.time())
print(local_time)
utc_time = time.gmtime(time.time())
print(utc_time)
secs = time.mktime(time.localtime())
print(secs)


print(time.ctime())

gives:
1755595636
(2025, 8, 19, 9, 27, 16, 1, 231)
(2025, 8, 19, 9, 27, 16, 1, 231)
1755595636
Traceback (most recent call last):
  File "<stdin>", line 11, in <module>
AttributeError: 'module' object has no attribute 'ctime'

And a similar answer with:

formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
AttributeError: 'module' object has no attribute 'strftime'

Or "time.strptime" as well.
I rebooted the Mac and reloaded Thonny to noavail.

Any tips there very welcome!
Renaud F5ZR

Paul Naish

unread,
Aug 19, 2025, 9:48:26 AMAug 19
to thonny
You neglected to say which version of uPython you are running

They here is the messages "object has no attribute". The type of object (secs, time) does not have the function/attribute listed. With TIME objects you get into different object types such as TIME.TIME and TIME.DELTATIME

I don't have access to uPython but here is an example in Python

import datetime
import time

now=datetime.datetime.now()
print(now)
print(type(now) )
print(now.strftime("%Y-%m-%d %H:%M:%S") )
print()

later=datetime.datetime.now()
delta=later-now
print(delta)
print(type(delta))
print(delta.strftime("%Y-%m-%d %H:%M:%S") )

When run it produces the following output. Note the error on strftime on delta because there is no strftime on a TIME.DELTATIME object. The difference in reporting details between Python and uPython has to do with the reduced footprint available in uPython.

>>> %Run -c $EDITOR_CONTENT
2025-08-19 09:44:06.451857
<class 'datetime.datetime'>
2025-08-19 09:44:06

0:00:00.005417
<class 'datetime.timedelta'>

Traceback (most recent call last):
  File "<string>", line 14, in <module>
AttributeError: 'datetime.timedelta' object has no attribute 'strftime'
>>> 

Renaud

unread,
Aug 19, 2025, 10:48:34 AMAug 19
to thonny
Hi
Thanks Paul,
Sorry for the lack of Python version:  Thonny 4.1.7 is running Python 3.10.11
I guessed too that my version is limited.
Your example had me curious to try the add of "import datetime" but "no module named "datetime" either.
I suppose  I'll have to do with my version.
Thanks,
Renaud

Paul Naish

unread,
Aug 19, 2025, 2:09:56 PMAug 19
to thonny
That sounds like the Python version of your computer or Thonny and not the uPython version on the PICO firmware.

The point is that your errors are you are invoking functions for the data type that do not exist. You need to search out the uPython doc to find what you need.

Reply all
Reply to author
Forward
0 new messages