October 19, 2009

Measure your working time on windows

0  comments

For measure working time on windows I wrote a little script. It calculates the time since the last login. The output of the script looks like this:

Login:        10:10
Now:          18:13
======================
Working time: 8 h 3 m

The script is written in Ruby and runs under cygwin. I saved the script in a folder tools in my homedir and have the following alias in my .bashrc:

alias wt='~/tools/workingtime.rb'

So finally the script looks like this:

#!/usr/bin/ruby
HOURS_PER_DAY = 24
MINUTES_PER_HOUR = 60
SEC_PER_MINUTE = 60 
times = []

#get times from windows systeminfo
#Systembetriebszeit:                     0 Tage, 0 Stunden, 10 Minuten, 0 Sekunden
uptime_string = `systeminfo | grep Systembetriebszeit`
uptime_string.slice!(0..28)
time_strings = uptime_string.split(',')
time_strings.each do |s|
times < < s.gsub(/[A-Za-z\s]+/, '')
end

#do some calculation
seconds = times[0].to_i *  HOURS_PER_DAY * MINUTES_PER_HOUR * SEC_PER_MINUTE
seconds = seconds + times[1].to_i * MINUTES_PER_HOUR * SEC_PER_MINUTE
seconds = seconds + times[2].to_i * SEC_PER_MINUTE
seconds = seconds + times[3].to_i
now = Time.now
login = now - seconds
if (times[0].to_i != 0)
  times[1] = times[1].to_i + times[0] * HOURS_PER_DAY  	
end

#print it
puts login.strftime("Login:        %H:%M")
puts now.strftime("Now:          %H:%M")
puts "======================"
puts "Working time: #{times[1].to_i} h #{times[2].to_i} m"

Known issues:

The script is based on the windows command systeminfo. Systeminfo returns values in the language of the operating system. So this works only with a german windows. Replace "Systembetriebszeit" with whatever your systeminfo command returns for the uptime.

Update:

Workingtime.rb moves to github. Follow the installation instructions there.


Tags

cygwin, Ruby, windows


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked

Information about Data protection

This site uses Akismet to reduce spam. Learn how your comment data is processed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}