enterprise_time_extensions 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Miles Z. Sterrett
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ h1. Enterprise Time Extensions
2
+
3
+ Enterprisey extensions to Ruby's Time class. How else will you know when it's
4
+ OK to stop avoiding work, and you can actually leave work?
5
+
6
+ Example:
7
+
8
+ h2. Un-Fun, All-Business Instance Method Examples
9
+
10
+ <pre>
11
+ <code>
12
+ t = Time.now #=> Fri Jan 09 22:12:31 0100 2009
13
+ # Enterprisey methods in action!:
14
+ t.after_hours? #=> false
15
+ t.holiday? #=> false
16
+
17
+ t = Time.local(2009, 12, 25) #=> Fri Dec 25 00:00:00 0100 2009
18
+ t.after_hours? #=> true -- because it's a holiday
19
+ t.holiday? #=> true -- I told you!
20
+
21
+ t = Time.local(2009, 1, 9, 18, 30) #=> Fri Jan 09 18:30:00 0100 2009
22
+ t.after_hours? #=> true -- Business hours are 8AM to 6PM ... SHARP!
23
+ t.holiday? #=> false
24
+ </code>
25
+ </pre>
26
+
27
+ There are question mark methods for the holidays that are paid-time-off for
28
+ slaves of The Enterprise.
29
+
30
+ * new_years_day?
31
+ * mlk_jr_day?
32
+ * memorial_day?
33
+ * independence_day?
34
+ * labor_day?
35
+ * thanksgiving?
36
+ * christmas_day?
37
+
38
+ <em>If you need/want more, let me know! You're welcome
39
+ to do it yourself, if you wish.</em>
40
+
41
+ h2. Frank, Straight-Shooting Class Methods
42
+
43
+ <pre>
44
+ <code>
45
+ # Countdown to the next football game?
46
+ Time.next_sunday #=> Sun Jan 11 00:00:00 0100 2009
47
+ # The big one:
48
+ Time.nth_wday(3, 6, 7, 2009) #=> Sat Jul 18 00:00:00 0200 2009
49
+ </code>
50
+ </pre>
51
+
52
+ Time::nth_wday could probably use some explanation. Time::nth_wday will find the 'nth' week day of a month. Above, we found the 3th (hehe) Saturday of July 2009. This is actually used often in the various holiday methods from above. For instance, Thanksgiving is on the 4th Thursday of November. The method looks like this:
53
+ <pre>
54
+ <code>
55
+ def self.nth_wday(n, wday, month, year)
56
+ # ...
57
+ end
58
+ </code>
59
+ </pre>
60
+
61
+ *Enjoy!*
62
+
63
+ <em>(I feel obligated to tell you, honestly, that the environment at nFrame is
64
+ far removed from the usual negative implications of the word 'Enterprise'.
65
+ It's quite nice, actually. Oh, and we only have like 40 total employees.)</em>
@@ -0,0 +1,105 @@
1
+ module EnterpriseTimeExtensions
2
+ module ClassMethods
3
+ def next_sunday
4
+ timeobj = Time.local(Time.now.year, Time.now.month, Time.now.day, 0) # Today at midnight
5
+ timeobj += 60*60*24 until timeobj.wday == 0
6
+ timeobj # Should now be next Sunday at midnight
7
+ end
8
+
9
+ def nth_wday(n, wday, month, year)
10
+ if (!n.between? 1,5) ||
11
+ (!wday.between? 0,6) ||
12
+ (!month.between? 1,12)
13
+ raise ArgumentError
14
+ end
15
+ t = Time.local year, month, 1
16
+ first = t.wday
17
+ if first == wday
18
+ fwd = 1
19
+ elsif first < wday
20
+ fwd = wday - first + 1
21
+ elsif first > wday
22
+ fwd = (wday+7) - first + 1
23
+ end
24
+ target = fwd + (n-1)*7
25
+ begin
26
+ t2 = Time.local year, month, target
27
+ rescue ArgumentError
28
+ return nil
29
+ end
30
+ t2
31
+ end
32
+ end
33
+
34
+ def after_hours?
35
+ result = true
36
+ # M-F, 8am to 6pm are business hours
37
+ if (1..5).include?(self.wday) && (8..17).include?(self.hour)
38
+ result = false
39
+ end
40
+ # unless it's a holiday!
41
+ if self.holiday?
42
+ result = true
43
+ end
44
+ result
45
+ end
46
+
47
+ def holiday?
48
+ unless new_years_day? ||
49
+ mlk_jr_day? ||
50
+ memorial_day? ||
51
+ independence_day? ||
52
+ labor_day? ||
53
+ thanksgiving? ||
54
+ christmas_day?
55
+ return false
56
+ end
57
+ true
58
+ end
59
+
60
+ def new_years_day?
61
+ return false unless self.yday == 1
62
+ true
63
+ end
64
+
65
+ def mlk_jr_day?
66
+ return false unless self.yday == Time.nth_wday(3,1,1,self.year).yday
67
+ true
68
+ end
69
+
70
+ def memorial_day?
71
+ last_day_of_month = Time.local(self.year, 5, 31)
72
+ if last_day_of_month.wday == 0
73
+ d = 25
74
+ else
75
+ d = last_day_of_month.mday - last_day_of_month.wday + 1
76
+ end
77
+ return false unless self.yday == Time.local(self.year, 5, d).yday
78
+ true
79
+ end
80
+
81
+ def independence_day?
82
+ return false unless self.month == 7 && self.mday == 4
83
+ true
84
+ end
85
+
86
+ def labor_day?
87
+ return false unless self.yday == Time.nth_wday(1,1,9,self.year).yday
88
+ true
89
+ end
90
+
91
+ def thanksgiving?
92
+ return false unless self.yday == self.class.nth_wday(4,4,11,self.year).yday
93
+ true
94
+ end
95
+
96
+ def christmas_day?
97
+ return false unless self.month == 12 && self.mday == 25
98
+ true
99
+ end
100
+ end
101
+
102
+ Time.class_eval do
103
+ include EnterpriseTimeExtensions
104
+ extend EnterpriseTimeExtensions::ClassMethods
105
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enterprise_time_extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Miles Z. Sterrett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-09 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Enterprisey extensions to Ruby's Time class allowing you to quickly discover whether a time is a holiday, or after hours.
17
+ email: miles.sterrett@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.textile
26
+ - lib/enterprise_time_extensions.rb
27
+ - MIT-LICENSE
28
+ has_rdoc: false
29
+ homepage: http://github.com/mileszs/enterprise_time_extensions
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.1
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: Enterprisey extensions to Ruby's Time class.
54
+ test_files: []
55
+