百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
W

working_hours

> 编程语言
开源

⏰ 一款现代的 Ruby 宝石,可用于计算与工作时间相关的时间。

537 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

⏰ 一款现代的 Ruby 宝石,可用于计算与工作时间相关的时间。

WorkingHours

A modern ruby gem allowing to do time calculation with working hours.

Compatible and tested with:

  • Ruby 3.1, 3.2, 3.3, 4.0, JRuby 9.4
  • ActiveSupport 7.x, 8.x

Installation

Gemfile:

ruby
gem 'working_hours'

Usage

…

Configuration

The working hours configuration is thread safe and consists of a hash defining working periods for each day, a time zone and a list of days off. You can set it once, for example in a initializer for rails:

ruby
# Configure working hours
WorkingHours::Config.working_hours = {
  :tue => {'09:00' => '12:00', '13:00' => '17:00'},
  :wed => {'09:00' => '12:00', '13:00' => '17:00'},
  :thu => {'09:00' => '12:00', '13:00' => '17:00'},
  :fri => {'09:00' => '12:00', '13:00' => '17:05:30'},
  :sat => {'19:00' => '24:00'}
}

# Configure timezone (uses activesupport, defaults to UTC)
WorkingHours::Config.time_zone = 'Paris'

# Configure holidays
WorkingHours::Config.holidays = [Date.new(2014, 12, 31)]

Or you can set it for the duration of a block with the with_config method, this is particularly useful with around_filter:

ruby
WorkingHours::Config.with_config(working_hours: {mon:{'09:00' => '18:00'}}, holidays: [], time_zone: 'Paris') do
  # Intense calculations
end

with_config uses keyword arguments, you can pass all or some of the supported arguments :

  • working_hours
  • holidays
  • time_zone

Holiday hours

Sometimes you need to configure different working hours as a one-off, e.g. the working day might end earlier on Christmas Eve.

You can configure this with the holiday_hours option, either as an override on the existing working hours, or as a set of hours that are being worked on a holiday day.

If any hours are set for a calendar day in holiday_hours, then the working_hours for that day will be ignored, and only the entries in holiday_hours taken into consideration.

ruby
# Configure holiday hours
WorkingHours::Config.holiday_hours = {Date.new(2020, 12, 24) => {'09:00' => '12:00', '13:00' => '15:00'}}

Handling errors

If the configuration is erroneous, an WorkingHours::InvalidConfiguration exception will be raised containing the appropriate error message.

You can also access the error code in case you want to implement custom behavior or changing one specific message, e.g:

ruby
rescue WorkingHours::InvalidConfiguration => e
  if e.error_code == :empty
    raise StandardError.new "Config is required"
  end
  raise e
end

No core extensions / monkey patching

Core extensions (monkey patching to add methods on Time, Date, Numbers, etc.) are handy but not appreciated by everyone. WorkingHours can also be used without any monkey patching:

…

Use in your class/module

If you want to use working hours only inside a specific class or module, you can include its computation methods like this:

ruby
require 'working_hours/module'

class Order
  include WorkingHours

  def shipping_date_estimate
    Duration.new(2, :days).since(payment_received_at)
  end

  def payment_delay
    working_days_between(created_at, payment_received_at)
  end
end

Timezones

This gem uses a simple but efficient approach in dealing with timezones. When you define your working hours you have to choose a timezone associated with it (in the config example, the working hours are in Paris time). Then, any time used in calculation will be converted to this timezone first, so you don't have to worry if your times are local or UTC as long as they are correct :)

Alternatives

There is a gem called business_time already available to do this kind of computation and it was of great help to us. But we decided to start another one because business_time is suffering from a few bugs and inconsistencies. It also lacks essential features to us (like working minutes computation).

Another gem called biz was released after working_hours to bring some alternative.

Contributing

  1. Fork it ( http://github.com/intrepidd/working_hours/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubybusiness-hours

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言