roxbury 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 13b52faa3fb9374270da742934fdee4f0765ccf4774c8502f364aa05a411795d
4
- data.tar.gz: 902edc63311964fbeb521a73f9cb4a5350c944852fa0ed8062a1d96f8416b4d1
3
+ metadata.gz: 95a78111c7254e8b3e5f35c69e781f16f971ad839c0e7c76c42b4f41ea681bd7
4
+ data.tar.gz: 9d76e376abecaa9796dd40f532b66d69e623876a0efc9360cf70976d4a4a9359
5
5
  SHA512:
6
- metadata.gz: e1aea73afe80cb96c4cfea67486bd208eba3587f903ffcce6f4c45db4493ecd9e987549035c0ba8405c2618ac780e3b213192c7c90dc4830aa3aa4b2be50698f
7
- data.tar.gz: 12b5aac1014909993f5119a5bb327182e09818bb85ba5899aa0621ee157c918ae9af385995212092420158962b893be412b78a880bb5ee50cb86079c06b58dfd
6
+ metadata.gz: db1b169f1db6ce10e4a9831a2400a3ff914f0bb5c7ba31d0bca9527fa144ded503200b78e2583bc1d619337862aa7126134f8cd852a54566c8efaef1f455fc32
7
+ data.tar.gz: 12b76c182370f9b382cdb15f73c7eb3afebdf1300c94973fc0bec8d1a56178a280323ac0bd3b2d3ff9ed6908ec6b9d4e13dcf4c59e04759f9f75fb502951a10b
data/Gemfile.lock CHANGED
@@ -1,17 +1,18 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- roxbury (0.1.0)
4
+ roxbury (0.1.1)
5
5
  activesupport (>= 4.2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (5.2.3)
10
+ activesupport (6.0.0)
11
11
  concurrent-ruby (~> 1.0, >= 1.0.2)
12
12
  i18n (>= 0.7, < 2)
13
13
  minitest (~> 5.1)
14
14
  tzinfo (~> 1.1)
15
+ zeitwerk (~> 2.1, >= 2.1.8)
15
16
  byebug (11.0.1)
16
17
  coderay (1.1.2)
17
18
  concurrent-ruby (1.1.5)
@@ -74,6 +75,7 @@ GEM
74
75
  thread_safe (0.3.6)
75
76
  tzinfo (1.2.5)
76
77
  thread_safe (~> 0.1)
78
+ zeitwerk (2.1.9)
77
79
 
78
80
  PLATFORMS
79
81
  ruby
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  A Ruby library for handling business days calculations, e.g., working days/hours between two dates, add working days/hours to a date, etc.
4
4
 
5
+ It was extracted from a production application that's been running for many years.
6
+
5
7
  [![Build Status](https://travis-ci.org/eeng/roxbury.svg?branch=master)](https://travis-ci.org/eeng/roxbury)
6
8
 
7
9
  ## Installation
data/bin/benchmark.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'benchmark'
2
+ require 'roxbury'
3
+
4
+ calendar = Roxbury::BusinessCalendar.new(working_hours: Hash.new(5..21))
5
+ puts Benchmark.measure { 100.times { calendar.add_working_days Date.today, 500 } }
@@ -55,11 +55,24 @@ module Roxbury
55
55
  # @param number_of_days [Integer, Float]
56
56
  # @return [Date, Time] The result of adding the number_of_days to the given date. If a Date is given returns a Date, otherwise if a Time is given returns a Time.
57
57
  def add_working_days to, number_of_days
58
- result = add_working_hours(to, number_of_days * max_working_hours_in_a_day)
59
- to.is_a?(Date) ? result.to_date : result
58
+ raise ArgumentError, 'number_of_days must not be negative' if number_of_days < 0
59
+
60
+ if to.is_a?(Time)
61
+ # this implementation would work for Date instances as well, but is around 10 times slower than the other in my machine
62
+ add_working_hours(to, number_of_days * max_working_hours_in_a_day)
63
+ else
64
+ remaining_days = number_of_days
65
+ rolling_date = to
66
+ loop do
67
+ remaining_days -= business_day(rolling_date).number_of_working_hours * 1.0 / max_working_hours_in_a_day
68
+ break if remaining_days < 0
69
+ rolling_date = roll_forward rolling_date.next
70
+ end
71
+ rolling_date
72
+ end
60
73
  end
61
74
 
62
- # Snaps the date to the beginning of the next business day, unless it is already within the working hours.
75
+ # Snaps the date to the beginning of the next business day, unless it is already within the working hours of a business day.
63
76
  #
64
77
  # @param date [Date, Time]
65
78
  def roll_forward date
@@ -69,7 +82,7 @@ module Roxbury
69
82
  elsif bday.starts_after?(date)
70
83
  bday.at_beginning
71
84
  else
72
- roll_forward date.tomorrow.beginning_of_day
85
+ roll_forward(date.is_a?(Date) ? date.tomorrow : date.tomorrow.beginning_of_day)
73
86
  end
74
87
  end
75
88
 
@@ -111,7 +124,7 @@ module Roxbury
111
124
  end
112
125
 
113
126
  def max_working_hours_in_a_day
114
- @working_hours.values.map(&:quantity).max
127
+ @max_working_hours_in_a_day ||= @working_hours.values.map(&:quantity).max
115
128
  end
116
129
  end
117
130
  end
@@ -1,3 +1,3 @@
1
1
  module Roxbury
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roxbury
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emmanuel Nicolau
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-08 00:00:00.000000000 Z
11
+ date: 2019-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -142,6 +142,7 @@ files:
142
142
  - LICENSE.txt
143
143
  - README.md
144
144
  - Rakefile
145
+ - bin/benchmark.rb
145
146
  - bin/console
146
147
  - bin/setup
147
148
  - lib/roxbury.rb