date formatting

W

WmGill

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(os.stat('thefile')[ST_MTIME])", but this seems
like a long way around the block. Is there a more direct way?

Bill
 
P

Peter Hansen

WmGill said:
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(os.stat('thefile')[ST_MTIME])", but this seems
like a long way around the block. Is there a more direct way?

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
 
W

WmGill

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 said:
WmGill said:
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(os.stat('thefile')[ST_MTIME])", but this seems
like a long way around the block. Is there a more direct way?

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
 

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

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,436
Latest member
MaxD

Latest Threads

Top