Skip to content

Instantly share code, notes, and snippets.

@itspriddle
Created March 20, 2009 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itspriddle/82187 to your computer and use it in GitHub Desktop.
Save itspriddle/82187 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# Tested on OS X Leopard only but should work
# for BSDs
start = /\{ sec = ([0-9]+),/.match(`sysctl -n kern.boottime`).captures[0]
uptime_secs = (Time.now.to_f - start.to_f).round
def seconds_to_time(seconds)
out = []
mins = (seconds.to_f / 60.0).floor
if mins.to_f >= 60
hours = (mins.to_f / 60).floor
mins = (mins.to_f - (hours.to_f * 60.0))
end
if hours.to_f >= 24
days = (hours.to_f / 24.0).floor
hours = (hours.to_f % 24.0).floor
end
secs = (seconds % 60).floor
out << "#{days.to_i} day#{days > 1 ? 's' : ''}" unless days.nil? or days.to_i == 0
out << "#{hours.to_i} hour#{hours > 1 ? 's' : ''}" unless hours.nil? or hours.to_i == 0
out << "#{mins.to_i} minute#{mins > 1 ? 's' : ''}" unless mins.nil? or mins.to_i == 0
out.join(', ')
end
puts seconds_to_time(uptime_secs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment