# On Linux, parse /proc/uptime module Sys class Uptime class Error < StandardError; end VERSION = '0.5.1' @@file = '/proc/uptime' # Returns the uptime in seconds # def self.seconds begin IO.readlines(@@file).to_s.split.first.to_i rescue Exception => err raise Error, err end end # Returns the uptime in minutes # def self.minutes self.seconds / 60 end # Returns the uptime in hours # def self.hours self.minutes / 60 end # Returns the uptime in days # def self.days self.hours / 24 end # Returns the uptime as a colon separated string, including days, # hours, minutes and seconds # def self.uptime seconds = self.seconds days = seconds / 86400 seconds -= days * 86400 hours = seconds / 3600 seconds -= hours * 3600 minutes = seconds / 60 seconds -= minutes * 60 "#{days}:#{hours}:#{minutes}:#{seconds}" end # Returns the uptime as a four element array, including days, hours, # minutes and seconds # def self.dhms self.uptime.split(":") end # Returns the time the system was booted as a Time object def self.boot_time Time.now - self.seconds end end end