Time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonas Galvez

    Time

    If I have a tuple like this:

    (year, month, day, hour, minute)

    Is there a way to automatically generate a string like this?

    "YYYY-MM-DDTHH:MM:00"

    I remember seeing something about it somewhere... wanted to be sure.



    Thanks in advance,



    =-
    Jonas Galvez
    jonasgalvez.com/blog
    macromedia.com/go/team





  • William Park

    #2
    Re: Time

    Jonas Galvez <jonas@jonasgal vez.com> wrote:[color=blue]
    > If I have a tuple like this:
    >
    > (year, month, day, hour, minute)
    >
    > Is there a way to automatically generate a string like this?
    >
    > "YYYY-MM-DDTHH:MM:00"
    >
    > I remember seeing something about it somewhere... wanted to be sure.[/color]

    Hint:
    '%02d' % 4

    --
    William Park, Open Geometry Consulting, <opengeometry@y ahoo.ca>
    Linux solution/training/migration, Thin-client

    Comment

    • Peter Hansen

      #3
      Re: Time

      Jonas Galvez wrote:
      [color=blue]
      > If I have a tuple like this:
      >
      > (year, month, day, hour, minute)
      >
      > Is there a way to automatically generate a string like this?
      >
      > "YYYY-MM-DDTHH:MM:00"
      >
      > I remember seeing something about it somewhere... wanted to be sure.[/color]

      You want to read about the "time" module:
      [color=blue][color=green][color=darkred]
      >>> t = (2004, 4, 21, 15, 33)
      >>> import time
      >>> time.strftime(' %Y-%m-%dT%H:%M:00', t+(0,0,0,0))[/color][/color][/color]
      '2004-04-21T15:33:00'

      I left the "T" in there as a literal because I don't know what you
      meant it to be and there's no obvious single-character substitution.

      -Peter

      Comment

      • Jonas Galvez

        #4
        Re: Time

        Thanks all.
        [color=blue]
        > [Mike Fletcher]
        > That format is the ISO standard date-time format. The modern way
        > to work with date-times in Python is to use the datetime module,
        > so:
        >[color=green][color=darkred]
        > >>> import datetime
        > >>> d = datetime.dateti me( 2004, 4, 21, 15, 05)
        > >>> d.isoformat()[/color][/color]
        > '2004-04-21T15:05:00'
        >
        > You can use datetime.dateti me( * mytuple ) to auto-unpack your
        > tuple, btw.[/color]

        Cool, that's what I was looking for.
        [color=blue]
        > [Peter Hansen]
        > You want to read about the "time" module:
        >[color=green][color=darkred]
        > >>> t = (2004, 4, 21, 15, 33)
        > >>> import time
        > >>> time.strftime(' %Y-%m-%dT%H:%M:00', t+(0,0,0,0))[/color][/color]
        > '2004-04-21T15:33:00'
        >
        > I left the "T" in there as a literal because I don't know what you
        > meant it to be and there's no obvious single-character
        > substitution.[/color]

        The "T" is from the W3C format:


        I need it to generate valid RSS/ATOM feeds.



        =-
        Jonas Galvez
        jonasgalvez.com/blog
        macromedia.com/go/team





        Comment

        Working...