#!/usr/bin/ruby # # sectotime.rb v1.0 - 2006/11 # # this script converts a time string in seconds into a string with the # representation of the same time in days, hours and minutes. # # Copyright (C) 2006 by Pedro Venda < pjvenda (at) pjvenda org > # # Distributed under the terms of the GNU Public License Agreement (GPLv2) # http://www.fsf.org/licensing/licenses/gpl.html or # http://www.fsf.org/licensing/licenses/gpl.txt # # Usage # ===== # # this script receives its input from stdin and outputs it into stdout # example: $ echo "10000" | ./sectotime.rb # 2h 46m 40s # $ # alternatively, the time input can be given as a command line argument # example: $ ./sectotime.rb 10000 # 2h 46m 40s # finally it can be included as a class 'SecToTime' to instantiate objects # or it can just provide some static methods. # # Changelog # ========= # # v1.0 - 2006/11/20 # - initial (ruby) version, ported from a equivalent written in python # class SecToTime # class version @@VERSION="1.0" # time constants @@MINUTE_SECONDS=60 @@HOUR_SECONDS=@@MINUTE_SECONDS*60 @@DAY_SECONDS=@@HOUR_SECONDS*24 # class variables: # given total_seconds # discrete time calculation @seconds @time # enable read/write access to seconds attribute attr_accessor :seconds # enable read access to time attribute attr_reader :time # constructur - optional argument def initialize(total_seconds=0) @seconds=total_seconds update_time end # updates @time hash according to @seconds def update_time @time=SecToTime.seconds_to_time(@seconds) end # changes internal @seconds variable and updates # @time hash def seconds=(sec) @seconds=sec update_time end # printout def to_s SecToTime.seconds_str(@time) end # # convert a number of seconds into corresponding days, hours, minutes and seconds # this is to be used directly as a static method # def self.seconds_to_time(total_seconds) remain=total_seconds.to_i days=remain/@@DAY_SECONDS remain-=days*@@DAY_SECONDS hours=remain/@@HOUR_SECONDS remain-=hours*@@HOUR_SECONDS minutes=remain/@@MINUTE_SECONDS remain-=minutes*@@MINUTE_SECONDS seconds=remain # returns a hash with the following elements; # days, hours, minutes and seconds {:days => days,:hours => hours,:minutes => minutes,:seconds => seconds} end # # convert and print some amount of time in seconds. # this is to be used directly as a static method # def self.seconds_str(seconds) if seconds.is_a?(Hash) # seconds is already a hash time=seconds else # convert total_seconds in hash with discrete elements time=seconds_to_time(seconds) end # build a string ret="" noblock=false if time[:days]>0 ret+=time[:days].to_s+"d " noblock=true end if time[:hours]>0 || noblock ret+=time[:hours].to_s.rjust(2)+"h " noblock=true end if time[:minutes]>0 || noblock ret+=time[:minutes].to_s.rjust(2)+"m " noblock=true end if time[:seconds]>0 || noblock ret+=time[:seconds].to_s.rjust(2)+"s" noblock=true end ret end end # # stand alone usage: # # total_seconds can be given as argument or from stdin if __FILE__ == $0 if ARGV.length == 0 puts SecToTime.seconds_str(gets.to_i) else puts SecToTime.seconds_str(ARGV[0].to_i) end end