Guest User

Untitled

a guest
Apr 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. irb(main):001:0> start = Time.now
  2. => Thu Nov 05 01:02:54 -0800 2009
  3.  
  4. irb(main):002:0> Time.now - start
  5. => 25.239
  6.  
  7. irb(main):003:0> (Time.now - start).duration
  8. => "25 seconds"
  9.  
  10. 23 minutes and 35 seconds
  11. 1 hour and 33 minutes
  12. 2 days and 3 hours
  13.  
  14. class Numeric
  15. def duration
  16. secs = self.to_int
  17. mins = secs / 60
  18. hours = mins / 60
  19. days = hours / 24
  20.  
  21. if days > 0
  22. "#{days} days and #{hours % 24} hours"
  23. elsif hours > 0
  24. "#{hours} hours and #{mins % 60} minutes"
  25. elsif mins > 0
  26. "#{mins} minutes and #{secs % 60} seconds"
  27. elsif secs >= 0
  28. "#{secs} seconds"
  29. end
  30. end
  31. end
  32.  
  33. require 'date'
  34.  
  35. start_time = Time.now
  36. end_time = Time.now
  37.  
  38. time_diff = end_time - start_time
  39. hours,minutes,seconds,frac = Date.day_fraction_to_time(time_diff)
  40. puts "Time elapsed: #{hours} hours, #{minutes} minutes and #{seconds} seconds"
  41.  
  42. time_difference = current_time - old_time
  43.  
  44.  
  45.  
  46. def seconds_fraction_to_time(time_difference)
  47. days = hours = mins = 0
  48. mins = (seconds / 60).to_i
  49. seconds = (seconds % 60 ).to_i
  50. hours = (mins / 60).to_i
  51. mins = (mins % 60).to_i
  52. days = (hours / 24).to_i
  53. hours = (hours % 24).to_i
  54. return [days,hours,mins,seconds]
  55. end
  56.  
  57. if(days > 0)
  58. return "#{days} Days #{hours} Hours"
  59. else
  60. return "#{hours} Hours #{mins} Minutes"
  61. end
Add Comment
Please, Sign In to add comment