### Start from custom Date or Time Source: https://github.com/intrepidd/working_hours/blob/master/README.md Calculate future or past dates/times starting from a specific reference point. ```ruby WorkingHours::Duration.new(8, :days).since(Date.new(2014, 12, 31)) # => Mon, 12 Jan 2015 WorkingHours::Duration.new(4, :hours).until(Time.utc(2014, 8, 4, 8, 32)) # => 2014-08-01 13:00:00 ``` -------------------------------- ### Install WorkingHours Gem Source: https://github.com/intrepidd/working_hours/blob/master/README.md Add the WorkingHours gem to your Gemfile to include it in your project. ```ruby gem 'working_hours' ``` -------------------------------- ### Get Next Working Time Source: https://github.com/intrepidd/working_hours/blob/master/README.md Find the start of the next working day from a given Time. ```ruby require 'working_hours' # Next working time sunday = Time.utc(2014, 8, 3) monday = WorkingHours.next_working_time(sunday) # => Mon, 04 Aug 2014 09:00:00 UTC +00:00 tuesday = WorkingHours.next_working_time(monday) # => Tue, 05 Aug 2014 09:00:00 UTC +00:00 ``` -------------------------------- ### Time Calculation from Custom Date/Time Source: https://github.com/intrepidd/working_hours/blob/master/README.md Start time calculations from a specific Date or Time object, moving forward or backward by working intervals. ```ruby require 'working_hours' # Start from custom Date or Time Date.new(2014, 12, 31) + 8.working.days # => Mon, 12 Jan 2015 Time.utc(2014, 8, 4, 8, 32) - 4.working.hours # => 2014-08-01 13:00:00 ``` -------------------------------- ### Advance to Next Working Time Source: https://github.com/intrepidd/working_hours/blob/master/README.md Find the next available working time starting from a given Time, respecting configured working hours. ```ruby require 'working_hours' # Advance to next working time WorkingHours.advance_to_working_time(Time.utc(2014, 8, 4, 7, 16)) # => Mon, 04 Aug 2014 09:00:00 UTC +00:00 ``` -------------------------------- ### Configure Working Hours Temporarily Source: https://github.com/intrepidd/working_hours/blob/master/README.md Use `with_config` to apply temporary configuration for working hours, holidays, and time zone within a block. This is useful for specific calculations or tests. ```ruby WorkingHours::Config.with_config(working_hours: {mon:{'09:00' => '18:00'}}, holidays: [], time_zone: 'Paris') do # Intense calculations end ``` -------------------------------- ### Basic Time Calculations (Forward) Source: https://github.com/intrepidd/working_hours/blob/master/README.md Perform time calculations to move forward by working days, hours, or minutes from the current time. ```ruby require 'working_hours' # Move forward 1.working.day.from_now 2.working.hours.from_now 15.working.minutes.from_now ``` -------------------------------- ### Basic Time Calculations (Backward) Source: https://github.com/intrepidd/working_hours/blob/master/README.md Perform time calculations to move backward by working days, hours, or minutes from the current time. ```ruby require 'working_hours' # Move backward 1.working.day.ago 2.working.hours.ago 15.working.minutes.ago ``` -------------------------------- ### Include WorkingHours in a class Source: https://github.com/intrepidd/working_hours/blob/master/README.md Incorporate working hours computation methods into a custom class by including the WorkingHours module. ```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 ``` -------------------------------- ### Configure Working Hours Source: https://github.com/intrepidd/working_hours/blob/master/README.md Set the default working hours for different days of the week, time zone, and holidays. This configuration is thread-safe. ```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)] ``` -------------------------------- ### Handle Invalid Configuration Errors Source: https://github.com/intrepidd/working_hours/blob/master/README.md Catch `WorkingHours::InvalidConfiguration` exceptions to handle erroneous configurations. You can check the `error_code` for specific error types. ```ruby rescue WorkingHours::InvalidConfiguration => e if e.error_code == :empty raise StandardError.new "Config is required" end raise e end ``` -------------------------------- ### Advance to Next Closing Time Source: https://github.com/intrepidd/working_hours/blob/master/README.md Determine the next closing time based on the current Time and configured working hours. ```ruby require 'working_hours' # Advance to next closing time WorkingHours.advance_to_closing_time(Time.utc(2014, 8, 4, 7, 16)) # => Mon, 04 Aug 2014 17:00:00 UTC +00:00 WorkingHours.advance_to_closing_time(Time.utc(2014, 8, 4, 10, 16)) # => Mon, 04 Aug 2014 17:00:00 UTC +00:00 WorkingHours.advance_to_closing_time(Time.utc(2014, 8, 4, 18, 16)) # => Tue, 05 Aug 2014 17:00:00 UTC +00:00 ``` -------------------------------- ### Use WorkingHours Without Monkey Patching (Forward) Source: https://github.com/intrepidd/working_hours/blob/master/README.md Utilize the WorkingHours gem without modifying core Ruby classes. Use `WorkingHours::Duration` for time calculations. ```ruby require 'working_hours/module' # Move forward WorkingHours::Duration.new(1, :days).from_now WorkingHours::Duration.new(2, :hours).from_now WorkingHours::Duration.new(15, :minutes).from_now ``` -------------------------------- ### Compute working duration between two times Source: https://github.com/intrepidd/working_hours/blob/master/README.md Calculate the total working duration in seconds between two specific times, accounting for working hours. ```ruby from = Time.utc(2014, 8, 3, 8, 32) # sunday 8:32am to = Time.utc(2014, 8, 4, 10, 32) # monday 10:32am WorkingHours.working_time_between(from, to) # => 5520 (1.hour + 32.minutes) ``` -------------------------------- ### Configure Holiday Hours Source: https://github.com/intrepidd/working_hours/blob/master/README.md Define specific working hours for particular holidays, overriding the general working hours for that day. If any hours are set for a holiday, they take precedence. ```ruby # Configure holiday hours WorkingHours::Config.holiday_hours = {Date.new(2020, 12, 24) => {'09:00' => '12:00', '13:00' => '15:00'}} ``` -------------------------------- ### Return to Previous Working Time Source: https://github.com/intrepidd/working_hours/blob/master/README.md Find the end of the previous working day from a given Time. ```ruby require 'working_hours' # Return to previous working time WorkingHours.return_to_working_time(Time.utc(2014, 8, 4, 7, 16)) # => Fri, 01 Aug 2014 17:00:00 UTC +00:00 ``` -------------------------------- ### Compute working days between two dates Source: https://github.com/intrepidd/working_hours/blob/master/README.md Calculate the number of working days between two given dates, considering weekends as non-working days. ```ruby friday = Date.new(2014, 10, 17) monday = Date.new(2014, 10, 20) WorkingHours.working_days_between(friday, monday) # => 1 # Time is considered at end of day, so: # - friday to saturday = 0 working days # - sunday to monday = 1 working days ``` -------------------------------- ### Compute Working Days Between Dates Source: https://github.com/intrepidd/working_hours/blob/master/README.md Calculate the number of working days between two specified dates. Time is considered at the end of the day for calculations. ```ruby require 'working_hours' # Compute working days between two dates friday = Date.new(2014, 10, 17) monday = Date.new(2014, 10, 20) fri.working_days_until(monday) # => 1 # Time is considered at end of day, so: # - friday to saturday = 0 working days # - sunday to monday = 1 working days ``` -------------------------------- ### Compute Working Duration Between Times Source: https://github.com/intrepidd/working_hours/blob/master/README.md Calculate the total working time in seconds between two Time objects. ```ruby require 'working_hours' # Compute working duration (in seconds) between two times from = Time.utc(2014, 8, 3, 8, 32) # sunday 8:32am to = Time.utc(2014, 8, 4, 10, 32) # monday 10:32am from.working_time_until(to) # => 5520 (1.hour + 32.minutes) ``` -------------------------------- ### Move backward in time Source: https://github.com/intrepidd/working_hours/blob/master/README.md Calculate a past date or time relative to the current moment using Duration objects. ```ruby WorkingHours::Duration.new(1, :days).ago WorkingHours::Duration.new(2, :hours).ago WorkingHours::Duration.new(15, :minutes).ago ``` -------------------------------- ### Check if a time is within working hours Source: https://github.com/intrepidd/working_hours/blob/master/README.md Verify if a specific time falls within the configured working hours. ```ruby WorkingHours.in_working_hours?(Time.utc(2014, 8, 4, 7, 16)) # => false ``` -------------------------------- ### Check if a Day is a Working Day Source: https://github.com/intrepidd/working_hours/blob/master/README.md Determine if a given Date falls on a working day according to the configured working hours. ```ruby require 'working_hours' # Know if a day is worked Date.new(2014, 12, 28).working_day? # => false ``` -------------------------------- ### Check if a day is a working day Source: https://github.com/intrepidd/working_hours/blob/master/README.md Determine if a given date falls within the defined working days. ```ruby WorkingHours.working_day?(Date.new(2014, 12, 28)) # => false ``` -------------------------------- ### Check if a Time is within Working Hours Source: https://github.com/intrepidd/working_hours/blob/master/README.md Check if a given Time falls within the configured working hours for its day. ```ruby require 'working_hours' # Know if a time is worked Time.utc(2014, 8, 4, 7, 16).in_working_hours? # => false ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.