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



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

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

Instance Attribute Details

#atomsObject (readonly)

The original atoms



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

def atoms
  @atoms
end

#separatorObject (readonly)

The separator used in the original ISO 8601 string.



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

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:



49
50
51
52
# File 'lib/iso8601/date.rb', line 49

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:



59
60
61
62
# File 'lib/iso8601/date.rb', line 59

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)


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

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

#eql?(other) ⇒ Boolean

Parameters:

  • other (#hash)

    The contrast to compare against

Returns:

  • (Boolean)


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

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

#hashFixnum

Returns:

  • (Fixnum)


84
85
86
# File 'lib/iso8601/date.rb', line 84

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

#to_aObject

Converts self to an array of atoms.



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

def to_a
  [year, month, day]
end

#weekInteger

The calendar week number (1-53)

Returns:

  • (Integer)


40
41
42
# File 'lib/iso8601/date.rb', line 40

def week
  @date.cweek
end