Created
June 30, 2017 11:27
-
-
Save bodsch/eb34a9eb8a58a18faa1508ef6317b444 to your computer and use it in GitHub Desktop.
ruby time_diff problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'date' | |
require 'time_difference' | |
now = DateTime.now.utc | |
last = Time.at( 1506470400000 / 1000 ) | |
puts "now is #{now}. class=#{now.class}" | |
puts "last is #{last}. class=#{last.class}" | |
puts TimeDifference.between(now,last).in_each_component | |
def time_diff(start_time, end_time) | |
seconds_diff = (start_time - end_time).to_i.abs | |
{ | |
years: (seconds_diff / 31556952), | |
month: (seconds_diff / 2629746), | |
weeks: (seconds_diff / 2628288.0).round(0), | |
days: (seconds_diff / 86400), | |
hours: (seconds_diff / 3600), | |
minutes: (seconds_diff / 60), | |
seconds: seconds_diff, | |
} | |
end | |
puts time_diff(now,last) | |
a = Time.new | |
puts a.diff( now, last) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment