find_week 0.0

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: 4b4ead3b3a8a091062fee69df3acdf71943451b5
4
+ data.tar.gz: 03617123fd254f1540a2282600cb151cd61ef839
5
+ SHA512:
6
+ metadata.gz: e07a54ba32bba02eba9a243c748e06d88387a573378e4951aab65c42555ec4f135a8e42e937fd8d3da6b70116fc44df5d7d64adc136c294aae4c0acfa6949b63
7
+ data.tar.gz: 1a0bc21630167b5c72b0bd0beea3072c436a52b206964638d6dac9c7c806dbaced79b6dba5150aa0686403d78079acff8244b58019931a972221f4d3ceef686f
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << "test"
5
+ t.test_files = FileList['lib/test/modules/test*.rb']
6
+ t.verbose = true
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :default => :test
data/lib/find_week.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'date'
2
+ require 'time'
3
+ require_relative 'modules/day'
4
+ require_relative 'modules/month'
5
+ require_relative 'modules/week'
6
+ require_relative 'modules/year'
7
+
8
+ class Date
9
+ include FindWeek::Day
10
+ include FindWeek::Month
11
+ include FindWeek::Week
12
+ include FindWeek::Year
13
+ end
14
+
15
+ class Time
16
+ include FindWeek::Day
17
+ include FindWeek::Month
18
+ include FindWeek::Week
19
+ include FindWeek::Year
20
+
21
+ def leap?
22
+ self.to_date.leap?
23
+ end
24
+ end
@@ -0,0 +1,49 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ # @author Navaneethan
5
+
6
+ module FindWeek
7
+ module Constant
8
+
9
+ # hash containg english words from one to seventh
10
+ WEEK_IN_ENG = { 1 => 'First', 2 => 'Second', 3 => 'Third',
11
+ 4 => 'Fourth', 5 => 'Fifth', 6 => 'Sixth', 7 => 'Seventh'}
12
+
13
+ # hash containg french words from one to seventh
14
+ WEEK_IN_FR = { 1 => 'First', 2 => 'Second', 3 => 'Third',
15
+ 4 => 'Quatrième', 5 => 'Cinquième', 6 => 'sixième', 7 => 'septième'}
16
+
17
+ # hash containg german words from one to seventh
18
+ WEEK_IN_GER = { 1 => 'First', 2 => 'Second', 3 => 'Dritten',
19
+ 4 => 'Vierte', 5 => 'Fünfte', 6 => 'Sechste', 7 => 'siebte'}
20
+
21
+ # hash containg japneese words from one to seventh
22
+ WEEK_IN_JAP = { 1 => '最初', 2 => '秒', 3 => 'サード',
23
+ 4 => '第4回', 5 => '第五', 6=> 'シックス', 7 => '第7' }
24
+
25
+ WEEK_IN_TAMIL = { 1 => 'ஒன்று', 2 => 'இரண்டு', 3 => 'மூன்று',
26
+ 4 => 'நான்கு', 5 => 'ஐந்து', 6=> 'ஆறு', 7 => 'ஏழு' }
27
+
28
+ WEEK_IN_TELUGU = { 1 => 'ఒక', 2 => 'రెండు', 3 => 'మూడు',
29
+ 4 => 'నాలుగు', 5 => 'అయిదు', 6=> 'ఆరు', 7 => 'ఏడు' }
30
+
31
+
32
+
33
+ # hash containg month name with days in that month(in non leap yaer)
34
+ MONTH_WITH_DAY = { :january => 31, :february => 28, :march => 31,
35
+ :april => 30, :may => 31, :june => 30, :july => 31,
36
+ :august => 31, :september => 30, :october => 31,
37
+ :november => 30, :december => 31 }
38
+
39
+ # hash containg month names with their sequence
40
+ MONTH_WITH_SEQUENCE = { :january => 1, :february => 2, :march => 3,
41
+ :april => 4, :may => 5, :june => 6, :july => 7,
42
+ :august => 8, :september => 9, :october => 10,
43
+ :november => 11, :december => 12 }
44
+
45
+ # array of days names ordered starting with sunday and ending with saturday.
46
+ DAYS_IN_SEQUENCE = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
47
+ "Saturday"]
48
+ end
49
+ end
@@ -0,0 +1,64 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ # @author Navaneethan
5
+
6
+ module FindWeek
7
+ module Day
8
+
9
+ # gives array of days in month
10
+ # Date.new(2012,1,1).days_array
11
+ # => [ 1, 2, 3, 4, 5, 6, 7, 8, 9,
12
+ # 10, 11, 12, 13, 14, 15, 16,
13
+ # 17, 18, 19, 20, 21, 22, 23,
14
+ # 24, 25, 26, 27, 28, 29, 30,
15
+ # 31]
16
+ # @return [Array]
17
+ def days_array
18
+ day = self.beginning_of_month.to_date.wday
19
+ array = []
20
+ array[day] = 1
21
+ (2..self.end_of_month.mday).each {|i| array << i }
22
+ array
23
+ end
24
+
25
+ # Date.new(2012,11,1).name_of_week_day
26
+ # => 'Thursday'
27
+ # @return [String]
28
+ def name_of_week_day
29
+ self.class.new(year,month,day).strftime('%A')
30
+ end
31
+
32
+ # this code generates method names like 'upcomong_monday' and 'previous_monday'
33
+ # Date.new(2013,1,1).upcoming_monday
34
+ # => #<Date: 2013-01-07 ((2456300j,0s,0n),+0s,2299161j)>
35
+ # Date.new(2013,1,1).previous_monday
36
+ # => #<Date: 2012-12-31 ((2456293j,0s,0n),+0s,2299161j)>
37
+ { 'upcoming' => '+', 'previous' => '-' }.each_pair do |key,value|
38
+ Date::DAYNAMES.each do |day_name|
39
+ name = "#{key}_#{day_name.downcase}".to_sym
40
+ check = "#{day_name.downcase}?".to_sym
41
+ define_method(name) do
42
+ date = eval "self"
43
+ if date.send(check)
44
+ if date.class == Date
45
+ date = date.send(value,7)
46
+ elsif date.class == Time
47
+ date = date.send(value,(60 * 60 * 24 * 7))
48
+ end
49
+ else
50
+ until date.send(check)
51
+ if date.class == Date
52
+ date = date.send(value,1)
53
+ elsif date.class == Time
54
+ date = date.send(value,(60 * 60 * 24))
55
+ end
56
+ end
57
+ end
58
+ date
59
+ end
60
+ end
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,68 @@
1
+ # @author Navaneethan
2
+
3
+ require_relative 'constant'
4
+
5
+ module FindWeek
6
+ module Month
7
+
8
+ include FindWeek::Constant
9
+
10
+ # this code generates method named like january?..december?
11
+ # to check whether a month is january or march? etc.
12
+ # @return [Boolean]
13
+ MONTH_WITH_DAY.keys.each do |month_name|
14
+ define_method((month_name.to_s + '?').to_sym) do
15
+ MONTH_WITH_SEQUENCE[month_name] == month
16
+ end
17
+ end
18
+
19
+ # returns day of last day of month for a given date.
20
+ # Date.new(2012,11,1).last_day_of_month
21
+ # => 30
22
+ # @return [Fixnum]
23
+ def last_day_of_month
24
+ if leap? && february?
25
+ 29
26
+ else
27
+ MONTH_WITH_DAY[MONTH_WITH_SEQUENCE.key(month)]
28
+ end
29
+ end
30
+
31
+ # returns date of last day of month for a given date.
32
+ # Date.new(2012,11,1).end_of_month
33
+ # => #<Date: 2012-11-30 ((2456262j,0s,0n),+0s,2299161j)>
34
+ # @return [Date]
35
+ def end_of_month
36
+ self.class.new(year,month,last_day_of_month)
37
+ end
38
+
39
+ # returns date of first day of month for a given date.
40
+ # Date.new(2012,11,1).beginning_of_month
41
+ # => #<Date: 2012-11-01 ((2456233j,0s,0n),+0s,2299161j)>
42
+ # @return [Date]
43
+ def beginning_of_month
44
+ self.class.new(year,month,1)
45
+ end
46
+
47
+ # returns name of month for a given date.
48
+ # Date.new(2012,11,1).name_of_month
49
+ # => "November"
50
+ # @return [String]
51
+ def name_of_month
52
+ self.class.new(year,month,day).strftime('%B')
53
+ end
54
+
55
+ # this code generates method named like 'all_mondays_in_month',
56
+ # 'all_tuesdays_in_month' etc. for all seven days of week
57
+ # Date.new(2012,11,1).all_mondays_in_month
58
+ # => [5, 12, 19, 26]
59
+ # @return [Array]
60
+ DAYS_IN_SEQUENCE.each_with_index do |day_name, i|
61
+ method_name = "all_#{day_name.downcase}s_in_month".to_sym
62
+ define_method(method_name) do
63
+ week_split.map{|d| d[i] }.compact
64
+ end
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,179 @@
1
+ # @author Navaneethan
2
+
3
+ require_relative 'constant'
4
+
5
+ module FindWeek
6
+ module Week
7
+
8
+ include FindWeek::Constant
9
+
10
+ # returns week of month for a given date
11
+ # Date.new(2012,11,15).week_of_month
12
+ # => 3
13
+ # @return [Fixnum]
14
+ def find_week
15
+ week_split.each_with_index do |o,i|
16
+ return (i + 1) if o.include?(self.day)
17
+ end
18
+ end
19
+
20
+ # checks whether the given day lies in first week of month
21
+ # Date.new(2012,11,1).first_week?
22
+ # => true
23
+ # @return [Boolean]
24
+ def first_week?
25
+ week_split[0].include?((self.day))
26
+ end
27
+
28
+ # checks whether the given day lies in second week of month
29
+ # Date.new(2012,11,8).second_week?
30
+ # => true
31
+ # @return [Boolean]
32
+ def second_week?
33
+ week_split[1].include?((self.day))
34
+ end
35
+
36
+ # checks whether the given day lies in last week of month
37
+ # Date.new(2012,11,8).last_week?
38
+ # => false
39
+ # @return [Boolean]
40
+ def last_week?
41
+ week_split.last.include?((self.day))
42
+ end
43
+
44
+ # returns total number of weeks in month
45
+ # Date.new(2012,11,8).total_weeks
46
+ # => 5
47
+ # @return [Fixnum]
48
+ def total_weeks
49
+ week_split.size
50
+ end
51
+
52
+ # checks whether the given day is saturday or sunday.
53
+ # Date.new(2012,11,8).week_end?
54
+ # => false
55
+ # @return [Boolean]
56
+ def week_end?
57
+ saturday? || sunday?
58
+ end
59
+
60
+ # checks whether the given day is not saturday or sunday.
61
+ # Date.new(2012,11,8).working_day?
62
+ # => true
63
+ # @return [Boolean]
64
+ def working_day?
65
+ !week_end?
66
+ end
67
+
68
+ # returns week split of the month for the given date
69
+ # example-
70
+ # Date.new(2012,1,1).week_split
71
+ # => [[1, 2, 3, 4, 5, 6, 7],
72
+ # [8, 9, 10, 11, 12, 13, 14],
73
+ # [15, 16, 17, 18, 19, 20, 21],
74
+ # [22, 23, 24, 25, 26, 27, 28],
75
+ # [29, 30, 31]
76
+ # @return [Array]
77
+ def week_split
78
+ days_array.each_slice(7).to_a
79
+ end
80
+
81
+ # this code generates method like 'week_of_month_eng', 'week_of_month_fr' etc.
82
+ # Date.new(2012,11,15).week_of_month_in_eng
83
+ # => 'Third'
84
+ # Date.new(2012,11,30).week_of_month_in_fr
85
+ # => "Cinquième"
86
+ # @return [String]
87
+ ['eng', 'fr', 'ger', 'jap','tamil','telugu'].each do |lang|
88
+ method_name = "find_week_in_#{lang}"
89
+ define_method(method_name) do
90
+ eval "WEEK_IN_#{lang.upcase}[find_week]"
91
+ end
92
+ end
93
+
94
+ # it returns days past in the week
95
+ # Date.new(2012,11,15).days_past_in_week
96
+ # => 4
97
+ # Time.new(2012,11,30).days_past_in_week
98
+ # => 5
99
+ # @return [Fixnum]
100
+ def days_past_in_week
101
+ self.to_date.cwday
102
+ end
103
+
104
+ # it returns days left in the week
105
+ # Date.new(2012,11,15).days_left_in_week
106
+ # => 3
107
+ # Time.new(2012,11,30).days_left_in_week
108
+ # => 2
109
+ # @return [Fixnum]
110
+ def days_left_in_week
111
+ 7 - days_past_in_week
112
+ end
113
+
114
+ # it returns date of the first day(sunday) of the week
115
+ # Date.new(2012,11,15).beginning_of_week
116
+ # => #<Date: 2012-11-12 ((2456244j,0s,0n),+0s,2299161j)>
117
+ # Time.new(2012,11,30).beginning_of_week
118
+ # => 2012-11-29 23:59:55 +0530
119
+ # @return [Date || Time]
120
+ def beginning_of_week
121
+ self.class.new(year,month,current_week.detect {|i| !i.nil?})
122
+ end
123
+
124
+ # it returns date of the last day(saturday) of the week
125
+ # Date.new(2012,11,15).end_of_week
126
+ # => #<Date: 2012-11-19 ((2456251j,0s,0n),+0s,2299161j)>
127
+ # Time.new(2012,11,30).end_of_week
128
+ # => 2012-11-30 00:00:02 +0530
129
+ # @return [Date || Time]
130
+ def end_of_week
131
+ if current_week.index(self.day) == 6
132
+ self.class.new(year,month,current_week.last)
133
+ elsif current_week.index(self.day) < 6
134
+ if self.class == Date
135
+ self + (6 - current_week.index(self.day))
136
+ elsif self.class == Time
137
+ self + (60 * 60 * 24 * (6 - current_week.index(self.day)))
138
+ end
139
+ end
140
+ end
141
+
142
+ # it returns date of the next week day.
143
+ # Date.new(2012,11,15).next_week
144
+ # => #<Date: 2012-11-22 ((2456254j,0s,0n),+0s,2299161j)>
145
+ # Time.new(2012,11,30).next_week
146
+ # => 2012-11-30 00:00:07 +0530
147
+ # @return [Date || Time]
148
+ def next_week
149
+ if self.class == Date
150
+ self + 7
151
+ elsif self.class == Time
152
+ self + (60 * 60 * 24 * 7)
153
+ end
154
+ end
155
+
156
+ # it returns date of the previous week day.
157
+ # Date.new(2012,11,15).previous_week
158
+ # => #<Date: 2012-11-08 ((2456240j,0s,0n),+0s,2299161j)>
159
+ # Time.new(2012,11,30).previous_week
160
+ # => 2012-11-29 23:59:53 +0530
161
+ # @return [Date || Time]
162
+ def previous_week
163
+ if self.class == Date
164
+ self - 7
165
+ elsif self.class == Time
166
+ self - (60 * 60 * 24 * 7)
167
+ end
168
+ end
169
+
170
+ # it returns array of days in current week.
171
+ # Date.new(2013,1,13).current_week
172
+ # => [7, 8, 9, 10, 11, 12, 13]
173
+ # @return [Array]
174
+ def current_week
175
+ week_split.select{|c| c.include?((self.day))}.flatten
176
+ end
177
+
178
+ end
179
+ end
@@ -0,0 +1,24 @@
1
+ # @author Navaneethan
2
+
3
+ module FindWeek
4
+ module Year
5
+
6
+ def self.included(klass)
7
+ klass.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ # @param[Date,Date]
12
+ # Date.years_between_dates(Date.new(2015,11,1),Date.new(2012,11,15))
13
+ # => 3
14
+ # @param[Time,Time]
15
+ # Time.years_between_dates(Time.new(2015,11,1),Time.new(2012,11,15))
16
+ # => 3
17
+ # @return [Fixnum]
18
+ def years_between_dates(date1,date2)
19
+ (date1.year - date2.year).abs
20
+ end
21
+ end
22
+
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: find_week
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.0'
5
+ platform: ruby
6
+ authors:
7
+ - Navaneethan B
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2011-04-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Find week
14
+ email: rornavaneethan@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - Gemfile
20
+ - Rakefile
21
+ - lib/modules/day.rb
22
+ - lib/modules/constant.rb
23
+ - lib/modules/month.rb
24
+ - lib/modules/week.rb
25
+ - lib/modules/year.rb
26
+ - lib/find_week.rb
27
+ homepage: https://github.com/sainavaneethan/find_week
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
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
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.0.3
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: ''
51
+ test_files: []