Last active
July 30, 2018 22:23
-
-
Save DNA/0e0233e6c40b31477e00c6932f326f61 to your computer and use it in GitHub Desktop.
check when its time to go home
This file contains hidden or 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 | |
# ./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