Measure your working time on windows

Posted by Jens on October 19, 2009

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:

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

So finally the script looks like this:

  1. #!/usr/bin/ruby
  2. HOURS_PER_DAY = 24
  3. MINUTES_PER_HOUR = 60
  4. SEC_PER_MINUTE = 60
  5. times = []
  6.  
  7. #get times from windows systeminfo
  8. #Systembetriebszeit:                     0 Tage, 0 Stunden, 10 Minuten, 0 Sekunden
  9. uptime_string = `systeminfo | grep Systembetriebszeit`
  10. uptime_string.slice!(0..28)
  11. time_strings = uptime_string.split(',')
  12. time_strings.each do |s|
  13. times < < s.gsub(/[A-Za-z\s]+/, '')
  14. end
  15.  
  16. #do some calculation
  17. seconds = times[0].to_i *  HOURS_PER_DAY * MINUTES_PER_HOUR * SEC_PER_MINUTE
  18. seconds = seconds + times[1].to_i * MINUTES_PER_HOUR * SEC_PER_MINUTE
  19. seconds = seconds + times[2].to_i * SEC_PER_MINUTE
  20. seconds = seconds + times[3].to_i
  21. now = Time.now
  22. login = now - seconds
  23. if (times[0].to_i != 0)
  24.   times[1] = times[1].to_i + times[0] * HOURS_PER_DAY  
  25. end
  26.  
  27. #print it
  28. puts login.strftime("Login:        %H:%M")
  29. puts now.strftime("Now:          %H:%M")
  30. puts "======================"
  31. 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.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

Comments