[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] Uptime Since... (#174

Matthew Moss

8/23/2008 2:36:00 PM

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Quiz 2:

1. Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have passed from the time on this message.

2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
permanent, new website is in the works for Ruby Quiz 2. Until then,
please visit the temporary website at

<http://splatbang.com/rub....

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion. Please reply to
the original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## Uptime Since... (#174)


Nice and easy one this week. Your task is to write a Ruby script that
reports the date and time of your last reboot, making use of the
`uptime` command.

While `uptime` is available on the various Unixes and Mac OS X,
Windows users might need to do a little extra work. Here are [two][1]
[options][2] for Windows users.


[1]: http://support.microsoft.com...
[2]: http://articles.techrepublic.com.com/5100-10878_11-58...

--
Matthew Moss <matthew.moss@gmail.com>

24 Answers

Matthew Moss

8/25/2008 2:45:00 PM

0

My own solution:


require 'date'

(now, _, dd, _, _, dhm) = `uptime`.split(/ +|,/)
(dd, dh, dm) = dd.to_i, *dhm.split(':').map { |x| x.to_i }

dh += (dm / 60.0)
dd += (dh / 24.0)

last = DateTime.parse(now) - dd

puts "Last reboot: #{last.year} #{Date::ABBR_MONTHNAMES[last.mon]}
#{last.day} at #{last.hour}:#{last.min}"



Gordon Thiesfeld

8/25/2008 4:05:00 PM

0

On Sat, Aug 23, 2008 at 9:35 AM, Matthew Moss <matthew.moss@gmail.com> wrote:
> ## Uptime Since... (#174)
>

# A windows solution.

require 'rubygems'
require 'ruby-wmi'
require 'date'

str = WMI::Win32_OperatingSystem.find(:first).LastBootupTime

boot = DateTime.strptime(str, "%Y%m%d%H%M")

boot_date = boot.strftime("%Y %b")
boot_time = boot.strftime("%I:%M")

puts "Last reboot: #{boot_date} at #{boot_time}"

Martin Bryce

8/25/2008 4:22:00 PM

0

And my try at this:

coeff = [60,3600]; i = 0; offset = 0
s = %x{uptime}
coeff.push 86400 if s =~ /day/

s.scan(/\d+/) do #I don't know how to stop scanning when I've had
enough ^^;;
|text|
i = i + 1
offset = offset + text.to_i*(coeff.pop||0) if (3..5) === i
end

puts (Time.now - offset).strftime("System booted at %H:%M of %A, %B %d
(%Y)")



(I don't know anything about regexp, and it shows)
--
Posted via http://www.ruby-....

Eric I.

8/25/2008 4:33:00 PM

0

My solution:

====

SecsPerMinute = 60
SecsPerHour = 60*SecsPerMinute
SecsPerDay = 24*SecsPerHour

match = /up (\d+) days, (\d+):(\d+),/.match %x{uptime}

# extract data from reg. exp. and convert to integers
days, hours, minutes = *((1..3).map { |index| match[index].to_i })

now = Time.now
now -= now.sec # normalize to 0 seconds into the current minute

boot_time = now - days*SecsPerDay - hours*SecsPerHour -
minutes*SecsPerMinute
boot_time_s = boot_time.strftime("%a %b %e %l:%M %p")

puts "Machine last booted: #{boot_time_s} (+/- 1 minute)."

====

Eric

====

On-site, hands-on Ruby and Ruby on Rails training is
available from http://Lea... !

Nathan Powell

8/25/2008 4:41:00 PM

0

Mine (Only tested on a couple of Linux boxes):


if `uptime`.include? "day"
puts(Time.now - ((`uptime`.split[2].to_i * 1440) + (`uptime`.split[4].split(":")[0].to_i * 60) + (`uptime`.split[4].split(":")[1].to_i)) * 60)
elsif `uptime`.include? "min"
puts(Time.now - `uptime`.split[2].split(":")[0].to_i * 60)
else
puts(Time.now - (`uptime`.split[2].split(":")[0].to_i * 60) + (`uptime`.split[2].split(":")[1].to_i))
end

On Tue, Aug 26, 2008 at 01:30:50AM +0900, Eric I. wrote:
> My solution:
>
> ====
>
> SecsPerMinute = 60
> SecsPerHour = 60*SecsPerMinute
> SecsPerDay = 24*SecsPerHour
>
> match = /up (\d+) days, (\d+):(\d+),/.match %x{uptime}
>
> # extract data from reg. exp. and convert to integers
> days, hours, minutes = *((1..3).map { |index| match[index].to_i })
>
> now = Time.now
> now -= now.sec # normalize to 0 seconds into the current minute
>
> boot_time = now - days*SecsPerDay - hours*SecsPerHour -
> minutes*SecsPerMinute
> boot_time_s = boot_time.strftime("%a %b %e %l:%M %p")
>
> puts "Machine last booted: #{boot_time_s} (+/- 1 minute)."
>
> ====
>
> Eric
>
> ====
>
> On-site, hands-on Ruby and Ruby on Rails training is
> available from http://Lea... !
>

--
nathan
nathan_at_nathanpowell_dot_org

To give anything less than your best is to sacrifice the gift.
~ Steve Prefontaine
------------------------------------

Jesús Gabriel y Galán

8/25/2008 5:27:00 PM

0

On Sat, Aug 23, 2008 at 4:35 PM, Matthew Moss <matthew.moss@gmail.com> wrote:
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> The three rules of Ruby Quiz 2:
>
> 1. Please do not post any solutions or spoiler discussion for this
> quiz until 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
> permanent, new website is in the works for Ruby Quiz 2. Until then,
> please visit the temporary website at
>
> <http://splatbang.com/rub....
>
> 3. Enjoy!
>
> Suggestion: A [QUIZ] in the subject of emails about the problem
> helps everyone on Ruby Talk follow the discussion. Please reply to
> the original quiz message, if you can.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> ## Uptime Since... (#174)
>
>
> Nice and easy one this week. Your task is to write a Ruby script that
> reports the date and time of your last reboot, making use of the
> `uptime` command.

My solution, tested in Ubuntu 8.04 and Fedora Core 2:

uptime = (`uptime`.match /up (.*),.*user/)[1].delete(" ")
captures = (uptime.match /((\d+)days,)?(\d+):(\d+)/).captures[1..-1]
elapsed_seconds = captures.zip([86440, 3600, 60]).inject(0) do |total, (x,y)|
total + (x.nil? ? 0 : x.to_i * y)
end
puts "Last reboot was on #{Time.now - elapsed_seconds}"

Nice quiz !!!

Jesus.

Jesse Merriman

8/25/2008 9:41:00 PM

0

Here's a one-liner using /proc/uptime:

puts Time.now - File.read('/proc/uptime').match(/^(\d+\.\d+) /)[1].to_f

--
Jesse Merriman


Daniel Berger

8/25/2008 10:07:00 PM

0



On Aug 23, 8:35 am, "Matthew Moss" <matthew.m...@gmail.com> wrote:
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> The three rules of Ruby Quiz 2:
>
> 1. Please do not post any solutions or spoiler discussion for this
> quiz until 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
> permanent, new website is in the works for Ruby Quiz 2. Until then,
> please visit the temporary website at
>
> <http://splatbang.com/rub....
>
> 3. Enjoy!
>
> Suggestion: A [QUIZ] in the subject of emails about the problem
> helps everyone on Ruby Talk follow the discussion. Please reply to
> the original quiz message, if you can.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> ## Uptime Since... (#174)
>
> Nice and easy one this week. Your task is to write a Ruby script that
> reports the date and time of your last reboot, making use of the
> `uptime` command.

require 'sys/uptime'
p Sys::Uptime.uptime

>:)

Regards,

Dan

PS - No parsing involved. Uses sysctl/times/wmi, depending on platform.

Erik Hollensbe

8/25/2008 10:12:00 PM

0

Matthew Moss wrote:

> Nice and easy one this week. Your task is to write a Ruby script that
> reports the date and time of your last reboot, making use of the
> `uptime` command.
>
> While `uptime` is available on the various Unixes and Mac OS X,
> Windows users might need to do a little extra work. Here are [two][1]
> [options][2] for Windows users.

last | grep reboot | head -1

Am I doing it right? :P

-Erik
--
Posted via http://www.ruby-....

Michael Guterl

8/25/2008 11:21:00 PM

0

On Mon, Aug 25, 2008 at 6:12 PM, Erik Hollensbe <erik@hollensbe.org> wrote:
> Matthew Moss wrote:
>
>> Nice and easy one this week. Your task is to write a Ruby script that
>> reports the date and time of your last reboot, making use of the
>> `uptime` command.
>>
>> While `uptime` is available on the various Unixes and Mac OS X,
>> Windows users might need to do a little extra work. Here are [two][1]
>> [options][2] for Windows users.
>
> last | grep reboot | head -1
>
> Am I doing it right? :P
>

This works for me on OS X.

require 'time'
Time.parse `last | tail -1`

Michael Guterl