回答編集履歴
1
テストコードを追加
test
CHANGED
@@ -119,3 +119,157 @@
|
|
119
119
|
|
120
120
|
|
121
121
|
また、余計なお世話ですが、年齢通りに進学しない人もいますし、大学に行かない人も、逆に大学を出てすぐに社会人にならない人もいますので、使いみちによってはその辺の配慮も必要かと存じます。
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
---
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
せっかく書いたので、テストコードを載せておきます。
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
|
135
|
+
# test_sample.rb
|
136
|
+
|
137
|
+
require 'test/unit'
|
138
|
+
|
139
|
+
require 'date'
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
class Sample
|
144
|
+
|
145
|
+
def self.age(birthday, today)
|
146
|
+
|
147
|
+
(today.strftime('%Y%m%d').to_i - birthday.strftime('%Y%m%d').to_i) / 10000
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
def self.schoolYear(birthday, today = Date.today)
|
154
|
+
|
155
|
+
toDate = Time.mktime(today.year - (today.month < 4 ? 1 : 0), 4, 1)
|
156
|
+
|
157
|
+
(toDate.strftime('%Y%m%d').to_i - birthday.strftime('%Y%m%d').to_i) / 10000
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
class TestSample < Test::Unit::TestCase
|
166
|
+
|
167
|
+
def test_age
|
168
|
+
|
169
|
+
assert_equal 20, Sample.age(Time.mktime(2000, 1, 1), Time.mktime(2020, 1, 1))
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
assert_equal 19, Sample.age(Time.mktime(2000, 1, 2), Time.mktime(2020, 1, 1))
|
174
|
+
|
175
|
+
assert_equal 20, Sample.age(Time.mktime(2000, 1, 2), Time.mktime(2020, 1, 2))
|
176
|
+
|
177
|
+
assert_equal 20, Sample.age(Time.mktime(2000, 1, 2), Time.mktime(2020, 1, 3))
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
assert_equal 19, Sample.age(Time.mktime(2000, 2, 28), Time.mktime(2020, 2, 27))
|
182
|
+
|
183
|
+
assert_equal 20, Sample.age(Time.mktime(2000, 2, 28), Time.mktime(2020, 2, 28))
|
184
|
+
|
185
|
+
assert_equal 20, Sample.age(Time.mktime(2000, 2, 28), Time.mktime(2020, 2, 29))
|
186
|
+
|
187
|
+
assert_equal 20, Sample.age(Time.mktime(2000, 2, 28), Time.mktime(2020, 3, 1))
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
assert_equal 19, Sample.age(Time.mktime(2000, 2, 29), Time.mktime(2020, 2, 28))
|
192
|
+
|
193
|
+
assert_equal 20, Sample.age(Time.mktime(2000, 2, 29), Time.mktime(2020, 2, 29))
|
194
|
+
|
195
|
+
assert_equal 20, Sample.age(Time.mktime(2000, 2, 29), Time.mktime(2020, 3, 1))
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
assert_equal 18, Sample.age(Time.mktime(2001, 2, 28), Time.mktime(2020, 2, 27))
|
200
|
+
|
201
|
+
assert_equal 19, Sample.age(Time.mktime(2001, 2, 28), Time.mktime(2020, 2, 28))
|
202
|
+
|
203
|
+
assert_equal 19, Sample.age(Time.mktime(2001, 2, 28), Time.mktime(2020, 2, 29))
|
204
|
+
|
205
|
+
assert_equal 19, Sample.age(Time.mktime(2001, 2, 28), Time.mktime(2020, 3, 1))
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
assert_equal 20, Sample.age(Time.mktime(2000, 2, 29), Time.mktime(2021, 2, 27))
|
210
|
+
|
211
|
+
assert_equal 20, Sample.age(Time.mktime(2000, 2, 29), Time.mktime(2021, 2, 28))
|
212
|
+
|
213
|
+
assert_equal 21, Sample.age(Time.mktime(2000, 2, 29), Time.mktime(2021, 3, 1))
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
def test_schoolYear
|
220
|
+
|
221
|
+
assert_equal 20, Sample.schoolYear(Time.mktime(2000, 1, 1))
|
222
|
+
|
223
|
+
assert_equal 20, Sample.schoolYear(Time.mktime(2000, 2, 29))
|
224
|
+
|
225
|
+
assert_equal 20, Sample.schoolYear(Time.mktime(2000, 3, 31))
|
226
|
+
|
227
|
+
assert_equal 20, Sample.schoolYear(Time.mktime(2000, 4, 1))
|
228
|
+
|
229
|
+
assert_equal 19, Sample.schoolYear(Time.mktime(2000, 4, 2))
|
230
|
+
|
231
|
+
assert_equal 19, Sample.schoolYear(Time.mktime(2000, 5, 1))
|
232
|
+
|
233
|
+
assert_equal 19, Sample.schoolYear(Time.mktime(2000, 12, 31))
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
assert_equal 19, Sample.schoolYear(Time.mktime(2000, 1, 1), Date.new(2020, 1, 1))
|
238
|
+
|
239
|
+
assert_equal 19, Sample.schoolYear(Time.mktime(2000, 1, 1), Date.new(2020, 3, 31))
|
240
|
+
|
241
|
+
assert_equal 20, Sample.schoolYear(Time.mktime(2000, 1, 1), Date.new(2020, 4, 1))
|
242
|
+
|
243
|
+
assert_equal 20, Sample.schoolYear(Time.mktime(2000, 1, 1), Date.new(2020, 4, 2))
|
244
|
+
|
245
|
+
assert_equal 20, Sample.schoolYear(Time.mktime(2000, 1, 1), Date.new(2020, 12, 31))
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
assert_equal 19, Sample.schoolYear(Time.mktime(2000, 4, 1), Date.new(2020, 1, 1))
|
250
|
+
|
251
|
+
assert_equal 19, Sample.schoolYear(Time.mktime(2000, 4, 1), Date.new(2020, 3, 31))
|
252
|
+
|
253
|
+
assert_equal 20, Sample.schoolYear(Time.mktime(2000, 4, 1), Date.new(2020, 4, 1))
|
254
|
+
|
255
|
+
assert_equal 20, Sample.schoolYear(Time.mktime(2000, 4, 1), Date.new(2020, 4, 2))
|
256
|
+
|
257
|
+
assert_equal 20, Sample.schoolYear(Time.mktime(2000, 4, 1), Date.new(2020, 12, 31))
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
assert_equal 18, Sample.schoolYear(Time.mktime(2000, 4, 2), Date.new(2020, 1, 1))
|
262
|
+
|
263
|
+
assert_equal 18, Sample.schoolYear(Time.mktime(2000, 4, 2), Date.new(2020, 3, 31))
|
264
|
+
|
265
|
+
assert_equal 19, Sample.schoolYear(Time.mktime(2000, 4, 2), Date.new(2020, 4, 1))
|
266
|
+
|
267
|
+
assert_equal 19, Sample.schoolYear(Time.mktime(2000, 4, 2), Date.new(2020, 4, 2))
|
268
|
+
|
269
|
+
assert_equal 19, Sample.schoolYear(Time.mktime(2000, 4, 2), Date.new(2020, 12, 31))
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
end
|
274
|
+
|
275
|
+
```
|