Skip to content

Simple time add in ruby

09/02/2011

this program for simple time adder.

class Timer
def time_calculation(time,addtime)
min_in_sec=(time[2].to_i+addtime[2].to_i)/60
cur_sec=(time[2].to_i+addtime[2].to_i)%60
hor_in_min=(time[1].to_i+addtime[1].to_i+min_in_sec)/60
cur_min=(time[1].to_i+addtime[1].to_i+min_in_sec)%60
cur_hor=(time[0].to_i+addtime[0].to_i+hor_in_min)%12
if cur_hor==0
cur_hor=12
end
print "time now:",cur_hor,":",cur_min,":",cur_sec,"\n"
end
end
print "Enter current time(H:M:S):"
time=gets.chomp.split(':')
print "Enter time to add(H:M:S):"
addtime=gets.chomp.split(':')
a=Timer.new()
a.time_calculation(time,addtime)

output:

Advertisements

From → Ruby

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: