Skip to content

Instantly share code, notes, and snippets.

@DNA

DNA/worktime.rb

Last active Jul 30, 2018
Embed
What would you like to do?
check when its time to go home
#! /usr/bin/env ruby
# ./worktime 10:12 [13:06] [14:15] [19:40]
now = Time.now
expected_work_time = 28800
if ARGV[0].nil?
puts 'Você precisa fornecer ao menos o horário de entrada'
exit 1
end
hour, min = ARGV[0].split(':')
check_in = Time.new(now.year, now.month, now.day, hour, min)
hour, min = ARGV[1].split(':') if ARGV[1]
lunch_start = Time.new(now.year, now.month, now.day, hour, min)
hour, min = ARGV[2].split(':') if ARGV[2]
lunch_end = Time.new(now.year, now.month, now.day, hour, min)
lunch_time = (lunch_end - lunch_start)
if ARGV[3].nil?
check_out = check_in + (expected_work_time + lunch_time)
work_time = (now - check_in) - lunch_time
else
hour, min = ARGV[3].split(':') if ARGV[3]
check_out = Time.new(now.year, now.month, now.day, hour, min)
work_time = (check_out - check_in) - lunch_time
end
work_delta = work_time - expected_work_time
work_hour, work_min = (work_time / 60).divmod(60)
hour_diff, min_diff = (work_delta.abs / 60).divmod(60)
puts 'Horário de saida: ' + check_out.strftime('%H:%M')
if work_delta > 0
puts "Horas trabalhadas: \e[1m\e[32m#{work_hour}:#{work_min.to_i} (hora extra!)\e[0m"
puts "Horas extras: \e[1m\e[32m#{hour_diff}:#{min_diff.to_i}"
elsif work_delta < 0
puts "Horas trabalhadas: \e[1m\e[31m#{work_hour}:#{work_min.to_i} (faltam horas)\e[0m"
puts "Horas faltantes: \e[1m\e[31m#{hour_diff}:#{min_diff.to_i}"
else
puts "Sem horas extras a registrar"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment