month_date 0.1.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +15 -0
- data/Guardfile +15 -0
- data/README.md +35 -3
- data/lib/month_date/version.rb +1 -1
- data/lib/month_date.rb +87 -14
- data/spec/month_date_spec.rb +23 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b66c962610a78457f3f9f750c8a1de3bd768fdd
|
4
|
+
data.tar.gz: 0e73047dd74fdc392700b34f14672a805a9323f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b449d975963651e21831fc1f10b45ec6c02d65c42aa7603ee0a89cae861f5f7f206a21f73b3b13fb7995ae085d859b5edf0557d953ee9ab5786d6c5c368e1dfa
|
7
|
+
data.tar.gz: bda76c5e338e66b5777316cf8fdb099102f26a0aecf0103f50964069d7d77575d3dbc44e21493a257dc55aee296291d3cdb32a5c331ecd25153bfbdf80215028
|
data/Gemfile
CHANGED
@@ -2,3 +2,18 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in month_date.gemspec
|
4
4
|
gemspec
|
5
|
+
group :test do
|
6
|
+
gem 'rake'
|
7
|
+
gem 'rspec'
|
8
|
+
end
|
9
|
+
|
10
|
+
group :development do
|
11
|
+
gem 'guard'
|
12
|
+
gem 'guard-rspec'
|
13
|
+
gem 'pry'
|
14
|
+
|
15
|
+
# Adds debugging steps to Pry
|
16
|
+
# continue, step, next
|
17
|
+
gem 'pry-remote'
|
18
|
+
gem 'pry-nav'
|
19
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
guard 'rspec', cmd: 'rspec --color --format doc' do
|
2
|
+
# watch /lib/ files
|
3
|
+
watch(%r{^lib/(.+)\.rb$}) do |m|
|
4
|
+
"spec/#{m[1]}_spec.rb"
|
5
|
+
end
|
6
|
+
|
7
|
+
watch(%r{^lib/bike_system/(.+)\.rb$}) do |m|
|
8
|
+
"spec/#{m[1]}_spec.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
# watch /spec/ files
|
12
|
+
watch(%r{^spec/(.+)\.rb$}) do |m|
|
13
|
+
"spec/#{m[1]}.rb"
|
14
|
+
end
|
15
|
+
end
|
data/README.md
CHANGED
@@ -31,9 +31,18 @@ MonthDate.dates_in_range("20141230", "20150102")
|
|
31
31
|
```
|
32
32
|
### get all dates in month
|
33
33
|
|
34
|
-
|
34
|
+
you can get all dates in specific month
|
35
35
|
|
36
|
-
|
36
|
+
MonthDate.date_in_month(year, month, format, day_type)
|
37
|
+
|
38
|
+
#### parameter
|
39
|
+
|
40
|
+
* year: year
|
41
|
+
* month: month
|
42
|
+
* format: **default: %Y%m%d** ; the date output format
|
43
|
+
* day_type: **default: false**; whether you want to seperate the weekday and weekend
|
44
|
+
|
45
|
+
**example:**
|
37
46
|
|
38
47
|
```ruby
|
39
48
|
MonthDate.date_in_month(2014, 1)
|
@@ -41,6 +50,21 @@ MonthDate.date_in_month(2014, 1)
|
|
41
50
|
=> ["20140101", "20140102", "20140103", "20140104", "20140105", "20140106", "20140107", "20140108", "20140109", "20140110", "20140111", "20140112", "20140113", "20140114", "20140115", "20140116", "20140117", "20140118", "20140119", "20140120", "20140121", "20140122", "20140123", "20140124", "20140125", "20140126", "20140127", "20140128", "20140129", "20140130", "20140131"]
|
42
51
|
```
|
43
52
|
|
53
|
+
**example: day_type=true**
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
MonthDate.date_in_month(2014, 1, %Y%m%d, true)
|
57
|
+
|
58
|
+
=>{:weekend=>["20140104", "20140105", "20140111", "20140112", "20140118", "20140119", "20140125", "20140126"], :weekday=>["20140101", "20140102", "20140103", "20140106", "20140107", "20140108", "20140109", "20140110", "20140113", "20140114", "20140115", "20140116", "20140117", "20140120", "20140121", "20140122", "20140123", "20140124", "20140127", "20140128", "20140129", "20140130", "20140131"]}
|
59
|
+
```
|
60
|
+
### date_in_month_weekday
|
61
|
+
|
62
|
+
MonthDate.date_in_month_weekday(year, month, format)
|
63
|
+
|
64
|
+
### date_in_month_weekend
|
65
|
+
|
66
|
+
MonthDate.date_in_month_weekend(year, month, format)
|
67
|
+
|
44
68
|
#### parameter
|
45
69
|
##### week
|
46
70
|
| 0 | Sunday |
|
@@ -67,6 +91,14 @@ Or install it yourself as:
|
|
67
91
|
|
68
92
|
$ gem install month_date
|
69
93
|
|
94
|
+
## Development
|
95
|
+
|
96
|
+
### Test
|
97
|
+
|
98
|
+
guard
|
99
|
+
|
100
|
+
## Todo
|
70
101
|
|
102
|
+
* Need to refactor!
|
71
103
|
## License
|
72
|
-
MIT
|
104
|
+
MIT [@ctxhou](http://github.com/ctxhou)
|
data/lib/month_date/version.rb
CHANGED
data/lib/month_date.rb
CHANGED
@@ -19,54 +19,127 @@ module MonthDate
|
|
19
19
|
Date.new(year, month, -1).day
|
20
20
|
end
|
21
21
|
|
22
|
-
def MonthDate.dates_in_range(start_date, end_date, format="%Y%m%d")
|
22
|
+
def MonthDate.dates_in_range(start_date, end_date, format="%Y%m%d", day_type=false)
|
23
23
|
start_date = Time.parse(start_date.to_s)
|
24
24
|
end_date = Time.parse(end_date.to_s)
|
25
|
-
|
25
|
+
weekend = []
|
26
|
+
weekday = []
|
26
27
|
if start_date.mon == end_date.mon && start_date.year == end_date.year
|
27
28
|
(start_date.day..end_date.day).to_a.each do |day|
|
28
29
|
date = Date.new(start_date.year, start_date.mon, day)
|
29
|
-
|
30
|
+
if self.weekend?(date)
|
31
|
+
weekend << date.strftime(format)
|
32
|
+
else
|
33
|
+
weekday << date.strftime(format)
|
34
|
+
end
|
30
35
|
end
|
31
36
|
elsif start_date.year == end_date.year
|
32
37
|
# first count the start date month date.
|
33
38
|
(start_date.day..self.days_in_month(start_date.year, start_date.mon)).to_a.each do |day|
|
34
39
|
date = Date.new(start_date.year, start_date.mon, day)
|
35
|
-
|
40
|
+
if self.weekend?(date)
|
41
|
+
weekend << date.strftime(format)
|
42
|
+
else
|
43
|
+
weekday << date.strftime(format)
|
44
|
+
end
|
36
45
|
end
|
37
46
|
(start_date.mon+1..end_date.mon-1).to_a.each do |month|
|
38
|
-
|
47
|
+
tmp = self.date_in_month(start_date.year, month, true)
|
48
|
+
weekend += tmp[:weekend]
|
49
|
+
weekday += tmp[:weekday]
|
39
50
|
end
|
40
51
|
(1..end_date.day).each do |day|
|
41
52
|
date = Date.new(end_date.year, end_date.mon, day)
|
42
|
-
|
53
|
+
if self.weekend?(date)
|
54
|
+
weekend << date.strftime(format)
|
55
|
+
else
|
56
|
+
weekday << date.strftime(format)
|
57
|
+
end
|
43
58
|
end
|
44
59
|
else
|
45
60
|
(start_date.day..self.days_in_month(start_date.year, start_date.mon)).to_a.each do |day|
|
46
61
|
date = Date.new(start_date.year, start_date.mon, day)
|
47
|
-
|
62
|
+
if self.weekend?(date)
|
63
|
+
weekend << date.strftime(format)
|
64
|
+
else
|
65
|
+
weekday << date.strftime(format)
|
66
|
+
end
|
48
67
|
end
|
49
68
|
(start_date.mon+1..12).to_a.each do |month|
|
50
|
-
|
69
|
+
tmp = self.date_in_month(start_date.year, month, format, true)
|
70
|
+
weekend += tmp[:weekend]
|
71
|
+
weekday += tmp[:weekday]
|
51
72
|
end
|
52
73
|
(1..end_date.mon-1).to_a.each do |month|
|
53
|
-
|
74
|
+
tmp = self.date_in_month(end_date.year, month, format, true)
|
75
|
+
weekend += tmp[:weekend]
|
76
|
+
weekday += tmp[:weekday]
|
54
77
|
end
|
55
78
|
(1..end_date.day).each do |day|
|
56
79
|
date = Date.new(end_date.year, end_date.mon, day)
|
57
|
-
|
80
|
+
if self.weekend?(date)
|
81
|
+
weekend << date.strftime(format)
|
82
|
+
else
|
83
|
+
weekday << date.strftime(format)
|
84
|
+
end
|
58
85
|
end
|
59
86
|
end
|
60
|
-
|
87
|
+
if day_type
|
88
|
+
return {:weekday=>weekday, :weekend=>weekend}
|
89
|
+
else
|
90
|
+
return (weekday + weekend).sort
|
91
|
+
end
|
61
92
|
end
|
62
93
|
|
63
|
-
def MonthDate.date_in_month(year, month, format="%Y%m%d")
|
94
|
+
def MonthDate.date_in_month(year, month, format="%Y%m%d", day_type=false)
|
64
95
|
days = self.days_in_month(year, month)
|
65
96
|
ary = []
|
97
|
+
result = {:weekend=>[], :weekday=>[]}
|
98
|
+
if day_type
|
99
|
+
1.upto(days) do |day|
|
100
|
+
date = Date.new(year, month, day)
|
101
|
+
if self.weekend?(date)
|
102
|
+
result[:weekend] << date.strftime(format)
|
103
|
+
else
|
104
|
+
result[:weekday] << date.strftime(format)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
return result
|
108
|
+
else
|
109
|
+
1.upto(days) do |day|
|
110
|
+
date = Date.new(year, month, day)
|
111
|
+
ary << date.strftime(format)
|
112
|
+
end
|
113
|
+
return ary
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def MonthDate.date_in_month_weekend(year, month, format="%Y%m%d")
|
118
|
+
days = self.days_in_month(year, month)
|
119
|
+
result = []
|
66
120
|
1.upto(days) do |day|
|
67
121
|
date = Date.new(year, month, day)
|
68
|
-
|
122
|
+
result << date.strftime(format) if self.weekend?(date)
|
123
|
+
end
|
124
|
+
return result
|
125
|
+
end
|
126
|
+
|
127
|
+
def MonthDate.date_in_month_weekday(year, month, format="%Y%m%d")
|
128
|
+
days = self.days_in_month(year, month)
|
129
|
+
result = []
|
130
|
+
1.upto(days) do |day|
|
131
|
+
date = Date.new(year, month, day)
|
132
|
+
result << date.strftime(format) unless self.weekend?(date)
|
133
|
+
end
|
134
|
+
return result
|
135
|
+
end
|
136
|
+
|
137
|
+
def self.weekend?(date)
|
138
|
+
wday = date.wday
|
139
|
+
if wday == 0 || wday == 6
|
140
|
+
return true
|
141
|
+
else
|
142
|
+
return false
|
69
143
|
end
|
70
|
-
return ary
|
71
144
|
end
|
72
145
|
end
|
data/spec/month_date_spec.rb
CHANGED
@@ -5,4 +5,27 @@ describe MonthDate do
|
|
5
5
|
expect(MonthDate.week_date_in_month(2014, 1, 1)).to eq ["20140106", "20140113", "20140120", "20140127"]
|
6
6
|
expect(MonthDate.week_date_in_month(2014, 1, 1, "%F")).to eq ["2014-01-06", "2014-01-13", "2014-01-20", "2014-01-27"]
|
7
7
|
end
|
8
|
+
|
9
|
+
it "test dates_in_range" do
|
10
|
+
result = MonthDate.dates_in_range(20140101, 20150205, "%Y%m%d", true)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "test date_in_month" do
|
14
|
+
answer = {:weekend=>["20140104", "20140105", "20140111", "20140112", "20140118", "20140119", "20140125", "20140126"], :weekday=>["20140101", "20140102", "20140103", "20140106", "20140107", "20140108", "20140109", "20140110", "20140113", "20140114", "20140115", "20140116", "20140117", "20140120", "20140121", "20140122", "20140123", "20140124", "20140127", "20140128", "20140129", "20140130", "20140131"]}
|
15
|
+
result = MonthDate.date_in_month(2014, 1, "%Y%m%d", true)
|
16
|
+
expect(result).to eq answer
|
17
|
+
end
|
18
|
+
|
19
|
+
it "test date_in_month_weekend" do
|
20
|
+
answer = ["20140104", "20140105", "20140111", "20140112", "20140118", "20140119", "20140125", "20140126"]
|
21
|
+
result = MonthDate.date_in_month_weekend(2014, 1, "%Y%m%d")
|
22
|
+
expect(result).to eq answer
|
23
|
+
end
|
24
|
+
|
25
|
+
it "test date_in_month_weekend" do
|
26
|
+
answer = ["20140101", "20140102", "20140103", "20140106", "20140107", "20140108", "20140109", "20140110", "20140113", "20140114", "20140115", "20140116", "20140117", "20140120", "20140121", "20140122", "20140123", "20140124", "20140127", "20140128", "20140129", "20140130", "20140131"]
|
27
|
+
result = MonthDate.date_in_month_weekday(2014, 1, "%Y%m%d")
|
28
|
+
expect(result).to eq answer
|
29
|
+
end
|
30
|
+
|
8
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: month_date
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ctxhou
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -47,6 +47,7 @@ extra_rdoc_files: []
|
|
47
47
|
files:
|
48
48
|
- ".gitignore"
|
49
49
|
- Gemfile
|
50
|
+
- Guardfile
|
50
51
|
- LICENSE.txt
|
51
52
|
- README.md
|
52
53
|
- Rakefile
|