LoginSignup
2
2

More than 1 year has passed since last update.

【勤怠管理】タイムカードの計算ができるGem「punch_time」の使い方

Last updated at Posted at 2020-02-10

この記事は

タイムカード計算 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を利用したい

株式会社旗指物 について

punch_time は株式会社旗指物によってメンテナンス、開発が行われています。

当社では、働き方改革関連法の施行とともに、より多くの従業員同士の個性の発揮の機会と、チームワークが高められる社会の実現を期待しています。
そして、科学的なアプローチの利用と拡大に貢献し、もって企業の生産性の向上を図るため、当社ソフトウェアのOSS化を進めています。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2