py3k: datetime resolution / isoformat

Discussion in 'Python' started by spam@uce.gov, Feb 26, 2011.

  1. 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?

    >>> 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/
     
    , Feb 26, 2011
    #1
    1. Advertising

  2. Ned Deily Guest

    In article <ssZ9p.10276$>, 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).isoformat()

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

    --
    Ned Deily,
     
    Ned Deily, Feb 26, 2011
    #2
    1. Advertising

  3. MRAB Guest

    On 26/02/2011 02:21, 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]
     
    MRAB, Feb 26, 2011
    #3
  4. Chris Rebert Guest

    On Fri, Feb 25, 2011 at 6:21 PM, <> 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
     
    Chris Rebert, Feb 26, 2011
    #4
  5. Guest

    , Feb 26, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    731
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    554
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    402
    John Machin
    Jul 28, 2006
  4. Travis Griggs
    Replies:
    0
    Views:
    68
    Travis Griggs
    Apr 23, 2014
  5. MRAB
    Replies:
    0
    Views:
    61
Loading...

Share This Page