date formatting

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

    date formatting

    I want to write a script that Among other things) renames a file based on
    it's timestamp.
    I can get the date info using
    "strftime('%y%m %d',localtime(o s.stat('thefile ')[ST_MTIME])", but this seems
    like a long way around the block. Is there a more direct way?

    Bill


  • Peter Hansen

    #2
    Re: date formatting

    WmGill wrote:[color=blue]
    >
    > I want to write a script that Among other things) renames a file based on
    > it's timestamp.
    > I can get the date info using
    > "strftime('%y%m %d',localtime(o s.stat('thefile ')[ST_MTIME])", but this seems
    > like a long way around the block. Is there a more direct way?[/color]

    More direct way for what? os.stat() directly returns the time in seconds,
    localtime() converts it to a nice tuple with year, month, day, etc., and
    strftime() is a handy way to turn that into a string based on various
    components.

    If you don't want a string, you could of course stop before calling stftime(),
    so that would be more direct. If you are certain you only want the year
    month and day, you could skip the strftime() call and just do a % formatting
    with '%04d%02d%02d' % localtime()[:3], but that's slightly less readable.

    -Peter

    Comment

    • WmGill

      #3
      Re: date formatting

      No. I just thought there was a function to convert "time in seconds"
      directly to "formatted string". Maybe it was some other language.

      Thanks,
      Bill

      "Peter Hansen" <peter@engcorp. com> wrote in message
      news:3F621367.3 5F22F4E@engcorp .com...[color=blue]
      > WmGill wrote:[color=green]
      > >
      > > I want to write a script that Among other things) renames a file based[/color][/color]
      on[color=blue][color=green]
      > > it's timestamp.
      > > I can get the date info using
      > > "strftime('%y%m %d',localtime(o s.stat('thefile ')[ST_MTIME])", but this[/color][/color]
      seems[color=blue][color=green]
      > > like a long way around the block. Is there a more direct way?[/color]
      >
      > More direct way for what? os.stat() directly returns the time in[/color]
      seconds,[color=blue]
      > localtime() converts it to a nice tuple with year, month, day, etc., and
      > strftime() is a handy way to turn that into a string based on various
      > components.
      >
      > If you don't want a string, you could of course stop before calling[/color]
      stftime(),[color=blue]
      > so that would be more direct. If you are certain you only want the year
      > month and day, you could skip the strftime() call and just do a %[/color]
      formatting[color=blue]
      > with '%04d%02d%02d' % localtime()[:3], but that's slightly less readable.
      >
      > -Peter[/color]


      Comment

      Working...