Converting seconds to hrs:min:seconds

From MyWiki

Jump to: navigation, search

Learning Python and at the same time messing around with Amanda's amreport JSON output, which we are trying to feed into Logstash/Kibana to build some Zabbix alerts out of it and some reporting from the logs.

JSON output has backup run time encoded in seconds and I wanted to convert it into hrs:min:sec and nearly had it when some long running backup revealed a missing magic :-)

>>> run_time=5814.286
>>> print "Time: %02d:%02d:%02d" % (int(run_time/3600), int(run_time/60), int(run_time%60))
Time: 01:96:54

Hmm... one hour and ninety six minutes...

Thanks to the Actor's post who unlocked my stalled brain today :-)

Seconds = InSeconds % SECONDS_IN_MINUTE ;
InMinutes = InSeconds / SECONDS_IN_MINUTE ;

Minutes = InMinutes % MINUTES_IN_HOUR ;
InHours = InMinutes / MINUTES_IN_HOUR ;

Hours = InHours % HOURS_IN_DAY ;
Days = InHours / HOURS_IN_DAY ;

Of course, %60 for minutes is missing :-)

Now this is more like it:

>>> print "Time: %02d:%02d:%02d" % (int(run_time/3600), int(run_time/60)%60, int(run_time%60))
Time: 01:36:54
Personal tools