Workpattern Rubygem Documentation
Calculates dates and durations whilst taking into account working and non-working times. It creates calendars similar to what you can find in project scheduling software like Microsoft project and Primavera P6.
Workpattern is a library with no monkey-patching and was tested using Travis against the following Ruby versions
jruby-19mode (22.214.171.124 (2.5.7) and
jruby-head (126.96.36.199-SNAPSHOT (2.6.5).
You can install it using:
gem install workpattern
Or you can add it to your Gemfile with:
Then run the bundle command to install it.
Configure and Calculate
First create a
Workpattern to hold all the working and resting times.
mywp=Workpattern.new 'My Workpattern',2011,10
That line created a
My Workpattern starting on 1-Jan-2011 and continuing for
10 years until
mywp is created with a 24 hour a day working time. Next step is to tell it to ignore weekends by making every Saturday and Sunday non-working.
mywp.resting :days => :weekend
Workpattern.clock method can be used to specify the non-working times for each weekday. Any class that responds to
#min methods such as
DateTime can be used instead of
mywp.resting :days =>:weekday, :from_time=>Workpattern.clock(0,0),:to_time=>Workpattern.clock(8,59) mywp.resting :days =>:weekday, :from_time=>Workpattern.clock(12,0),:to_time=>Workpattern.clock(12,59) mywp.resting :days =>:weekday, :from_time=>Workpattern.clock(18,0),:to_time=>Workpattern.clock(23,59)
As well as
:weekday it is possible to use
mywp setup, the
#calc method is used to add 32 hours which must be supplied as the number of whole minutes (1920) to a date.
my_date=Time.gm 2011,9,1,9,0 result_date = mywp.calc my_date,1920 # => 6/9/11@18:00
The result takes into account the non-working or resting times.
Subtracting a date is just as easy by using a negative number of minutes in
Finding the duration between two dates is also easy using the
diff_result = mywp.diff my_date, result_date # => 1920
Vacations can be added to the
Workpattern using the
mywp.resting :days => :all, :start => DateTime.civil(2011,5,1), :finish => DateTime.civil(2011,5,7)
Find out if a specific date and time is working or not.
mydate = DateTime.civil 2011,5,2,9,10 mywp.resting? mydate # => true mywp.working? mydate # => false
# Fetch a specific Workpattern Workpattern.get "My Workpattern" # Delete a specific Workpattern Workpattern.delete "My Workpattern" # Delete all Workpatterns Workpattern.clear
(The MIT License)
Copyright (c) 2012 - 2021
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.