bizhours 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ === 1.0.0 / 2008-08-09
2
+
3
+ * Initial Release
4
+
@@ -0,0 +1,6 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/bizhours
6
+ lib/bizhours.rb
@@ -0,0 +1,43 @@
1
+ = bizhours
2
+
3
+ == DESCRIPTION:
4
+ Calculate hours between 2 timestamps that match business hours
5
+
6
+ == FEATURES/PROBLEMS:
7
+ Edit the BIZHOURS constant to set your normal business hours for each day of the week.
8
+ Supports custom hours on days as well, look at #diffhours= line in bizhours.rb for example using Rails
9
+
10
+ == SYNOPSIS:
11
+ require 'bizhours'
12
+ Bizhours::get_duration(Time.now, Time.now + 86400)
13
+
14
+ == REQUIREMENTS:
15
+ Duration >= 0.1.0
16
+
17
+ == INSTALL:
18
+ sudo gem install bizhours
19
+
20
+ == LICENSE:
21
+
22
+ (The MIT License)
23
+
24
+ Copyright (c) 2008 FIX
25
+
26
+ Permission is hereby granted, free of charge, to any person obtaining
27
+ a copy of this software and associated documentation files (the
28
+ 'Software'), to deal in the Software without restriction, including
29
+ without limitation the rights to use, copy, modify, merge, publish,
30
+ distribute, sublicense, and/or sell copies of the Software, and to
31
+ permit persons to whom the Software is furnished to do so, subject to
32
+ the following conditions:
33
+
34
+ The above copyright notice and this permission notice shall be
35
+ included in all copies or substantial portions of the Software.
36
+
37
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
38
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
39
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
40
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
41
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
42
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
43
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+
2
+ require 'rubygems'
3
+ require 'hoe'
4
+ require './lib/bizhours.rb'
5
+
6
+ Hoe.new('bizhours', Bizhours::VERSION) do |p|
7
+ p.rubyforge_name = 'bizhours'
8
+ p.summary = 'A utility to determine the ammount of business hours between two timestamps'
9
+ p.author = 'Curtis Carter'
10
+ p.email = 'curtis@rubyhq.com'
11
+ p.url = 'http://rubyhq.com'
12
+ p.description = p.paragraphs_of('README.txt', 2..6).join("\n\n")
13
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
14
+ p.extra_deps << ['duration', '>= 0.1.0']
15
+ #p.developer('FIX', 'FIX@example.com')
16
+ end
17
+
18
+ # vim: syntax=Ruby
File without changes
@@ -0,0 +1,152 @@
1
+ require 'rubygems'
2
+ require 'duration'
3
+ require 'date'
4
+
5
+ module Bizhours
6
+ VERSION = '1.0.0'
7
+ BIZHOURS = {
8
+ :mon => [8,20],
9
+ :tue => [8,20],
10
+ :wed => [8,20],
11
+ :thu => [8,20],
12
+ :fri => [8,20],
13
+ :sat => [9,18],
14
+ :sun => [8,8]
15
+ }
16
+
17
+ ##
18
+ #Takes 2 timestamps and returns a duration object of hours that match
19
+
20
+ def self.get_duration(startt, endt)
21
+ #if start and end time is on same day do the following
22
+ if (startt.yday == endt.yday && startt.year == endt.year)
23
+ openhr, closehr = business_hours_for_day(startt)
24
+ openhr = Time.local(startt.year, startt.month, startt.day, openhr)
25
+ closehr = Time.local(startt.year, startt.month, startt.day, closehr)
26
+
27
+ #to solve start hour before open
28
+ if startt < openhr
29
+ startt = Time.local(startt.year, startt.month, startt.day, openhr.hour)
30
+ end
31
+
32
+ #solves close hour after closing
33
+ if endt > closehr
34
+ endt = Time.local(endt.year, endt.month, endt.day, closehr.hour)
35
+ end
36
+ dur = Duration.new(endt - startt)
37
+
38
+ #to solve same time in and out past business hours
39
+ if dur < 0
40
+ return Duration.new
41
+ else
42
+ return dur
43
+ end
44
+ end
45
+
46
+ startd = Time.local(startt.year, startt.month, startt.day)
47
+ endd = Time.local(endt.year, endt.month, endt.day)
48
+
49
+ duration = first_day_time(startt) + last_day_time(endt) + inbetween_day_time(startt, endt)
50
+ return duration
51
+ end
52
+
53
+ private
54
+ ##
55
+ # Calculate duration for first day
56
+
57
+ def self.first_day_time(startt)
58
+ #get the closehr for the day
59
+ openhr, closehr = business_hours_for_day(startt)
60
+
61
+ #set open close start
62
+ open = Time.local(startt.year, startt.month, startt.day, openhr)
63
+ close = Time.local(startt.year, startt.month, startt.day, closehr)
64
+ start = Time.local(startt.year, startt.month, startt.day, startt.hour, startt.min, startt.sec)
65
+
66
+ #if start time happens after close then return 0 Duration
67
+ if close < start
68
+ return Duration.new
69
+ end
70
+
71
+ #if time started before opening time
72
+ if open > start
73
+ start = Time.local(startt.year, startt.month, startt.day, openhr)
74
+ end
75
+
76
+ elapsed = Duration.new(close - start)
77
+
78
+ return elapsed
79
+ end
80
+
81
+ ##
82
+ # Calculate duration for last day
83
+
84
+ def self.last_day_time(endt)
85
+ openhr, closehr = business_hours_for_day(endt)
86
+ close = Time.local(endt.year, endt.month, endt.day, closehr)
87
+ stop = Time.local(endt.year, endt.month, endt.day, endt.hour, endt.min, endt.sec)
88
+ open = Time.local(endt.year, endt.month, endt.day, openhr)
89
+
90
+ #if stop time happens before open hours for that day set that days duration to 0
91
+ if stop < open
92
+ return Duration.new
93
+ end
94
+
95
+ #if timestamped out after close set stop to closing time
96
+ if stop > close
97
+ stop = Time.local(endt.year, endt.month, endt.day, close.hour)
98
+ end
99
+
100
+ elapsed = Duration.new(stop - open)
101
+
102
+ return elapsed
103
+ end
104
+
105
+ ##
106
+ # Calculate duration for days inbetween first and last
107
+
108
+ def self.inbetween_day_time(startt,endt)
109
+ durs = []
110
+ duration = Duration.new
111
+ startd = DateTime.new(startt.year, startt.month, startt.day)
112
+ endd = DateTime.new(endt.year, endt.month, endt.day)
113
+
114
+ (startd..endd).each do |day|
115
+ daytime = Time.local(day.year, day.month, day.day)
116
+ openhr, closehr = business_hours_for_day(daytime)
117
+ close = Time.local(day.year, day.month, day.day, closehr)
118
+ open = Time.local(day.year, day.month, day.day, openhr)
119
+ elapsed = Duration.new(close - open)
120
+ durs << elapsed
121
+ end
122
+
123
+ durs.shift
124
+ durs.pop
125
+ durs.each do |dur|
126
+ duration += dur
127
+ end
128
+
129
+ return duration
130
+ end
131
+
132
+ ##
133
+ # If date has special hours it can pull them from db, otherwise it uses the BIZHOURS hash
134
+ # returns array with [openhour, closehour]
135
+
136
+ def self.business_hours_for_day(time)
137
+
138
+ ##If your going to have custom hours do something like the following and comment out the diffhours = nil
139
+ #diffhours = Hour.find(:first, :conditions => ["date = ?", time.to_date] )
140
+ #previous assumes table Hours, fields open and close
141
+ diffhours = nil
142
+ #if date is in database return them instead of defaults
143
+ if diffhours
144
+ return [diffhours.open.hour,diffhours.close.hour]
145
+ else
146
+ day = time.to_s.downcase[0,3]
147
+ return BIZHOURS[day.to_sym]
148
+ end
149
+ end
150
+ end
151
+
152
+ #puts Bizhours::get_duration(Time.now, Time.now + 86400 + 86400 + 899986400)
File without changes
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bizhours
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Curtis Carter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-10 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: duration
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.1.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.0
34
+ version:
35
+ description: "== FEATURES/PROBLEMS: Edit the BIZHOURS constant to set your normal business hours for each day of the week. Supports custom hours on days as well, look at #diffhours= line in bizhours.rb for example using Rails == SYNOPSIS: require 'bizhours' Bizhours::get_duration(Time.now, Time.now + 86400) == REQUIREMENTS: Duration >= 0.1.0 == INSTALL: sudo gem install bizhours == LICENSE:"
36
+ email: curtis@rubyhq.com
37
+ executables:
38
+ - bizhours
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - History.txt
43
+ - Manifest.txt
44
+ - README.txt
45
+ files:
46
+ - History.txt
47
+ - Manifest.txt
48
+ - README.txt
49
+ - Rakefile
50
+ - bin/bizhours
51
+ - lib/bizhours.rb
52
+ has_rdoc: true
53
+ homepage: http://rubyhq.com
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --main
57
+ - README.txt
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project: bizhours
75
+ rubygems_version: 1.2.0
76
+ signing_key:
77
+ specification_version: 2
78
+ summary: A utility to determine the ammount of business hours between two timestamps
79
+ test_files:
80
+ - test/test_bizhours.rb