expanded_date 0.1.0
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/expanded_date.rb +111 -0
- metadata +48 -0
data/expanded_date.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'fileutils' #Workaround for rubyscript2exe ref Erik Veenstra 11.07.2006
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class Date
|
6
|
+
def prev_month
|
7
|
+
m = month - 1
|
8
|
+
y = year
|
9
|
+
if m == 0
|
10
|
+
m = 12
|
11
|
+
y = y - 1
|
12
|
+
end
|
13
|
+
d = day
|
14
|
+
self::class.civil(y,m,d)
|
15
|
+
end
|
16
|
+
|
17
|
+
def next_month
|
18
|
+
m = month + 1
|
19
|
+
y = year
|
20
|
+
if m == 13
|
21
|
+
m = 1
|
22
|
+
y = y + 1
|
23
|
+
end
|
24
|
+
d = day
|
25
|
+
self::class.civil(y,m,d)
|
26
|
+
end
|
27
|
+
|
28
|
+
def end_of_month
|
29
|
+
d = next_month
|
30
|
+
self::class.civil(d.year, d.month, 1)-1
|
31
|
+
end
|
32
|
+
|
33
|
+
def beginning_of_month
|
34
|
+
self::class.civil(self.year, self.month, 1)
|
35
|
+
end
|
36
|
+
|
37
|
+
def end_of_next_month
|
38
|
+
self.beginning_of_month.next_month.end_of_month
|
39
|
+
end
|
40
|
+
|
41
|
+
def end_of_prev_month
|
42
|
+
self.beginning_of_month-1
|
43
|
+
end
|
44
|
+
|
45
|
+
def beginning_of_next_month
|
46
|
+
self.beginning_of_month.next_month
|
47
|
+
end
|
48
|
+
|
49
|
+
def beginning_of_prev_month
|
50
|
+
self.beginning_of_month.prev_month
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class Datotest < Test::Unit::TestCase
|
55
|
+
def test_next
|
56
|
+
d = Date.parse("2000-01-01")
|
57
|
+
assert_equal(d.to_s, "2000-01-01")
|
58
|
+
assert_equal(d.next_month.to_s, "2000-02-01")
|
59
|
+
assert_equal(d.next_month.next_month.to_s, "2000-03-01")
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_prev
|
63
|
+
d = Date.parse("2000-01-01")
|
64
|
+
assert_equal(d.to_s, "2000-01-01")
|
65
|
+
assert_equal(d.prev_month.to_s, "1999-12-01")
|
66
|
+
assert_equal(d.prev_month.prev_month.to_s, "1999-11-01")
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_end_of_month
|
70
|
+
assert_equal(Date.parse("2000-02-13").end_of_month.to_s, "2000-02-29")
|
71
|
+
assert_equal(Date.parse("2001-02-13").end_of_month.to_s, "2001-02-28")
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_beginning_of_month
|
75
|
+
assert_equal(Date.parse("2000-02-13").beginning_of_month.to_s, "2000-02-01")
|
76
|
+
assert_equal(Date.parse("2001-02-13").beginning_of_month.to_s, "2001-02-01")
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_beginning_of_next_month
|
80
|
+
assert_equal(Date.parse("1999-12-13").beginning_of_next_month.to_s, "2000-01-01")
|
81
|
+
assert_equal(Date.parse("2000-01-13").beginning_of_next_month.to_s, "2000-02-01")
|
82
|
+
assert_equal(Date.parse("2000-02-13").beginning_of_next_month.to_s, "2000-03-01")
|
83
|
+
assert_equal(Date.parse("2001-02-13").beginning_of_next_month.to_s, "2001-03-01")
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_beginning_of_prev_month
|
87
|
+
assert_equal(Date.parse("1999-12-13").beginning_of_prev_month.to_s, "1999-11-01")
|
88
|
+
assert_equal(Date.parse("2000-01-13").beginning_of_prev_month.to_s, "1999-12-01")
|
89
|
+
assert_equal(Date.parse("2000-02-13").beginning_of_prev_month.to_s, "2000-01-01")
|
90
|
+
assert_equal(Date.parse("2001-03-13").beginning_of_prev_month.to_s, "2001-02-01")
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_end_of_next_month
|
94
|
+
assert_equal(Date.parse("1999-12-13").end_of_next_month.to_s, "2000-01-31")
|
95
|
+
assert_equal(Date.parse("2000-01-13").end_of_next_month.to_s, "2000-02-29")
|
96
|
+
assert_equal(Date.parse("2000-02-13").end_of_next_month.to_s, "2000-03-31")
|
97
|
+
assert_equal(Date.parse("2001-02-13").end_of_next_month.to_s, "2001-03-31")
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_end_of_prev_month
|
101
|
+
assert_equal(Date.parse("1999-12-13").end_of_prev_month.to_s, "1999-11-30")
|
102
|
+
assert_equal(Date.parse("2000-01-13").end_of_prev_month.to_s, "1999-12-31")
|
103
|
+
assert_equal(Date.parse("2000-02-13").end_of_prev_month.to_s, "2000-01-31")
|
104
|
+
assert_equal(Date.parse("2001-03-13").end_of_prev_month.to_s, "2001-02-28")
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_long_conversion
|
108
|
+
assert_equal(Date.today.next_month.next_month.end_of_prev_month, Date.today.end_of_next_month)
|
109
|
+
assert_equal(Date.today, Date.today.next_month.next_month.next_month.prev_month.prev_month.prev_month)
|
110
|
+
end
|
111
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: expanded_date
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2006-07-11 00:00:00 +02:00
|
8
|
+
summary: Adds extra functionality to date
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
- .
|
12
|
+
- C:\ruby\lib
|
13
|
+
email:
|
14
|
+
homepage:
|
15
|
+
rubyforge_project:
|
16
|
+
description:
|
17
|
+
autorequire:
|
18
|
+
default_executable:
|
19
|
+
bindir: bin
|
20
|
+
has_rdoc: false
|
21
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 0.0.0
|
26
|
+
version:
|
27
|
+
platform: ruby
|
28
|
+
signing_key:
|
29
|
+
cert_chain:
|
30
|
+
post_install_message:
|
31
|
+
authors: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- expanded_date.rb
|
35
|
+
test_files: []
|
36
|
+
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
requirements: []
|
46
|
+
|
47
|
+
dependencies: []
|
48
|
+
|