Created
May 13, 2009 00:28
-
-
Save itspriddle/110806 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| # 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 | |
| if days.to_f >= 7 | |
| weeks = (days.to_f / 7).floor | |
| days = (days - (weeks * 7)).floor | |
| end | |
| secs = (seconds % 60).floor | |
| out << "#{weeks.to_i} week#{weeks > 1 ? 's' : ''}" unless weeks.nil? or weeks.to_i == 0 | |
| 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