datetime: print as seconds
I have a datetime
object. I want to print it as just number of seconds (i.e., 1 min 30.5 sec should print as 90.5 s). Can't seem to find a way to do it wi开发者_如何学运维th strftime
.
I think that for your situation you'd be better off using a datetime.timedelta object. It has a function that will do exactly what you want, datetime.timedelta.total_seconds().
Datetimes are instants in time, unlike durations which are just numbers. The reason strftime
won't say "90.5s" is that, well, that is a duration and not a datetime.
Does your datetime object represent the instant that is 90.5 seconds past the epoch? If so, just get the epoch time in millis and divide by 1000, concat an "s" and you are good to go.
Also, for fun, see http://en.wikipedia.org/wiki/ISO_8601#Durations.
精彩评论