#!/usr/bin/perl -W ################################################################### # secs2dhms # ################################################################### # by A.Greg (15/10/2003) # # scripts at agreg dot com http://agreg.com/scripts/secs2dhms # ################################################################### # This script reads standard input, translates numbers of seconds # # (e.g. "82348221 seconds") into days, hours, minutes and seconds # # and prints the results to stdout. # # # # This can be used, for example, to increase the readability of # # data from svstat (part of Dan Bernstein's daemontools package) # # # # e.g. `svstat /service/* | secs2dhms` # ################################################################### # This program is free software; you can redistribute it and/or # # modify it under the same terms as Perl itself # ################################################################### use strict; while (<>) { s|(\d+) seconds|secs2dhms($1)|eg; print; } sub secs2dhms { my ($data) = $_[0]; return "0 days 0 hours 0 mins $data secs" if $data < 60; my ($days,$hrs_data,$hours,$mins_data,$mins,$secs); $days = floor($data/86400); $hrs_data = $data - ($days*86400); $hours = floor($hrs_data/3600); $mins_data = $hrs_data - ($hours*3600); $mins = floor($mins_data/60); $secs = $mins_data - ($mins*60); return "$days days $hours hours $mins mins $secs secs"; } sub floor { my ($var) = $_[0]; $var =~ s/\.\d+$//g; return $var; }