month 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/lib/month.rb +93 -0
- data/lib/month.rb~ +91 -0
- metadata +40 -0
data/lib/month.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# Month represents a specific month in time. With the exception of
|
2
|
+
# Month.month_names (which returns a zero-based array), every usage of the
|
3
|
+
# month value assumes that 1 equals January and 12 equals December.
|
4
|
+
class Month
|
5
|
+
Version = '0.1.0'
|
6
|
+
|
7
|
+
# Returns an array of the full names of months (in English). Note that
|
8
|
+
# "January" is the 0th element, and "December" is the 11th element.
|
9
|
+
def Month.month_names
|
10
|
+
[ "January", "February", "March", "April", "May", "June", "July",
|
11
|
+
"August", "September", "October", "November", "December" ]
|
12
|
+
end
|
13
|
+
|
14
|
+
include Comparable
|
15
|
+
|
16
|
+
attr_reader :month, :year
|
17
|
+
|
18
|
+
# A new month can be set to a specific +month+ and +year+, or you can call
|
19
|
+
# Month.new with no arguments to receive the current month.
|
20
|
+
def initialize( year = nil, month = nil )
|
21
|
+
require 'date'
|
22
|
+
if month.nil? || year.nil?
|
23
|
+
date = Date.today
|
24
|
+
month = date.mon unless month
|
25
|
+
year = date.year unless year
|
26
|
+
end
|
27
|
+
fail "invalid month" if month < 1 || month > 12
|
28
|
+
@month = month
|
29
|
+
@year = year
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns a new Month that is +amountToAdd+ months later.
|
33
|
+
def +( amountToAdd )
|
34
|
+
( fullYears, remainingMonths ) = amountToAdd.divmod( 12 )
|
35
|
+
resultYear = @year + fullYears
|
36
|
+
resultMonth = @month + remainingMonths
|
37
|
+
if resultMonth > 12
|
38
|
+
resultMonth -= 12
|
39
|
+
resultYear += 1
|
40
|
+
end
|
41
|
+
Month.new( resultYear, resultMonth )
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns a new Month that is +amountToSubtract+ months earlier.
|
45
|
+
def -(amountToSubtract)
|
46
|
+
self + (-amountToSubtract)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Compare this Month to another Month.
|
50
|
+
def <=>(anOther)
|
51
|
+
if @year == anOther.year
|
52
|
+
@month <=> anOther.month
|
53
|
+
else
|
54
|
+
@year <=> anOther.year
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Returns the last Date of the month.
|
59
|
+
def end_date
|
60
|
+
self.next.start_date - 1
|
61
|
+
end
|
62
|
+
|
63
|
+
# Is this Month equal to +anOther+? +anOther+ must be another Month of the
|
64
|
+
# same value.
|
65
|
+
def eql?(anOther)
|
66
|
+
self == anOther
|
67
|
+
end
|
68
|
+
|
69
|
+
# Calculate a hash value for this Month.
|
70
|
+
def hash
|
71
|
+
"#{@year}#{@month}".to_i
|
72
|
+
end
|
73
|
+
|
74
|
+
# Returns the next Month.
|
75
|
+
def next
|
76
|
+
self + 1
|
77
|
+
end
|
78
|
+
|
79
|
+
# Returns the previous Month.
|
80
|
+
def prev
|
81
|
+
self - 1
|
82
|
+
end
|
83
|
+
|
84
|
+
# Returns the first Date of the month.
|
85
|
+
def start_date
|
86
|
+
Date.new( @year, @month, 1 )
|
87
|
+
end
|
88
|
+
|
89
|
+
# Returns a string of the format "January 2001".
|
90
|
+
def to_s
|
91
|
+
Month.month_names[@month-1][0..2] + " " + @year.to_s
|
92
|
+
end
|
93
|
+
end
|
data/lib/month.rb~
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# Represents a specific month in time. With the exception of
|
2
|
+
# Month.month_names (which returns a zero-based array), every usage of the
|
3
|
+
# month value assumes that 1 equals January and 12 equals December.
|
4
|
+
class Month
|
5
|
+
# Returns an array of the full names of months (in English). Note that
|
6
|
+
# "January" is the 0th element, and "December" is the 11th element.
|
7
|
+
def Month.month_names
|
8
|
+
[ "January", "February", "March", "April", "May", "June", "July",
|
9
|
+
"August", "September", "October", "November", "December" ]
|
10
|
+
end
|
11
|
+
|
12
|
+
include Comparable
|
13
|
+
|
14
|
+
attr_reader :month, :year
|
15
|
+
|
16
|
+
# A new month can be set to a specific +month+ and +year+, or you can call
|
17
|
+
# Month.new with no arguments to receive the current month.
|
18
|
+
def initialize( year = nil, month = nil )
|
19
|
+
require 'date'
|
20
|
+
if month.nil? || year.nil?
|
21
|
+
date = Date.today
|
22
|
+
month = date.mon unless month
|
23
|
+
year = date.year unless year
|
24
|
+
end
|
25
|
+
fail "invalid month" if month < 1 || month > 12
|
26
|
+
@month = month
|
27
|
+
@year = year
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns a new Month that is +amountToAdd+ months later.
|
31
|
+
def +( amountToAdd )
|
32
|
+
( fullYears, remainingMonths ) = amountToAdd.divmod( 12 )
|
33
|
+
resultYear = @year + fullYears
|
34
|
+
resultMonth = @month + remainingMonths
|
35
|
+
if resultMonth > 12
|
36
|
+
resultMonth -= 12
|
37
|
+
resultYear += 1
|
38
|
+
end
|
39
|
+
Month.new( resultYear, resultMonth )
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns a new Month that is +amountToSubtract+ months earlier.
|
43
|
+
def -(amountToSubtract)
|
44
|
+
self + (-amountToSubtract)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Compare this Month to another Month.
|
48
|
+
def <=>(anOther)
|
49
|
+
if @year == anOther.year
|
50
|
+
@month <=> anOther.month
|
51
|
+
else
|
52
|
+
@year <=> anOther.year
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the last Date of the month.
|
57
|
+
def end_date
|
58
|
+
self.next.start_date - 1
|
59
|
+
end
|
60
|
+
|
61
|
+
# Is this Month equal to +anOther+? +anOther+ must be another Month of the
|
62
|
+
# same value.
|
63
|
+
def eql?(anOther)
|
64
|
+
self == anOther
|
65
|
+
end
|
66
|
+
|
67
|
+
# Calculate a hash value for this Month.
|
68
|
+
def hash
|
69
|
+
"#{@year}#{@month}".to_i
|
70
|
+
end
|
71
|
+
|
72
|
+
# Returns the next Month.
|
73
|
+
def next
|
74
|
+
self + 1
|
75
|
+
end
|
76
|
+
|
77
|
+
# Returns the previous Month.
|
78
|
+
def prev
|
79
|
+
self - 1
|
80
|
+
end
|
81
|
+
|
82
|
+
# Returns the first Date of the month.
|
83
|
+
def start_date
|
84
|
+
Date.new( @year, @month, 1 )
|
85
|
+
end
|
86
|
+
|
87
|
+
# Returns a string of the format "January 2001".
|
88
|
+
def to_s
|
89
|
+
Month.month_names[@month-1][0..2] + " " + @year.to_s
|
90
|
+
end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.6
|
3
|
+
specification_version: 1
|
4
|
+
name: month
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2005-06-07
|
8
|
+
summary: Month is a utility class for representing months in Ruby.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: sera@fhwang.net
|
12
|
+
homepage: http://month.rubyforge.org/
|
13
|
+
rubyforge_project:
|
14
|
+
description: "Ruby Month is a utility class for representing months in Ruby. It handles
|
15
|
+
addition, previous and next months, end and start dates, month names (in
|
16
|
+
English), and a few other handy things."
|
17
|
+
autorequire: month
|
18
|
+
default_executable:
|
19
|
+
bindir: bin
|
20
|
+
has_rdoc: false
|
21
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
22
|
+
requirements:
|
23
|
+
-
|
24
|
+
- ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.0
|
27
|
+
version:
|
28
|
+
platform: ruby
|
29
|
+
authors:
|
30
|
+
- Francis Hwang
|
31
|
+
files:
|
32
|
+
- lib/month.rb
|
33
|
+
- lib/month.rb~
|
34
|
+
test_files: []
|
35
|
+
rdoc_options: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
executables: []
|
38
|
+
extensions: []
|
39
|
+
requirements: []
|
40
|
+
dependencies: []
|