business_time 0.7.3 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5bdd88444abb3662202edb5a58a7966198095d0c
4
+ data.tar.gz: d3bb972b85a2f229c34a88511bcb6eb9595f8589
5
+ SHA512:
6
+ metadata.gz: b4d19b1491299ca47b3a765f701ba6ba909a5af84925005c75262679b4d193c70520d7938bc23c694fc03d5ee509c742f472285d82da75050aa457513d2f5f5f
7
+ data.tar.gz: c959f75f0cc63e165e7a55bceaddbc9dceefb0db9491cbeec38bf75944359c0113e20aa9a22460ee650071e6a7af515b7de6e3c3ceb4fa71004bda8f76f5e0fd
data/README.rdoc CHANGED
@@ -90,6 +90,9 @@ I needed this, but taking into account business hours/days and holidays.
90
90
  ticket_resolved = Time.parse("February 4, 2012, 10:50 am")
91
91
  ticket_reported.business_time_until(ticket_resolved) #=> 8.hours + 10.minutes
92
92
 
93
+ # you can also determine if a given time is within business hours
94
+ Time.parse("February 3, 2012, 10:00 am").during_business_hours?
95
+
93
96
  # note that counterintuitively, durations might not be quite what you expect when involving weekends.
94
97
  # Consider the following example:
95
98
  ticket_reported = Time.parse("February 3, 2012, 10:40 am")
@@ -100,6 +103,16 @@ I needed this, but taking into account business hours/days and holidays.
100
103
  # Monday, Feb 6th 2012, 9:00am. The business time between 10:40am friday and 9am monday is
101
104
  # 6 hours and 20 minutes. From a quick inspection of the code, it looks like it should be 8 hours.
102
105
 
106
+ # or you can calculate business dates between two dates
107
+ monday = Date.parse("December 20, 2010")
108
+ wednesday = Date.parse("December 22, 2010")
109
+ monday.business_dates_until(wednesday) #=> [Mon, 20 Dec 2010, Tue, 21 Dec 2010]
110
+
111
+ # you can get the first workday after a time or return itself if it is a workday
112
+ saturday = Time.parse("Sat Aug 9, 18:00:00, 2014")
113
+ monday = Time.parse("Mon Aug 11, 18:00:00, 2014")
114
+ Time.first_business_day(saturday) #=> "Mon Aug 11, 18:00:00, 2014"
115
+ Time.first_business_day(monday) #=> "Mon Aug 11, 18:00:00, 2014"
103
116
  == Rails generator
104
117
 
105
118
  rails generate business_time:config
@@ -158,6 +171,7 @@ I'm hoping Arild and I write some good blog entries on the subject at http://blo
158
171
  * Piotr Jakubowski http://github.com/piotrj
159
172
  * Glenn Vanderburg http://github.com/glv
160
173
  * Michael Grosser http://github.com/grosser
174
+ * Michael Curtis http://github.com/mcurtis
161
175
 
162
176
  (Special thanks for Arild on the complexities of dealing with TimeWithZone)
163
177
 
data/lib/business_time.rb CHANGED
@@ -7,9 +7,9 @@ require 'yaml'
7
7
  require 'business_time/config'
8
8
  require 'business_time/business_hours'
9
9
  require 'business_time/business_days'
10
- require 'business_time/core_ext/date'
11
10
  require 'business_time/core_ext/fixnum'
12
11
 
13
12
  require 'business_time/time_extensions'
13
+ require 'business_time/core_ext/date'
14
14
  require 'business_time/core_ext/time'
15
15
  require 'business_time/core_ext/active_support/time_with_zone'
@@ -1,38 +1,33 @@
1
1
  require 'active_support/time'
2
2
 
3
3
  module BusinessTime
4
-
5
4
  class BusinessDays
6
5
  def initialize(days)
7
6
  @days = days
8
7
  end
9
8
 
10
- def after(time = Time.now)
11
- time = Time.zone ? Time.zone.parse(time.strftime('%Y-%m-%d %H:%M:%S %z')) : Time.parse(time.strftime('%Y-%m-%d %H:%M:%S %z'))
9
+ def after(time = Time.current)
12
10
  days = @days
13
- while days > 0 || !Time.workday?(time)
14
- days -= 1 if Time.workday?(time)
11
+ while days > 0 || !time.workday?
12
+ days -= 1 if time.workday?
15
13
  time = time + 1.day
16
14
  end
17
15
  time
18
16
  end
19
-
17
+
20
18
  alias_method :from_now, :after
21
19
  alias_method :since, :after
22
-
23
- def before(time = Time.now)
24
- time = Time.zone ? Time.zone.parse(time.rfc822) : Time.parse(time.rfc822)
20
+
21
+ def before(time = Time.current)
25
22
  days = @days
26
- while days > 0 || !Time.workday?(time)
27
- days -= 1 if Time.workday?(time)
23
+ while days > 0 || !time.workday?
24
+ days -= 1 if time.workday?
28
25
  time = time - 1.day
29
26
  end
30
27
  time
31
28
  end
32
-
29
+
33
30
  alias_method :ago, :before
34
31
  alias_method :until, :before
35
-
36
- end
37
-
32
+ end
38
33
  end
@@ -28,7 +28,7 @@ module BusinessTime
28
28
  end
29
29
 
30
30
  # Ignore weekends and holidays
31
- while !Time.workday?(after_time)
31
+ while !after_time.workday?
32
32
  after_time = after_time + 1.day
33
33
  end
34
34
  end
@@ -56,7 +56,7 @@ module BusinessTime
56
56
  end
57
57
 
58
58
  # Ignore weekends and holidays
59
- while !Time.workday?(before_time)
59
+ while !before_time.workday?
60
60
  before_time = before_time - 1.day
61
61
  end
62
62
  end
@@ -1,3 +1,4 @@
1
1
  class ActiveSupport::TimeWithZone
2
2
  include BusinessTime::TimeExtensions
3
+ extend BusinessTime::TimeExtensions::ClassMethods
3
4
  end
@@ -1,14 +1,12 @@
1
1
  # Add workday and weekday concepts to the Date class
2
2
  class Date
3
- def workday?
4
- weekday? && !BusinessTime::Config.holidays.include?(self)
5
- end
3
+ include BusinessTime::TimeExtensions
6
4
 
7
- def weekday?
8
- BusinessTime::Config.weekdays.include? wday
5
+ def business_days_until(to_date)
6
+ business_dates_until(to_date).size
9
7
  end
10
8
 
11
- def business_days_until(to_date)
12
- (self...to_date).select{ |day| day.workday? }.size
9
+ def business_dates_until(to_date)
10
+ (self...to_date).select { |day| day.workday? }
13
11
  end
14
- end
12
+ end
@@ -1,4 +1,5 @@
1
1
  # Add workday and weekday concepts to the Time class
2
2
  class Time
3
3
  include BusinessTime::TimeExtensions
4
+ extend BusinessTime::TimeExtensions::ClassMethods
4
5
  end
@@ -1,7 +1,14 @@
1
1
  module BusinessTime
2
2
  module TimeExtensions
3
- def self.included(base)
4
- base.extend(ClassMethods)
3
+ # True if this time is on a workday (between 00:00:00 and 23:59:59), even if
4
+ # this time falls outside of normal business hours.
5
+ def workday?
6
+ weekday? && !BusinessTime::Config.holidays.include?(to_date)
7
+ end
8
+
9
+ # True if this time falls on a weekday.
10
+ def weekday?
11
+ BusinessTime::Config.weekdays.include?(wday)
5
12
  end
6
13
 
7
14
  module ClassMethods
@@ -26,13 +33,14 @@ module BusinessTime
26
33
  # True if this time is on a workday (between 00:00:00 and 23:59:59), even if
27
34
  # this time falls outside of normal business hours.
28
35
  def workday?(day)
29
- Time.weekday?(day) &&
30
- !BusinessTime::Config.holidays.include?(day.to_date)
36
+ ActiveSupport::Deprecation.warn("`Time.workday?(time)` is deprecated. Please use `time.workday?`")
37
+ day.workday?
31
38
  end
32
39
 
33
40
  # True if this time falls on a weekday.
34
41
  def weekday?(day)
35
- BusinessTime::Config.weekdays.include? day.wday
42
+ ActiveSupport::Deprecation.warn("`Time.weekday?(time)` is deprecated. Please use `time.weekday?`")
43
+ day.weekday?
36
44
  end
37
45
 
38
46
  def before_business_hours?(time)
@@ -47,7 +55,7 @@ module BusinessTime
47
55
  # when the time is outside of business hours
48
56
  def roll_forward(time)
49
57
 
50
- if Time.before_business_hours?(time) || !Time.workday?(time)
58
+ if Time.before_business_hours?(time) || !time.workday?
51
59
  next_business_time = Time.beginning_of_workday(time)
52
60
  elsif Time.after_business_hours?(time) || Time.end_of_workday(time) == time
53
61
  next_business_time = Time.beginning_of_workday(time + 1.day)
@@ -55,17 +63,27 @@ module BusinessTime
55
63
  next_business_time = time.clone
56
64
  end
57
65
 
58
- while !Time.workday?(next_business_time)
66
+ while !next_business_time.workday?
59
67
  next_business_time = Time.beginning_of_workday(next_business_time + 1.day)
60
68
  end
61
69
 
62
70
  next_business_time
63
71
  end
64
72
 
73
+ # Returns the time parameter itself if it is a business day
74
+ # or else returns the next business day
75
+ def first_business_day(time)
76
+ while !time.workday?
77
+ time = time + 1.day
78
+ end
79
+
80
+ time
81
+ end
82
+
65
83
  # Rolls backwards to the previous end_of_workday when the time is outside
66
84
  # of business hours
67
85
  def roll_backward(time)
68
- prev_business_time = if (Time.before_business_hours?(time) || !Time.workday?(time))
86
+ prev_business_time = if (Time.before_business_hours?(time) || !time.workday?)
69
87
  Time.end_of_workday(time - 1.day)
70
88
  elsif Time.after_business_hours?(time)
71
89
  Time.end_of_workday(time)
@@ -73,7 +91,7 @@ module BusinessTime
73
91
  time.clone
74
92
  end
75
93
 
76
- while !Time.workday?(prev_business_time)
94
+ while !prev_business_time.workday?
77
95
  prev_business_time = Time.end_of_workday(prev_business_time - 1.day)
78
96
  end
79
97
 
@@ -81,7 +99,7 @@ module BusinessTime
81
99
  end
82
100
 
83
101
  def work_hours_total(day)
84
- return 0 unless Time.workday?(day)
102
+ return 0 unless day.workday?
85
103
 
86
104
  day = day.strftime('%a').downcase.to_sym
87
105
 
@@ -141,5 +159,9 @@ module BusinessTime
141
159
  first_day + days_in_between + last_day
142
160
  end * direction
143
161
  end
162
+
163
+ def during_business_hours?
164
+ self.workday? && self.to_i.between?(Time.beginning_of_workday(self).to_i, Time.end_of_workday(self).to_i)
165
+ end
144
166
  end
145
167
  end
@@ -1,3 +1,3 @@
1
1
  module BusinessTime
2
- VERSION = "0.7.3"
2
+ VERSION = "0.7.4"
3
3
  end
metadata CHANGED
@@ -1,110 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: business_time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
5
- prerelease:
4
+ version: 0.7.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - bokmann
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-06-10 00:00:00.000000000 Z
11
+ date: 2015-04-12 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.1.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.1.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: tzinfo
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rake
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rdoc
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: minitest
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - ">="
84
74
  - !ruby/object:Gem::Version
85
75
  version: '0'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - ">="
92
81
  - !ruby/object:Gem::Version
93
82
  version: '0'
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: minitest-rg
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - ">="
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - ">="
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
97
  description: Have you ever wanted to do things like "6.business_days.from_now" and
@@ -133,32 +120,25 @@ files:
133
120
  homepage: https://github.com/bokmann/business_time
134
121
  licenses:
135
122
  - MIT
123
+ metadata: {}
136
124
  post_install_message:
137
125
  rdoc_options: []
138
126
  require_paths:
139
127
  - lib
140
128
  required_ruby_version: !ruby/object:Gem::Requirement
141
- none: false
142
129
  requirements:
143
- - - ! '>='
130
+ - - ">="
144
131
  - !ruby/object:Gem::Version
145
132
  version: '0'
146
- segments:
147
- - 0
148
- hash: -1720097037897540308
149
133
  required_rubygems_version: !ruby/object:Gem::Requirement
150
- none: false
151
134
  requirements:
152
- - - ! '>='
135
+ - - ">="
153
136
  - !ruby/object:Gem::Version
154
137
  version: '0'
155
- segments:
156
- - 0
157
- hash: -1720097037897540308
158
138
  requirements: []
159
139
  rubyforge_project:
160
- rubygems_version: 1.8.23
140
+ rubygems_version: 2.2.2
161
141
  signing_key:
162
- specification_version: 3
142
+ specification_version: 4
163
143
  summary: Support for doing time math in business hours and days
164
144
  test_files: []