Le 23 aoû¹ 2008 16:35, Matthew Moss a ñÄrit :
A solution tested with windows uptime.exe, FreeBSD and Linux. It should
work with most of the others and in most of the edge cases, I think.
#! /usr/local/bin/ruby
def startup(t)
u = (t.downcase.match(/up(.*?)(?:\d+ user.*)?$/)[1])
lu = u.split(/([0-9:]+)/).map { |e| e.strip.gsub(/,/, '') }.reject { |e| e == '' }
count = 0
lu.each_index do |i|
count += case lu[i]
when /(\d+):(\d+)(?::(\d+))?/
$1.to_i * 3600 + $2.to_i * 60 + ($3 ? $3.to_i : 0)
when /(\d+)/
n = lu[i].to_i
case lu[i + 1]
when nil then raise "Unknown format"
when /^day/ then n * 86400
when /^hour/ then n * 3600
when /^min/ then n * 60
when /^sec/ then n
else raise "Unknown modifier (#{lu[i + 1]})"
end
else
0
end
end
Time.now - count
end
[ `uptime`,
'11:40:08 up 15 days, 16:08, 1 user,2 load average: 0.06, 0.04, 0.01',
'\\LAPHROAIG has been up for: 25 day(s), 13 hour(s), 46 minute(s), 10 second(s)'
].each { |c| puts startup(c).strftime('%c') }
12:09 fred@balvenie:~/ruby> ./uptime.rb
Wed Jun 25 07:05:41 2008
Sun Aug 10 20:01:41 2008
Thu Jul 31 22:23:31 2008
Fred
--
Tried to save a place from the cuts and the scratches
Tried to overcome the complications and the catches
Nothing ever grows and the sun doesn't shine all day (Nine Inch Nails,
Tried to save myself but myself keeps slipping away Into the Void)