shifty_week 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Scott Noel-Hemming
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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
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.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = shifty_week
2
+
3
+ Description in ShiftyWeek[link:classes/ShiftyWeek.html]
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Scott Noel-Hemming. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,83 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'init'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "shifty_week"
9
+ gem.summary = ShiftyWeek::Constants::SUMMARY
10
+ gem.description = ShiftyWeek::Constants::DESCRIPTION
11
+ gem.email = "frogstarr78@gmail.com"
12
+ gem.homepage = "http://github.com/frogstarr78/shifty_week"
13
+ gem.authors = ["Scott Noel-Hemming"]
14
+ gem.rdoc_options << '--main' << 'ShiftyWeek' << '--line-numbers'
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/*_test.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/*_test.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ begin
45
+ require 'reek/rake_task'
46
+ Reek::RakeTask.new do |t|
47
+ t.fail_on_error = true
48
+ t.verbose = false
49
+ t.source_files = 'lib/**/*.rb'
50
+ end
51
+ rescue LoadError
52
+ task :reek do
53
+ abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
54
+ end
55
+ end
56
+
57
+ begin
58
+ require 'roodi'
59
+ require 'roodi_task'
60
+ RoodiTask.new do |t|
61
+ t.verbose = false
62
+ end
63
+ rescue LoadError
64
+ task :roodi do
65
+ abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
66
+ end
67
+ end
68
+
69
+ task :default => :test
70
+
71
+ require 'rake/rdoctask'
72
+ Rake::RDocTask.new do |rdoc|
73
+ if File.exist?('VERSION')
74
+ version = File.read('VERSION')
75
+ else
76
+ version = ""
77
+ end
78
+
79
+ rdoc.rdoc_dir = 'rdoc'
80
+ rdoc.title = "shifty_week #{version}"
81
+ rdoc.rdoc_files.include('README*')
82
+ rdoc.rdoc_files.include('lib/**/*.rb')
83
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1,9 @@
1
+ # Include hook code here
2
+
3
+ require 'time'
4
+ require 'date'
5
+
6
+ root_dir = File.dirname(__FILE__)
7
+ require File.join(root_dir, 'lib', 'shifty_week')
8
+ require File.join(root_dir, 'lib', 'shifty_week', 'time')
9
+ require File.join(root_dir, 'lib', 'shifty_week', 'date')
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,34 @@
1
+ class Date
2
+ include ShiftyWeek
3
+ attr_writer :week_day_start
4
+
5
+ alias_method :'weekless_>>', :'>>'
6
+ def >> int
7
+ new_day = send(:'weekless_>>', int)
8
+ new_day.week_day_start = self.week_day_start
9
+ new_day
10
+ end
11
+
12
+ alias_method :step_to_date, :step #:nodoc:
13
+ # Seemed to make more sense to me to have step accept an integer number
14
+ # see step_to_date for previous behavior. Although, you can still pass
15
+ # a datetime object to this method and it will call the previous behavior.
16
+ def step(limit, step=1)
17
+ method = 'step_to_date'
18
+ if limit.is_a?(Integer)
19
+ limit = (self + limit)-1
20
+ method = 'step'
21
+ end
22
+ send(method, limit, step) {|d|
23
+ d.week_day_start = self.week_day_start
24
+ yield d
25
+ }
26
+ end
27
+ end
28
+
29
+ class DateTime < Date
30
+ include ShiftyWeek
31
+ def to_time
32
+ Time.parse(self.strftime("%c -0800"), self)
33
+ end
34
+ end
@@ -0,0 +1,22 @@
1
+ class Time
2
+ include ShiftyWeek
3
+ def >> int
4
+ # r = self
5
+ # (int < 0 ? int*-1 : int).times do
6
+ # r = r.send( (int < 0 ? "-" : "+"), (DAYS(r.days_in_month)) )
7
+ # end
8
+ # r += HOUR if r.dst?
9
+ # r
10
+ DateTime.parse(self.strftime(WORKER_FORMAT)) >> int
11
+ end
12
+
13
+ def << int
14
+ # r = self
15
+ # (int < 0 ? int*-1 : int).times do
16
+ # r = r.send( (int < 0 ? "+" : "-"), (DAYS(r.days_in_month)) )
17
+ # end
18
+ # r += HOUR if r.dst?
19
+ # r
20
+ DateTime.parse(self.strftime(WORKER_FORMAT)) << int
21
+ end
22
+ end
@@ -0,0 +1,181 @@
1
+ # Normally a calendar displayed by ruby for October 2009 would be appear:
2
+ #
3
+ # (Using cwday)
4
+ # Mo Tu We Th Fr Sa Su
5
+ # 1 2 3 4
6
+ # 5 6 7 8 9 10 11
7
+ # 12 13 14 15 16 17 18
8
+ # 19 20 21 22 23 24 25
9
+ # 26 27 28 29 30 31
10
+ #
11
+ # (Using wday)
12
+ # Su Mo Tu We Th Fr Sa
13
+ # 1 2 3
14
+ # 4 5 6 7 8 9 10
15
+ # 11 12 13 14 15 16 17
16
+ # 18 19 20 21 22 23 24
17
+ # 25 26 27 28 29 30 31
18
+ #
19
+ # With this gem you can set a week_day_start.
20
+ #
21
+ # In this example to Wed.
22
+ #
23
+ # And the calendar will be displayed::
24
+ #
25
+ # We Th Fr Sa Su Mo Tu
26
+ # 1 2 3 4 5 6
27
+ # 7 8 9 10 11 12 13
28
+ # 14 15 16 17 18 19 20
29
+ # 21 22 23 24 25 26 27
30
+ # 28 29 30 31
31
+
32
+ module ShiftyWeek
33
+ class Constants # :nodoc:
34
+ SUMMARY = %q{Calculate dates based on a configurable first day of the week.}
35
+ DESCRIPTION = %q{Calculate dates based on a configurable first day of the week. If you want
36
+ the first day of the week to be Wednesday and the current calendar Year-Month are 2009-10,
37
+ DateTime#week_days will return an array of DateTime objects which when formatted with "%Y-%m-%d %a" look like
38
+ ["2009-10-14 Wed", "2009-10-15 Thu", "2009-10-16 Fri", "2009-10-17 Sat", "2009-10-18 Sun", "2009-10-19 Mon", "2009-10-20 Tue"] }
39
+ end
40
+
41
+ # TODO: Implement Quarters
42
+ # TODO: Implement human_diff method 2009-02-01 - 2009-01-01 #=> 1 month or 31 days, etc...
43
+ # def human_diff o, options = {}
44
+ # d = (self.day - o.day)*-1
45
+ # h = self.day < o.day ? ' ago' : ' til'
46
+ # r = "#{d} day".send(d > 1 ? :pluralize : :singularize)
47
+ # r << h if options[:historical]
48
+ # r
49
+ # end
50
+
51
+ # Calculate the last week day for the month
52
+ def last_week_day
53
+ DateTime.new(self.year, self.month, self.days_in_month).wday
54
+ end
55
+
56
+ # Instance method for retreiving the number of days in this month
57
+ def days_in_month
58
+ Time.send("month_days", self.year, self.month)
59
+ end
60
+
61
+ # alculate the number of weeks in a year taking into account "shifted" weeks
62
+ def weeks_in_year
63
+ return (self-(self.yday-1)).wday == 6 ? 54 : 53
64
+ end
65
+
66
+ # Get the offset of this object's date, to the first day of the week.
67
+ def wday_offset()
68
+ _week_day_numbers.index(self.wday)
69
+ end
70
+
71
+ # Convenience method
72
+ def month_name
73
+ DateTime::MONTHNAMES[self.month]
74
+ end
75
+
76
+ # Convenience method
77
+ def month_names
78
+ self.class.month_names
79
+ end
80
+
81
+ # Get the week number for the current object
82
+ def week
83
+ working_date = DateTime.new(self.year, 1, 1)
84
+ working_date.week_day_start = self.week_day_start
85
+ working_date = (working_date-working_date.send("wday_offset"))
86
+ week_num = 0
87
+ working_date.step(self) { |d|
88
+ if d.wday == _week_day_numbers.first
89
+ week_num += 1
90
+ end
91
+ }
92
+ week_num
93
+ end
94
+
95
+ # TODO: Implement weeks method
96
+ # def weeks
97
+ # DateTime.new(self.year, self.month).step(self.days_in_month, 7) { |d|
98
+ # week_days d.day
99
+ # }
100
+ # end
101
+
102
+ # Accessor for first week day of the calendar
103
+ def week_day_start
104
+ @week_day_start || 0
105
+ end
106
+
107
+ # Get the days of the week based on the current week_day_start
108
+ def week_days(options={}, &block)
109
+ start_date = self
110
+ result = []
111
+ (start_date-wday_offset).step 7 do |d|
112
+ d.week_day_start = self.week_day_start
113
+ if block_given?
114
+ yield d
115
+ else
116
+ result.push(d)
117
+ end
118
+ end
119
+ result
120
+ end
121
+
122
+ # TODO: Implement month_weeks method
123
+ # def month_weeks(&block)
124
+ # result = []
125
+ # (self - (self.day-1)).step(self.days_in_month+(6-self.last_week_day), 7) { |d|
126
+ # if block
127
+ # yield d
128
+ # else
129
+ # result.push(d.week)
130
+ # end
131
+ # }
132
+ # result
133
+ # end
134
+
135
+ # Hopefully the name explains what this method does
136
+ def step_to_month_end step = 1
137
+ days_in_month = self.days_in_month
138
+ days_in_month += self.wday_offset if self.days_in_month%(7*4) >= 0
139
+ self.step(days_in_month, step) do |date|
140
+ date.week_day_start = self.week_day_start
141
+ yield date
142
+ end
143
+ end
144
+
145
+ class << self
146
+
147
+ # Let's generate some helpers
148
+ def included in_class
149
+ in_class.class.send :define_method, :day_names, proc {
150
+ (
151
+ (0..6).to_a +
152
+ Date::DAYNAMES +
153
+ Date::ABBR_DAYNAMES +
154
+ Date::ABBR_DAYNAMES.collect {|wday| wday[0,2] }
155
+ )
156
+ }
157
+
158
+ in_class.class.send :define_method, :month_names, proc {
159
+ (
160
+ (0..12).to_a +
161
+ DateTime::MONTHNAMES +
162
+ DateTime::ABBR_MONTHNAMES
163
+ )
164
+ }
165
+ end
166
+ end
167
+
168
+ private
169
+ # Calculate the dates within a week range taking into account "shifted" weeks
170
+ def _week_day_numbers
171
+ week_day_start = self.week_day_start
172
+ week_day_start.capitalize if week_day_start.is_a? String
173
+ [0, 1, 2, 3, 4, 5, 6].partition {|on| on >= day_names.index(week_day_start)%7 }.flatten
174
+ end
175
+
176
+ # Simplifying access to day_names/numbers
177
+ def day_names
178
+ self.class.day_names
179
+ end
180
+ end
181
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :calendar do
3
+ # # Task goes here
4
+ # end
data/test/date_test.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'test/test_helper'
2
+
3
+ class DateTest < Test::Unit::TestCase
4
+ def test_step
5
+ date = Date.strptime Time.now.strftime("%Y-%m-%d")
6
+ future_date = date+6
7
+ received_date = date
8
+ date.step(7){ |d| received_date = d }
9
+ assert_equal(received_date, future_date, "un-equal dates: #{date.to_s} #{future_date.to_s}")
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ require 'test/test_helper'
2
+
3
+ class DateTimeTest < Test::Unit::TestCase
4
+ def test_to_time
5
+ now = DateTime.now.to_time
6
+ assert_kind_of Time, now
7
+ end
8
+ end
@@ -0,0 +1,357 @@
1
+ require 'test_helper'
2
+
3
+ class ShiftyWeekTest < Test::Unit::TestCase
4
+ def test_week_starting_sunday
5
+ month_starts = [
6
+ %w(Su Mo Tu We Th Fr Sa
7
+ \ \ 1 2 3 4 5
8
+ 6 7 8 9 10 11 12
9
+ 13 14 15 16 17 18 19
10
+ 20 21 22 23 24 25 26
11
+ 27 28 29 30 31),
12
+
13
+ %w(Mo Tu We Th Fr Sa Su
14
+ \ 1 2 3 4 5 6
15
+ 7 8 9 10 11 12 13
16
+ 14 15 16 17 18 19 20
17
+ 21 22 23 24 25 26 27
18
+ 28 29 30 31),
19
+
20
+ %w(Tu We Th Fr Sa Su Mo
21
+ 1 2 3 4 5 6 7
22
+ 8 9 10 11 12 13 14
23
+ 15 16 17 18 19 20 21
24
+ 22 23 24 25 26 27 28
25
+ 29 30 31),
26
+
27
+ %w(We Th Fr Sa Su Mo Tu
28
+ \ \ \ \ \ \ 1
29
+ 2 3 4 5 6 7 8
30
+ 9 10 11 12 13 14 15
31
+ 16 17 18 19 20 21 22
32
+ 23 24 25 26 27 28 29
33
+ 30 31),
34
+
35
+ %w(Th Fr Sa Su Mo Tu We
36
+ \ \ \ \ \ 1 2
37
+ 3 4 5 6 7 8 9
38
+ 10 11 12 13 14 15 16
39
+ 17 18 19 20 21 22 23
40
+ 24 25 26 27 28 29 30
41
+ 31),
42
+
43
+ %w(Fr Sa Su Mo Tu We Th
44
+ \ \ \ \ 1 2 3
45
+ 4 5 6 7 8 9 10
46
+ 11 12 13 14 15 16 17
47
+ 18 19 20 21 22 23 24
48
+ 25 26 27 28 29 30 31),
49
+
50
+ %w(Sa Su Mo Tu We Th Fr
51
+ \ \ \ 1 2 3 4
52
+ 5 6 7 8 9 10 11
53
+ 12 13 14 15 16 17 18
54
+ 19 20 21 22 23 24 25
55
+ 26 27 28 29 30 31)
56
+ ]
57
+ end
58
+
59
+ def test_week
60
+
61
+ date = DateTime.new(2008, 1, 6)
62
+ assert_equal(0, date.send("wday_offset"))
63
+ assert_equal(2, date.week, "week #{date.week} on date #{date.to_s} != 2")
64
+
65
+ date = date-1
66
+ assert_equal(6, date.send("wday_offset"))
67
+ assert_equal(1, date.week, "week #{date.week} on date #{date.to_s} != 2")
68
+
69
+ years_days = {
70
+ 2000 => 2,
71
+ 2001 => 7,
72
+ 2002 => 6,
73
+ 2003 => 5,
74
+ 2004 => 4,
75
+ 2005 => 2,
76
+ 2006 => 8,
77
+ 2007 => 7,
78
+ 2008 => 6,
79
+ 2010 => 3,
80
+ }.each { |year, day|
81
+ date = DateTime.new(year, 1, day)
82
+ assert_equal(2, date.week, "week #{date.week} on date #{date.to_s} != 1")
83
+ }
84
+
85
+ date = DateTime.new(2008, 1, 3)
86
+ date.week_day_start = 'Wed'
87
+ assert_equal(1, date.send("wday_offset"))
88
+ assert_equal(2, date.week, "week #{date.week} on date #{date.to_s} != 2")
89
+
90
+ date = date-1
91
+ date.week_day_start = 'Wed'
92
+ assert_equal(0, date.send("wday_offset"))
93
+ assert_equal("2008-01-02", date.strftime("%Y-%m-%d"))
94
+ assert_equal(2, date.week, "week #{date.week} on date #{date.to_s} != 2 with wday_start #{date.week_day_start}")
95
+
96
+ date = date-1
97
+ date.week_day_start = 'Wed'
98
+ assert_equal(6, date.send("wday_offset"))
99
+ assert_equal("2008-01-01", date.strftime("%Y-%m-%d"))
100
+ assert_equal(1, date.week, "week #{date.week} on date #{date.to_s} != 1 with wday_start #{date.week_day_start}")
101
+
102
+ date = DateTime.new(2007)
103
+ date.week_day_start = 'Sunday'
104
+ assert_equal(1, date.week, "week #{date.week} on date #{date.to_s} != 1")
105
+ assert_equal(1, (date+5).week, "week #{(date+5).week} on date #{(date+5).to_s} != 1")
106
+ date = DateTime.new(2007, 8, 25)
107
+ assert_equal(34, date.week, "week #{date.week} on date #{date.to_s} != 34")
108
+ date = (date+7)
109
+ # date = DateTime.new(2007, 9, 1)
110
+ assert_equal(35, date.week, "week #{date.week} on date #{date.to_s} != 35")
111
+
112
+ years_days = {
113
+ 1998 => 53,
114
+ 1999 => 53,
115
+ 2000 => 54,
116
+ 2001 => 53,
117
+ 2002 => 53,
118
+ 2003 => 53,
119
+ 2004 => 53,
120
+ 2005 => 53,
121
+ 2006 => 53,
122
+ 2007 => 53,
123
+ 2008 => 53,
124
+ 2009 => 53,
125
+ 2010 => 53,
126
+ 2011 => 53,
127
+ 2012 => 53,
128
+ 2013 => 53,
129
+ 2014 => 53,
130
+ 2015 => 53,
131
+ 2016 => 53,
132
+ }.each { |year, weeks|
133
+ date = DateTime.new(year, 12, 31)
134
+ assert_equal(weeks, date.week, "week #{date.week} in year #{year} != 1")
135
+ }
136
+
137
+ for year in (2000..2006)
138
+ day = 1
139
+ date = DateTime.new(year, 1, day)
140
+ assert_equal(1, date.week, "week #{date.week} on date #{date.to_s} != 1")
141
+ # end
142
+ end
143
+
144
+ # date = DateTime.new(2003, 1, 1)
145
+ # week = date.week(4)
146
+ # assert_equal(week, 1, "week #{week} on date #{date.to_s} != 1")
147
+ # date = DateTime.new(2003, 1, 2)
148
+ # week = date.week(4)
149
+ # assert_equal(week, 2, "week #{week} on date #{date.to_s} != 2")
150
+ # week = date.week(10)
151
+ # assert_equal(week, 2, "week #{week} on date #{date.to_s} != 2")
152
+ # date = DateTime.new(2003, 1, 3)
153
+ # week = date.week(4)
154
+ # assert_equal(week, 2, "week #{week} on date #{date.to_s} != 2")
155
+ end
156
+
157
+ def test_weeks_in_year
158
+ years_days = {
159
+ 2000 => 54,
160
+ 2001 => 53,
161
+ 2002 => 53,
162
+ 2003 => 53,
163
+ 2004 => 53,
164
+ 2005 => 54,
165
+ 2006 => 53,
166
+ 2007 => 53,
167
+ 2008 => 53,
168
+ 2009 => 53,
169
+ 2010 => 53,
170
+ 2011 => 54,
171
+ 2012 => 53,
172
+ 2013 => 53,
173
+ 2014 => 53,
174
+ 2015 => 53,
175
+ }.each { |year, weeks|
176
+ date = DateTime.new(year, 12, 31)
177
+ assert_equal weeks, date.weeks_in_year, "week #{date.week} on date #{date.to_s} != 1"
178
+ }
179
+ end
180
+
181
+ def test_wday_offset
182
+ date = DateTime.new(2007)
183
+ expected_value_map = [1, 0, 6, 5, 4, 3, 2]
184
+ date.send("day_names").each_with_index {|wday, index|
185
+ date.week_day_start = wday
186
+ assert_equal(expected_value_map[index%7], date.send("wday_offset"))
187
+ }
188
+ end
189
+
190
+ def test_week_days
191
+ date = DateTime.new(2007)
192
+ assert_equal([
193
+ DateTime.new(2006, 12, 31).to_s,
194
+ DateTime.new(2007, 1, 1).to_s,
195
+ DateTime.new(2007, 1, 2).to_s,
196
+ DateTime.new(2007, 1, 3).to_s,
197
+ DateTime.new(2007, 1, 4).to_s,
198
+ DateTime.new(2007, 1, 5).to_s,
199
+ DateTime.new(2007, 1, 6).to_s
200
+ ],
201
+ date.week_days.collect {|date| date.to_s}
202
+ )
203
+ date = DateTime.new(2007)
204
+ date.week_day_start = 'Saturday'
205
+ assert_equal([
206
+ DateTime.new(2006, 12, 30).to_s,
207
+ DateTime.new(2006, 12, 31).to_s,
208
+ DateTime.new(2007, 1, 1).to_s,
209
+ DateTime.new(2007, 1, 2).to_s,
210
+ DateTime.new(2007, 1, 3).to_s,
211
+ DateTime.new(2007, 1, 4).to_s,
212
+ DateTime.new(2007, 1, 5).to_s,
213
+ ], date.week_days.collect {|date| date.to_s}
214
+ )
215
+
216
+ date = DateTime.new(2008)
217
+ date.week_day_start = 'Tue'
218
+ assert_equal([
219
+ DateTime.new(2008, 1, 1).to_s,
220
+ DateTime.new(2008, 1, 2).to_s,
221
+ DateTime.new(2008, 1, 3).to_s,
222
+ DateTime.new(2008, 1, 4).to_s,
223
+ DateTime.new(2008, 1, 5).to_s,
224
+ DateTime.new(2008, 1, 6).to_s,
225
+ DateTime.new(2008, 1, 7).to_s,
226
+ ], date.week_days.collect {|date| date.to_s}
227
+ )
228
+ date = DateTime.new(2008)
229
+ date.week_day_start = 'Wed'
230
+ assert_equal([
231
+ DateTime.new(2007, 12, 26).to_s,
232
+ DateTime.new(2007, 12, 27).to_s,
233
+ DateTime.new(2007, 12, 28).to_s,
234
+ DateTime.new(2007, 12, 29).to_s,
235
+ DateTime.new(2007, 12, 30).to_s,
236
+ DateTime.new(2007, 12, 31).to_s,
237
+ DateTime.new(2008, 1, 1).to_s,
238
+ ], date.week_days.collect {|date| date.to_s}
239
+ )
240
+
241
+
242
+ date = DateTime.new(2003)
243
+ assert_equal([
244
+ DateTime.new(2002, 12, 29).to_s,
245
+ DateTime.new(2002, 12, 30).to_s,
246
+ DateTime.new(2002, 12, 31).to_s,
247
+ DateTime.new(2003, 1, 1).to_s,
248
+ DateTime.new(2003, 1, 2).to_s,
249
+ DateTime.new(2003, 1, 3).to_s,
250
+ DateTime.new(2003, 1, 4).to_s,
251
+ ],
252
+ date.week_days.collect {|date| date.to_s}
253
+ )
254
+ date = DateTime.new(2007, 12, 31)
255
+ assert_equal([
256
+ DateTime.new(2007, 12, 30).to_s,
257
+ DateTime.new(2007, 12, 31).to_s,
258
+ DateTime.new(2008, 1, 1).to_s,
259
+ DateTime.new(2008, 1, 2).to_s,
260
+ DateTime.new(2008, 1, 3).to_s,
261
+ DateTime.new(2008, 1, 4).to_s,
262
+ DateTime.new(2008, 1, 5).to_s,
263
+ ],
264
+ date.week_days.collect {|date| date.to_s}
265
+ )
266
+ assert_equal('2008-01-05', date.strftime("%Y-%m-%d"))
267
+ date.week_day_start = 2
268
+ assert_equal([
269
+ DateTime.new(2008, 1, 1).to_s,
270
+ DateTime.new(2008, 1, 2).to_s,
271
+ DateTime.new(2008, 1, 3).to_s,
272
+ DateTime.new(2008, 1, 4).to_s,
273
+ DateTime.new(2008, 1, 5).to_s,
274
+ DateTime.new(2008, 1, 6).to_s,
275
+ DateTime.new(2008, 1, 7).to_s,
276
+ ],
277
+ date.week_days.collect {|date| date.to_s }
278
+ )
279
+
280
+ assert_equal('2008-01-07', date.strftime("%Y-%m-%d"))
281
+ date.week_day_start = 'We'
282
+ assert_equal([
283
+ DateTime.new(2008, 1, 2).to_s,
284
+ DateTime.new(2008, 1, 3).to_s,
285
+ DateTime.new(2008, 1, 4).to_s,
286
+ DateTime.new(2008, 1, 5).to_s,
287
+ DateTime.new(2008, 1, 6).to_s,
288
+ DateTime.new(2008, 1, 7).to_s,
289
+ DateTime.new(2008, 1, 8).to_s,
290
+ ],
291
+ date.week_days.collect {|date| date.to_s }
292
+ )
293
+ end
294
+
295
+ def test_step_to_month_end
296
+ # expected_display = {
297
+ # %w(January 2009) =>
298
+ # %w(Su Mo Tu We Th Fr Sa
299
+ # 28 29 30 31 1 2 3
300
+ # 4 5 6 7 8 9 10
301
+ # 11 12 13 14 15 16 17
302
+ # 18 19 20 21 22 23 24
303
+ # 25 26 27 28 29 30 31)
304
+ # }
305
+ expected_display = {
306
+ %w(January 2009) =>
307
+ %w(
308
+ Th Fr Sa Su Mo Tu We
309
+ 1 2 3 4 5 6 7
310
+ 8 9 10 11 12 13 14
311
+ 15 16 17 18 19 20 21
312
+ 22 23 24 25 26 27 28
313
+ 29 30 31 1 2 3 4
314
+ )
315
+ }
316
+
317
+ jan_09 = DateTime.new(2009, 1)
318
+ days_in_month = []
319
+ jan_09.step_to_month_end do |date|
320
+ days_in_month << date.day
321
+ end
322
+ assert_equal 35, days_in_month.size
323
+ assert_equal expected_display[%w(January 2009)][7..-1], days_in_month.collect(&:to_s)
324
+ end
325
+
326
+ def test_month_name
327
+ assert_equal 'January', DateTime.new(2009, 01).month_name
328
+ end
329
+
330
+ # def test_month_weeks
331
+ # date = DateTime.new(2007, 11, 17)
332
+ # assert_equal([44, 45, 46, 47, 48], date.month_weeks, "")
333
+ # date = DateTime.new(2007, 1, 1)
334
+ # assert_equal([1, 2, 3, 4, 5], date.month_weeks, "")
335
+ # date = DateTime.new(2007, 8, 1)
336
+ # assert_equal([31, 32, 33, 34, 35], date.month_weeks, "")
337
+ # date = DateTime.new(2007, 9, 30)
338
+ # assert_equal([35, 36, 37, 38, 39, 40], date.month_weeks, "")
339
+ # date = DateTime.new(2007, 12, 31)
340
+ # assert_equal([48, 49, 50, 51, 52, 53], date.month_weeks, "")
341
+ # end
342
+
343
+ def test_last_week_day
344
+ assert_equal(DateTime.new(2007, 1).last_week_day, 3);
345
+ assert_equal(DateTime.new(2007, 2).last_week_day, 3);
346
+ assert_equal(DateTime.new(2007, 3).last_week_day, 6);
347
+ assert_equal(DateTime.new(2007, 4).last_week_day, 1);
348
+ assert_equal(DateTime.new(2007, 5).last_week_day, 4);
349
+ assert_equal(DateTime.new(2007, 6).last_week_day, 6);
350
+ assert_equal(DateTime.new(2007, 7).last_week_day, 2);
351
+ assert_equal(DateTime.new(2007, 8).last_week_day, 5);
352
+ assert_equal(DateTime.new(2007, 9).last_week_day, 0);
353
+ assert_equal(DateTime.new(2007, 10).last_week_day, 3);
354
+ assert_equal(DateTime.new(2007, 11).last_week_day, 5);
355
+ assert_equal(DateTime.new(2007, 12).last_week_day, 1);
356
+ end
357
+ end
@@ -0,0 +1,7 @@
1
+ require 'init'
2
+ require 'test/unit'
3
+
4
+ require 'shifty_week'
5
+
6
+ class Test::Unit::TestCase
7
+ end
data/test/time_test.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'test/test_helper'
2
+
3
+ class TimeTest < Test::Unit::TestCase
4
+ def test_shift_for_time
5
+ format = "%c"
6
+ assert_respond_to Time.now, ">>"
7
+ assert_respond_to Time.now, "<<"
8
+
9
+ assert_equal (DateTime.parse(Time.now.strftime(Time::WORKER_FORMAT))<<1).strftime(format), (Time.now<<1).strftime(format)
10
+ assert_equal (DateTime.parse(Time.now.strftime(Time::WORKER_FORMAT))>>1).strftime(format), (Time.now>>1).strftime(format)
11
+
12
+ assert_equal (Time.local(2008, 12, 1)).strftime(format), (Time.local(2008, 10, 1)>>2).strftime(format)
13
+ assert_equal (Time.utc(2008, 12, 1)).strftime(format), (Time.utc(2008, 10, 1)>>2).strftime(format)
14
+ end
15
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shifty_week
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Scott Noel-Hemming
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-19 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: "Calculate dates based on a configurable first day of the week. If you want\n the first day of the week to be Wednesday and the current calendar Year-Month are 2009-10,\n DateTime#week_days will return an array of DateTime objects which when formatted with \"%Y-%m-%d %a\" look like \n [\"2009-10-14 Wed\", \"2009-10-15 Thu\", \"2009-10-16 Fri\", \"2009-10-17 Sat\", \"2009-10-18 Sun\", \"2009-10-19 Mon\", \"2009-10-20 Tue\"] "
17
+ email: frogstarr78@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - MIT-LICENSE
30
+ - README.rdoc
31
+ - Rakefile
32
+ - VERSION
33
+ - init.rb
34
+ - install.rb
35
+ - lib/shifty_week.rb
36
+ - lib/shifty_week/date.rb
37
+ - lib/shifty_week/time.rb
38
+ - tasks/calendar_tasks.rake
39
+ - test/date_test.rb
40
+ - test/date_time_test.rb
41
+ - test/shifty_week_test.rb
42
+ - test/test_helper.rb
43
+ - test/time_test.rb
44
+ - uninstall.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/frogstarr78/shifty_week
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ - --main
53
+ - ShiftyWeek
54
+ - --line-numbers
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Calculate dates based on a configurable first day of the week.
76
+ test_files:
77
+ - test/time_test.rb
78
+ - test/shifty_week_test.rb
79
+ - test/date_time_test.rb
80
+ - test/date_test.rb
81
+ - test/test_helper.rb