How to Convert 270921Sec into Days + Hours + Minutes + Sec ? - Ruby

how to convert 270921sec into days + hours + minutes + sec ? (ruby)

It can be done pretty concisely using divmod:

t = 270921
mm, ss = t.divmod(60) #=> [4515, 21]
hh, mm = mm.divmod(60) #=> [75, 15]
dd, hh = hh.divmod(24) #=> [3, 3]
puts "%d days, %d hours, %d minutes and %d seconds" % [dd, hh, mm, ss]
#=> 3 days, 3 hours, 15 minutes and 21 seconds

You could probably DRY it further by getting creative with collect, or maybe inject, but when the core logic is three lines it may be overkill.

Dividing seconds into hours, minutes and seconds

You could do it like this:

seconds = 219

hours = seconds / 3600
seconds -= hours * 3600

minutes = seconds / 60
seconds -= minutes * 60

puts "#{hours} hours, #{minutes} minutes and #{seconds} seconds"

Rails - Get the time difference in hours, minutes and seconds

You can try with this:

def time_diff(start_time, end_time)
seconds_diff = (start_time - end_time).to_i.abs

hours = seconds_diff / 3600
seconds_diff -= hours * 3600

minutes = seconds_diff / 60
seconds_diff -= minutes * 60

seconds = seconds_diff

"#{hours.to_s.rjust(2, '0')}:#{minutes.to_s.rjust(2, '0')}:#{seconds.to_s.rjust(2, '0')}"
# or, as hagello suggested in the comments:
# '%02d:%02d:%02d' % [hours, minutes, seconds]
end

And use it:

time_diff(Time.now, Time.now-2.days-3.hours-4.minutes-5.seconds)
# => "51:04:04"

Convert duration in seconds to hours and minutes and skip empty values

Why not just something simple like this:

def to_hms(time)
hours = time / 3600
minutes = (time / 60) % 60

if (hours > 0 and minutes > 0)
'%d h %d m' % [ hours, minutes ]
elsif (hours > 0)
'%d h' % hours
elsif (minutes > 0)
'%d m' % minutes
end
end

Where this produces the desired results:

to_hms(63000)
# => "17 h 30 m"
to_hms(28800)
# => "8 h"
to_hms(1800)
# => "30 m"

Calculate character string days, hours, minutes, seconds to numeric total days

again, without packages and a little regex

Time <- c(
"22 hours 3 minutes 22 seconds",
"170 hours 15 minutes 20 seconds",
"39 seconds",
"6 hours 44 minutes 17 seconds",
"9 hours 54 minutes 36 seconds",
"357 hours 23 minutes 28 seconds",
"464 hours 30 minutes 7 seconds",
"51 seconds",
"31 hours 39 minutes 2 seconds",
"355 hours 29 minutes 10 seconds")

pat <- '(?:(\\d+) hours )?(?:(\\d+) minutes )?(?:(\\d+) seconds)?'
m <- regexpr(pat, Time, perl = TRUE)

m_st <- attr(m, 'capture.start')
m_ln <- attr(m, 'capture.length')

(mm <- mapply(function(x, y) as.numeric(substr(Time, x, y)),
data.frame(m_st), data.frame(m_st + m_ln - 1)))

(dd <- setNames(data.frame(mm), c('h','m','s')))
# h m s
# 1 22 3 22
# 2 170 15 20
# 3 NA NA 39
# 4 6 44 17
# 5 9 54 36
# 6 357 23 28
# 7 464 30 7
# 8 NA NA 51
# 9 31 39 2
# 10 355 29 10

round(rowSums(dd / data.frame(h = rep(24, nrow(dd)), m = 24 * 60, s = 24 * 60 * 60),
na.rm = TRUE), 3)
# [1] 0.919 7.094 0.000 0.281 0.413 14.891 19.354 0.001 1.319 14.812

Ruby: Convert time to seconds?

You can use DateTime#parse to turn a string into a DateTime object, and then multiply the hour by 3600 and the minute by 60 to get the number of seconds:

require 'date'

# DateTime.parse throws ArgumentError if it can't parse the string
if dt = DateTime.parse("10:30") rescue false
seconds = dt.hour * 3600 + dt.min * 60 #=> 37800
end

As jleedev pointed out in the comments, you could also use Time#seconds_since_midnight if you have ActiveSupport:

require 'active_support'
Time.parse("10:30").seconds_since_midnight #=> 37800.0

Convert seconds to days, minutes, and hours in Obj-c

In this case, you simply need to divide.

days = num_seconds / (60 * 60 * 24);
num_seconds -= days * (60 * 60 * 24);
hours = num_seconds / (60 * 60);
num_seconds -= hours * (60 * 60);
minutes = num_seconds / 60;

For more sophisticated date calculations, such as the number of days within the ten million seconds after 3pm on January 19th in 1983, you would use the NSCalendar class along with NSDateComponents. Apple's date and time programming guide helps you here.



Related Topics



Leave a reply



Submit