Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > py3k: datetime resolution / isoformat

Reply
Thread Tools

py3k: datetime resolution / isoformat

 
 
spam@uce.gov
Guest
Posts: n/a
 
      02-26-2011

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'


>>> datetime.datetime.resolution = (0, 0, 0)

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?

--
Yves. http://www.SollerS.ca/
http://blog.zioup.org/
 
Reply With Quote
 
 
 
 
Ned Deily
Guest
Posts: n/a
 
      02-26-2011
In article <ssZ9p.10276$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.now().replace(microsecond=0).iso format()

'2011-02-25T18:48:24'

--
Ned Deily,
(E-Mail Removed)

 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      02-26-2011
On 26/02/2011 02:21, (E-Mail Removed) wrote:
>
> 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'
>
>
> >>> datetime.datetime.resolution = (0, 0, 0)

> 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]
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      02-26-2011
On Fri, Feb 25, 2011 at 6:21 PM, <(E-Mail Removed)> wrote:
>
> 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
--
Your "From:" address is annoying.
http://blog.rebertia.com
 
Reply With Quote
 
yves@zioup.com
Guest
Posts: n/a
 
      02-26-2011
On 11-02-25 07:49 PM, Ned Deily wrote:
>>>> datetime.datetime.now().replace(microsecond=0).iso format()

> '2011-02-25T18:48:24'
>


Ah, Thanks!

--
Yves. http://www.SollerS.ca/
http://blog.zioup.org/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [2.4.4] creating a datetime.datetime from an XML xs:dateTime skip@pobox.com Python 2 01-06-2009 01:31 PM
[2.4.4] creating a datetime.datetime from an XML xs:dateTime Martin Python 0 12-27-2008 08:08 PM
mx.DateTime to datetime.datetime mp Python 1 07-28-2006 10:57 PM
datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Christos TZOTZIOY Georgiou Python 3 09-13-2003 10:44 AM
RE: datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Tim Peters Python 0 09-09-2003 12:57 AM



Advertisments