py3k: datetime resolution / isoformat

G

Guest

When I do:

datetime.datetime.now().isoformat(' ')

I get the time with the microseconds. The docs says:
"if microsecond is 0 YYYY-MM-DDTHH:MM:SS+HH:MM".

How do I set microsecond to 0?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type
'datetime.datetime'

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type
'datetime.datetime'

Do I need to create my own class to extend datetime.datetime?
 
N

Ned Deily

When I do:

datetime.datetime.now().isoformat(' ')

I get the time with the microseconds. The docs says:
"if microsecond is 0 YYYY-MM-DDTHH:MM:SS+HH:MM".

How do I set microsecond to 0?
'2011-02-25T18:48:24'
 
M

MRAB

When I do:

datetime.datetime.now().isoformat(' ')

I get the time with the microseconds. The docs says:
"if microsecond is 0 YYYY-MM-DDTHH:MM:SS+HH:MM".

How do I set microsecond to 0?

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type
'datetime.datetime'


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type
'datetime.datetime'

Do I need to create my own class to extend datetime.datetime?
You could just truncate the result:

datetime.datetime.now().isoformat(' ')[ : 19]
 
C

Chris Rebert

When I do:

   datetime.datetime.now().isoformat(' ')

I get the time with the microseconds. The docs says:
"if microsecond is 0 YYYY-MM-DDTHH:MM:SS+HH:MM".

How do I set microsecond to 0?

    >>> datetime.datetime.microsecond = 0
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   TypeError: can't set attributes of built-in/extension type
'datetime.datetime'

Here you're trying to modify the *class* itself, not an *instance* of
the class like you want to.
However, datetime.datetime instances are immutable anyway. You have to
create a new instance with the desired values, as Ned Deily
demonstrated.

Cheers,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top