Help us understand the problem. What is going on with this article?

【残業計算】タイムカードの計算ができるGem「punch_time」の使い方

この記事は

タイムカード計算 punch_time を作ったメモ

やりたかったこと

Railsで深夜残業時間の計算をするのにSQLだと再利用ができなそうだったので

やり方

インストール方法

gem 'punch_time'

コンフィグ

コンフィグでシフトの開始、終了時間、休憩時間、深夜時間、時差が設定できます

PunchTime.configure do |config|
config.shift_in_time = Time.parse('10:00')
config.shift_out_time = Time.parse('19:00')
config.breaks = [
    {
    start_time: Time.parse('12:00'),
    end_time: Time.parse('13:00')
    }
]
config.night = {
    start_time: Time.parse('22:00'),
    end_time: Time.parse('05:00')
}
config.offset = '+0900'
end

タイムカードの記録

就業時間はシフトの開始時間からで計算

PunchTime.punch(DateTime.parse('20200101 10:10'), DateTime.parse('20200101 19:00'))

就業時間の出力

p PunchTime.sum_work.hours

例えば、business_time、holiday_jpを組み合わせれば祝日の勤務時間計算がこうなります

sum_works = []
Date.parse('20200101').upto(Date.parse('20200105')) do |x|
  PunchTime.punch(DateTime.parse(x.to_s + ' 10:10'), DateTime.parse(x.to_s + ' 19:00'))
  sum_works.append(PunchTime.sum_work.hours) unless x.workday?
end
p sum_works.inject(:+).to_i

そのほか

PunchTime.sum_work
PunchTime.sum_tardy
PunchTime.sum_over_work
PunchTime.sum_night_work
PunchTime.tardy?
PunchTime.overtime_work?
PunchTime.night_overtime_work?

技術的ポイント

なあなあだったタイムゾーンとか、Rationalの扱いに結構時間とられたので、これからはオープンソースは人々の協力によって支えられているのだ、ということを心に留めてGithubを利用したい

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした