date_utils 0.1.1
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.
- data/README +23 -0
- data/lib/date_utils.rb +195 -0
- metadata +47 -0
data/README
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# == Synopsis
|
2
|
+
# Some handy Year, Month, Week objects to make live with Date easier.
|
3
|
+
#
|
4
|
+
# == Author
|
5
|
+
# Rene Paulokat
|
6
|
+
#
|
7
|
+
# == Copyright
|
8
|
+
# Copyright (c) 2007 Rene Paulokat
|
9
|
+
#
|
10
|
+
# This program is free software; you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU General Public License as published by
|
12
|
+
# the Free Software Foundation; either version 2 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# This program is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with this program; if not, write to the Free Software
|
22
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
23
|
+
#
|
data/lib/date_utils.rb
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
# this Module prepares some handy objects to
|
2
|
+
# deal with Date 's
|
3
|
+
#--
|
4
|
+
# Copyright (c) 2007 Rene Paulokat
|
5
|
+
# This program is free software; you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation; either version 2 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
18
|
+
#
|
19
|
+
module DateUtils
|
20
|
+
# represents a timezone
|
21
|
+
#
|
22
|
+
class GMTZone
|
23
|
+
|
24
|
+
# collection of String each representing a timezone
|
25
|
+
#
|
26
|
+
def GMTZone.offsets
|
27
|
+
arr = []
|
28
|
+
(-12..13).each do |i|
|
29
|
+
if i.to_s.include?('-')
|
30
|
+
i.to_s.length == 2 ? arr.push("GMT -0" + i.to_s.split('-')[-1].to_s + ":00") : arr.push("GMT " + i.to_s + ":00")
|
31
|
+
else
|
32
|
+
i.to_s.length == 1 ? arr.push("GMT +0" + i.to_s + ":00") : arr.push("GMT +" + i.to_s + ":00")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
arr
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
# Represents a 'Week' beginning on Mondays and
|
41
|
+
# ending on Sundays
|
42
|
+
#
|
43
|
+
class Week
|
44
|
+
|
45
|
+
# the first day (Monday) of the week
|
46
|
+
attr_reader :first_day
|
47
|
+
|
48
|
+
# the last day (Sunday) of the week
|
49
|
+
attr_reader :last_day
|
50
|
+
|
51
|
+
# the num of the week
|
52
|
+
attr_reader :num_week
|
53
|
+
|
54
|
+
# the initial / regular Date instance
|
55
|
+
attr_reader :date
|
56
|
+
|
57
|
+
# the Month of the week
|
58
|
+
attr_reader :month
|
59
|
+
|
60
|
+
# create a new Week-instance with the given initial Date
|
61
|
+
#
|
62
|
+
def initialize(date)
|
63
|
+
if date.kind_of?(Date)
|
64
|
+
@month = Month.new(date)
|
65
|
+
@date = date
|
66
|
+
@num_week = date.cweek
|
67
|
+
@first_day = date - ( date.cwday - 1 )
|
68
|
+
@last_day = date + ( 7 - date.cwday )
|
69
|
+
else
|
70
|
+
raise ArgumentError, "needs Date object as input."
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# return new Week -instance one week after self
|
75
|
+
#
|
76
|
+
def next
|
77
|
+
return Week.new(@last_day + 1)
|
78
|
+
end
|
79
|
+
|
80
|
+
# returns new Week -instance one week before self
|
81
|
+
#
|
82
|
+
def previous
|
83
|
+
return Week.new(@first_day - 1)
|
84
|
+
end
|
85
|
+
|
86
|
+
# returns collection of days as Date -instances
|
87
|
+
#
|
88
|
+
def days
|
89
|
+
arr = []
|
90
|
+
@first_day.upto(@last_day) { |date| arr << date }
|
91
|
+
arr
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
# Represents a 'Month'
|
97
|
+
#
|
98
|
+
class Month
|
99
|
+
# the initial / regular Date instance
|
100
|
+
attr_reader :date
|
101
|
+
|
102
|
+
# the first day of the Month -instance
|
103
|
+
attr_reader :first_day
|
104
|
+
|
105
|
+
# the last day of the Month -instance
|
106
|
+
attr_reader :last_day
|
107
|
+
|
108
|
+
# the Month -number
|
109
|
+
attr_reader :month
|
110
|
+
|
111
|
+
# the number of days in Month
|
112
|
+
attr_reader :num_days
|
113
|
+
|
114
|
+
# create a new Month of given Date
|
115
|
+
#
|
116
|
+
def initialize(date)
|
117
|
+
if date.kind_of?(Date)
|
118
|
+
@date = date
|
119
|
+
@month = date.month
|
120
|
+
@first_day = date.mday > 1 ? (date - ( date.mday - 1)) : date
|
121
|
+
@num_days = 31 if [1,3,5,7,8,10,12].include?(@month)
|
122
|
+
@num_days = 30 if [4,6,9,11].include?(@month)
|
123
|
+
( date.leap? ? (@num_days = 29) : (@num_days = 28) ) if @month == 2
|
124
|
+
@last_day = @first_day + ( @num_days - 1 )
|
125
|
+
else
|
126
|
+
raise ArgumentError, "needs Date object as input!"
|
127
|
+
end
|
128
|
+
|
129
|
+
# returns new Month -instance one Month later than self
|
130
|
+
#
|
131
|
+
def next
|
132
|
+
return Month.new(@last_day + 1)
|
133
|
+
end
|
134
|
+
|
135
|
+
# returns a new Month -instance one Month prior to self
|
136
|
+
#
|
137
|
+
def previous
|
138
|
+
return Month.new((@first_day - 1).to_date)
|
139
|
+
end
|
140
|
+
|
141
|
+
# returns collection of days as Date -instances of self
|
142
|
+
#
|
143
|
+
def days
|
144
|
+
arr = []
|
145
|
+
@first_day.upto(@last_day) { |date| arr << date }
|
146
|
+
arr
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# represents a Year
|
153
|
+
#
|
154
|
+
class Year
|
155
|
+
# the initial Date of the Year -instance
|
156
|
+
attr_reader :date
|
157
|
+
|
158
|
+
# the Year as an Integer of self
|
159
|
+
attr_reader :year
|
160
|
+
|
161
|
+
# create a new Year -instance with given Date
|
162
|
+
#
|
163
|
+
def initialize(date)
|
164
|
+
unless date.kind_of?(Date)
|
165
|
+
raise ArgumentError, "needs Date as input!"
|
166
|
+
end
|
167
|
+
@year = date.year
|
168
|
+
end
|
169
|
+
|
170
|
+
# returns collection of Month -instances of self
|
171
|
+
#
|
172
|
+
def months
|
173
|
+
arr = []
|
174
|
+
for i in 1..12
|
175
|
+
arr.push( Month.new(Date.civil(@year,i) ) )
|
176
|
+
end
|
177
|
+
arr
|
178
|
+
end
|
179
|
+
|
180
|
+
# returns collection of Week -instances of self
|
181
|
+
# neccessarily overlaps year boundarys
|
182
|
+
#
|
183
|
+
def weeks
|
184
|
+
d = Date.civil(@year)
|
185
|
+
arr = []
|
186
|
+
week = Week.new(d)
|
187
|
+
arr.push(week)
|
188
|
+
for i in 1..52
|
189
|
+
week = week.next
|
190
|
+
arr.push(week)
|
191
|
+
end
|
192
|
+
arr
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.2
|
3
|
+
specification_version: 1
|
4
|
+
name: date_utils
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.1
|
7
|
+
date: 2007-02-28 00:00:00 +01:00
|
8
|
+
summary: Some handy utils to deal with Date
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: rene@so36.net
|
12
|
+
homepage: http://rcms.oopen.de
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Rene Paulokat
|
31
|
+
files:
|
32
|
+
- lib/date_utils.rb
|
33
|
+
- README
|
34
|
+
test_files: []
|
35
|
+
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- README
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
requirements: []
|
45
|
+
|
46
|
+
dependencies: []
|
47
|
+
|