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

Returns a new instance of Date.

Parameters:

  • input (String)

    The date pattern



36
37
38
39
40
# File 'lib/iso8601/date.rb', line 36

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

Instance Attribute Details

#atomsObject (readonly)

The original atoms



28
29
30
# File 'lib/iso8601/date.rb', line 28

def atoms
  @atoms
end

#separatorObject (readonly)

The separator used in the original ISO 8601 string.



32
33
34
# File 'lib/iso8601/date.rb', line 32

def separator
  @separator
end

Instance Method Details

#+(other) ⇒ ISO8601::Date

Forwards the date the given amount of days.

Parameters:

  • other (Numeric)

    The days to add

Returns:



56
57
58
59
# File 'lib/iso8601/date.rb', line 56

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.

Parameters:

  • other (Numeric)

    The days to remove

Returns:



67
68
69
70
# File 'lib/iso8601/date.rb', line 67

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

#==(other) ⇒ Boolean

Parameters:

  • other (#hash)

    The contrast to compare against

Returns:

  • (Boolean)


82
83
84
# File 'lib/iso8601/date.rb', line 82

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

#eql?(other) ⇒ Boolean

Parameters:

  • other (#hash)

    The contrast to compare against

Returns:

  • (Boolean)


90
91
92
# File 'lib/iso8601/date.rb', line 90

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

#hashFixnum

Returns:

  • (Fixnum)


96
97
98
# File 'lib/iso8601/date.rb', line 96

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

#to_aObject

Converts self to an array of atoms.



74
75
76
# File 'lib/iso8601/date.rb', line 74

def to_a
  [year, month, day]
end

#weekInteger

The calendar week number (1-53)

Returns:

  • (Integer)


46
47
48
# File 'lib/iso8601/date.rb', line 46

def week
  @date.cweek
end