You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

time_clock

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

time_clock

0.0.9
bundlerRubygems
Version published
Maintainers
1
Created
Source

TimeClock

Calculate the amount of business time between two times based on any arbitrary work calendar.

gem 'time_clock'

Once you've set a default calendar, any Time instance will have a few helpful methods.

Time.now.business_seconds_until(Time.now + 1.day)
=> 43200
Time.now.business_minutes_until(Time.now + 1.day)
=> 720
Time.now.business_hours_until(Time.now + 1.day)
=> 12

Also, Date and Time instances will have a method that calculates how many work dates there are between two times or dates.

Date.today.business_days_until(Date.today)
=> 1

You can also make custom calendars for each calculation (see Calendars section for details).

Things you may like about this library:

  • Works with any kind of Time, including mixed types.
  • Extremely simple codebase. Less than 100 lines of code.
  • Easy to customize with totally custom business calendars.
  • No dependencies.
  • 100% test coverage.
  • Code Climate

Configuration

Set a default_calendar for all comparisons to use for their calculation.

For example, in a Rails initializer:

# Time zones aren't required. Used here to make it easier to read.
time_zone = ActiveSupport::TimeZone.new("America/Chicago")

# The TimeClock::Calendar object stores the shifts for the business calendar.
calendar = TimeClock::Calendar.new

# Add all the work days (or individual shifts) to the calendar
# This example adds non week days between 6am and 6pm.
(Date.new(2011,1,1)..Date.new(2015,1,1)).each do |date|
  next unless (1..5).cover?(date.wday)
  calendar.add_shift(
    TimeClock::Shift.new(
      time_zone.local(date.year,date.month,date.day,6),
      time_zone.local(date.year,date.month,date.day,18),
    )
  )
end

# Set the default calendar
TimeClock.default_calendar = calendar

Calendars

If you want to use a separate calendar for an individual calculation, use a TimeClock::Comparison instance. Refer back to the configuration example to understand how to build a calendar.

TimeClock::Comparison.new(Time.now, Time.now + 1.day, custom_calendar).seconds

Contributing

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

FAQs

Package last updated on 06 Aug 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts