Class: ISO8601::Date

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/iso8601/date.rb

Overview

A Date representation.

Examples:

d = ISO8601::Date.new('2014-05-28')
d.year  # => 2014
d.month # => 5

Week dates

d = ISO8601::Date.new('2014-W15-2')
d.day   # => 27
d.wday  # => 2
d.week # => 15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Date


34
35
36
37
38
# File 'lib/iso8601/date.rb', line 34

def initialize(input)
  @original = input
  @atoms = atomize(input)
  @date = compose(@atoms)
end

Instance Attribute Details

#atomsObject (readonly)

The original atoms


26
27
28
# File 'lib/iso8601/date.rb', line 26

def atoms
  @atoms
end

#separatorObject (readonly)

The separator used in the original ISO 8601 string.


30
31
32
# File 'lib/iso8601/date.rb', line 30

def separator
  @separator
end

Instance Method Details

#+(other) ⇒ ISO8601::Date

Forwards the date the given amount of days.


54
55
56
57
# File 'lib/iso8601/date.rb', line 54

def +(other)
  other = other.to_days if other.respond_to?(:to_days)
  ISO8601::Date.new((@date + other).iso8601)
end

#-(other) ⇒ ISO8601::Date

Backwards the date the given amount of days.


65
66
67
68
# File 'lib/iso8601/date.rb', line 65

def -(other)
  other = other.to_days if other.respond_to?(:to_days)
  ISO8601::Date.new((@date - other).iso8601)
end

#==(other) ⇒ Boolean


80
81
82
# File 'lib/iso8601/date.rb', line 80

def ==(other)
  (hash == other.hash)
end

#eql?(other) ⇒ Boolean


88
89
90
# File 'lib/iso8601/date.rb', line 88

def eql?(other)
  (hash == other.hash)
end

#hashFixnum


94
95
96
# File 'lib/iso8601/date.rb', line 94

def hash
  [atoms, self.class].hash
end

#to_aObject

Converts self to an array of atoms.


72
73
74
# File 'lib/iso8601/date.rb', line 72

def to_a
  [year, month, day]
end

#weekInteger

The calendar week number (1-53)


44
45
46
# File 'lib/iso8601/date.rb', line 44

def week
  @date.cweek
end