Last active
October 30, 2015 08:24
-
-
Save aladac/e85c416f7d5828c5d650 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# A script to show number of workdays and work hours in a month, named in honor of the Warcraft Peon unit | |
require 'date' | |
require "optparse" | |
options = { hours: 8 } | |
OptionParser.new do |opts| | |
opts.banner = "Usage: #{$0} [options]" | |
opts.on("-w", "--hours N", Integer, "Hours per workday") do |h| | |
options[:hours] = h | |
end | |
opts.on_tail("-h", "--help", "Show this message") do | |
puts opts | |
exit | |
end | |
end.parse! | |
num_days = lambda { |time| Date.civil(time.year, time.month, -1).mday } | |
this_month = Time.now.month | |
this_year = Time.now.year | |
months = Hash.new | |
hours = options[:hours] | |
(this_month..12).to_a.each do |month| | |
time = Date.civil(this_year, month) | |
days = num_days.call(time) | |
months[month] = [] | |
(1..days).to_a.each do |day| | |
ts = Date.civil(this_year, month, day) | |
if ! ts.saturday? and ! ts.sunday? | |
months[month].push hours | |
end | |
end | |
end | |
months.each_pair do |month, work| | |
hours = work.inject { |sum, h| sum + h } | |
days = work.count | |
date = Date.civil(this_year, month).strftime("%Y-%m") | |
puts "#{date}\t\thours: #{hours}, days: #{days}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment