expanded_date 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/expanded_date.rb +33 -2
- data/test/datetest.rb +51 -30
- metadata +3 -3
data/lib/expanded_date.rb
CHANGED
@@ -1,6 +1,36 @@
|
|
1
|
+
#=Expanded date
|
2
|
+
#Jon Egil Strand
|
3
|
+
#
|
4
|
+
#Adds extra functionality to the regular Date class. Methods such as tomorrow,
|
5
|
+
#yesterday, next_week, prev_week are syntactic sugar for date +/- 1 (or 7).
|
6
|
+
#Their main use is for readability of code if one so wishes.
|
7
|
+
#
|
8
|
+
#The month functions are really handy when working with specific periods. In my
|
9
|
+
#experience these are quite often tied to beginning- and end of months rather than
|
10
|
+
#specific dates. I.e. "end of March, end of April, ... " instead of "31st March,
|
11
|
+
#30th April, ..." in a lot of cases.
|
12
|
+
#
|
13
|
+
|
1
14
|
require 'date'
|
2
15
|
|
3
16
|
class Date
|
17
|
+
def tomorrow
|
18
|
+
self + 1
|
19
|
+
end
|
20
|
+
|
21
|
+
def yesterday
|
22
|
+
self - 1
|
23
|
+
end
|
24
|
+
|
25
|
+
def next_week
|
26
|
+
self + 7
|
27
|
+
end
|
28
|
+
|
29
|
+
def prev_week
|
30
|
+
self - 7
|
31
|
+
end
|
32
|
+
|
33
|
+
#This date in previous month
|
4
34
|
def prev_month
|
5
35
|
m = month - 1
|
6
36
|
y = year
|
@@ -10,8 +40,9 @@ class Date
|
|
10
40
|
end
|
11
41
|
d = day
|
12
42
|
self::class.civil(y,m,d)
|
13
|
-
end
|
14
|
-
|
43
|
+
end
|
44
|
+
|
45
|
+
#This date in next month. Raises ArgumentError: invalid date if the date does not exist.
|
15
46
|
def next_month
|
16
47
|
m = month + 1
|
17
48
|
y = year
|
data/test/datetest.rb
CHANGED
@@ -2,60 +2,81 @@ require 'test/unit'
|
|
2
2
|
require 'lib/expanded_date'
|
3
3
|
|
4
4
|
class Datetest < Test::Unit::TestCase
|
5
|
-
|
5
|
+
|
6
|
+
def test_tomorrow
|
7
|
+
d = Date.parse("2004-02-28")
|
8
|
+
assert_equal("2004-02-29", d.tomorrow.to_s)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_yesterday
|
12
|
+
d = Date.parse("2004-03-01")
|
13
|
+
assert_equal("2004-02-29", d.yesterday.to_s)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_next_week
|
17
|
+
d = Date.parse("2004-02-28")
|
18
|
+
assert_equal("2004-03-06", d.next_week.to_s)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_prev_week
|
22
|
+
d = Date.parse("2004-02-28")
|
23
|
+
assert_equal("2004-02-21", d.prev_week.to_s)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_next_month
|
6
27
|
d = Date.parse("2000-01-01")
|
7
|
-
assert_equal(
|
8
|
-
assert_equal(
|
9
|
-
assert_equal(d.next_month.next_month.to_s
|
28
|
+
assert_equal("2000-01-01", d.to_s)
|
29
|
+
assert_equal("2000-02-01", d.next_month.to_s)
|
30
|
+
assert_equal("2000-03-01", d.next_month.next_month.to_s)
|
10
31
|
end
|
11
32
|
|
12
|
-
def
|
33
|
+
def test_prev_month
|
13
34
|
d = Date.parse("2000-01-01")
|
14
|
-
assert_equal(
|
15
|
-
assert_equal(
|
16
|
-
assert_equal(d.prev_month.prev_month.to_s
|
35
|
+
assert_equal("2000-01-01", d.to_s)
|
36
|
+
assert_equal("1999-12-01", d.prev_month.to_s)
|
37
|
+
assert_equal("1999-11-01", d.prev_month.prev_month.to_s)
|
17
38
|
end
|
18
39
|
|
19
40
|
def test_end_of_month
|
20
|
-
assert_equal(Date.parse("2000-02-13").end_of_month.to_s
|
21
|
-
assert_equal(Date.parse("2001-02-13").end_of_month.to_s
|
41
|
+
assert_equal("2000-02-29", Date.parse("2000-02-13").end_of_month.to_s)
|
42
|
+
assert_equal("2001-02-28", Date.parse("2001-02-13").end_of_month.to_s)
|
22
43
|
end
|
23
44
|
|
24
45
|
def test_beginning_of_month
|
25
|
-
assert_equal(Date.parse("2000-02-13").beginning_of_month.to_s
|
26
|
-
assert_equal(Date.parse("2001-02-13").beginning_of_month.to_s
|
46
|
+
assert_equal("2000-02-01", Date.parse("2000-02-13").beginning_of_month.to_s)
|
47
|
+
assert_equal("2001-02-01", Date.parse("2001-02-13").beginning_of_month.to_s)
|
27
48
|
end
|
28
49
|
|
29
50
|
def test_beginning_of_next_month
|
30
|
-
assert_equal(Date.parse("1999-12-13").beginning_of_next_month.to_s
|
31
|
-
assert_equal(Date.parse("2000-01-13").beginning_of_next_month.to_s
|
32
|
-
assert_equal(Date.parse("2000-02-13").beginning_of_next_month.to_s
|
33
|
-
assert_equal(Date.parse("2001-02-13").beginning_of_next_month.to_s
|
51
|
+
assert_equal("2000-01-01", Date.parse("1999-12-13").beginning_of_next_month.to_s)
|
52
|
+
assert_equal("2000-02-01", Date.parse("2000-01-13").beginning_of_next_month.to_s)
|
53
|
+
assert_equal("2000-03-01", Date.parse("2000-02-13").beginning_of_next_month.to_s)
|
54
|
+
assert_equal("2001-03-01", Date.parse("2001-02-13").beginning_of_next_month.to_s)
|
34
55
|
end
|
35
56
|
|
36
57
|
def test_beginning_of_prev_month
|
37
|
-
assert_equal(Date.parse("1999-12-13").beginning_of_prev_month.to_s
|
38
|
-
assert_equal(Date.parse("2000-01-13").beginning_of_prev_month.to_s
|
39
|
-
assert_equal(Date.parse("2000-02-13").beginning_of_prev_month.to_s
|
40
|
-
assert_equal(Date.parse("2001-03-13").beginning_of_prev_month.to_s
|
58
|
+
assert_equal("1999-11-01", Date.parse("1999-12-13").beginning_of_prev_month.to_s)
|
59
|
+
assert_equal("1999-12-01", Date.parse("2000-01-13").beginning_of_prev_month.to_s)
|
60
|
+
assert_equal("2000-01-01", Date.parse("2000-02-13").beginning_of_prev_month.to_s)
|
61
|
+
assert_equal("2001-02-01", Date.parse("2001-03-13").beginning_of_prev_month.to_s)
|
41
62
|
end
|
42
63
|
|
43
64
|
def test_end_of_next_month
|
44
|
-
assert_equal(Date.parse("1999-12-13").end_of_next_month.to_s
|
45
|
-
assert_equal(Date.parse("2000-01-13").end_of_next_month.to_s
|
46
|
-
assert_equal(Date.parse("2000-02-13").end_of_next_month.to_s
|
47
|
-
assert_equal(Date.parse("2001-02-13").end_of_next_month.to_s
|
65
|
+
assert_equal("2000-01-31", Date.parse("1999-12-13").end_of_next_month.to_s)
|
66
|
+
assert_equal("2000-02-29", Date.parse("2000-01-13").end_of_next_month.to_s)
|
67
|
+
assert_equal("2000-03-31", Date.parse("2000-02-13").end_of_next_month.to_s)
|
68
|
+
assert_equal("2001-03-31", Date.parse("2001-02-13").end_of_next_month.to_s)
|
48
69
|
end
|
49
70
|
|
50
71
|
def test_end_of_prev_month
|
51
|
-
assert_equal(Date.parse("1999-12-13").end_of_prev_month.to_s
|
52
|
-
assert_equal(Date.parse("2000-01-13").end_of_prev_month.to_s
|
53
|
-
assert_equal(Date.parse("2000-02-13").end_of_prev_month.to_s
|
54
|
-
assert_equal(Date.parse("2001-03-13").end_of_prev_month.to_s
|
72
|
+
assert_equal("1999-11-30", Date.parse("1999-12-13").end_of_prev_month.to_s)
|
73
|
+
assert_equal("1999-12-31", Date.parse("2000-01-13").end_of_prev_month.to_s)
|
74
|
+
assert_equal("2000-01-31", Date.parse("2000-02-13").end_of_prev_month.to_s)
|
75
|
+
assert_equal("2001-02-28", Date.parse("2001-03-13").end_of_prev_month.to_s)
|
55
76
|
end
|
56
77
|
|
57
78
|
def test_long_conversion
|
58
|
-
assert_equal(Date.today.
|
59
|
-
assert_equal(Date.today
|
79
|
+
assert_equal(Date.today.end_of_next_month, Date.today.next_month.next_month.end_of_prev_month)
|
80
|
+
assert_equal(Date.today.next_month.next_month.next_month.prev_month.prev_month.prev_month, Date.today)
|
60
81
|
end
|
61
82
|
end
|
metadata
CHANGED
@@ -3,9 +3,9 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: expanded_date
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2006-07-
|
8
|
-
summary: Adds extra functionality to date
|
6
|
+
version: 0.2.0
|
7
|
+
date: 2006-07-20 00:00:00 +02:00
|
8
|
+
summary: Adds extra functionality to date, such as .yesterday, .next_week, .end_of_next_month, .beginning_of_prev_month ... and the like
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
email: jes@luretanker.no
|