Class: ISO8601::DateTime
- Inherits:
-
Object
- Object
- ISO8601::DateTime
- Defined in:
- lib/iso8601/dateTime.rb
Overview
Review the pattern ‘201005`. It has to be `20-10-05` instead of `2010-05`. The specification doesn’t allow a YYYYMM. It should be always YYYY-MM.
A DateTime representation
Instance Attribute Summary collapse
-
#century ⇒ Object
readonly
Returns the value of attribute century.
-
#day ⇒ Object
readonly
Returns the value of attribute day.
-
#hour ⇒ Object
readonly
Returns the value of attribute hour.
-
#minute ⇒ Object
readonly
Returns the value of attribute minute.
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#second ⇒ Object
readonly
Returns the value of attribute second.
-
#timezone ⇒ Object
readonly
Returns the value of attribute timezone.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Instance Method Summary collapse
-
#+(d) ⇒ Object
Addition.
-
#-(d) ⇒ Object
Substraction.
-
#initialize(date_time) ⇒ DateTime
constructor
A new instance of DateTime.
-
#to_s ⇒ Object
Returns the datetime string representation.
-
#to_time ⇒ Time
Converts the object to a Time instance.
Constructor Details
#initialize(date_time) ⇒ DateTime
Returns a new instance of DateTime.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/iso8601/dateTime.rb', line 14 def initialize(date_time) @dt = /^(?: (\d{2})(\d{2})? # Year. It can be either two digits (the century) or four digits (the full year) (?: (-)?(\d{2}) )? # Month with an optional separator (?: (\3)?(\d{2}) # Day with an optional separator which is the same for the Month )? )? # Date (?: T(\d{2}) # Hour (?: (:)?(\d{2}) # Minute with an optional separator )? (?: (\8)?(\d{2}) # Second with an optional separator which is the same that for the Minute )? # Time ( Z|([+-]) (\d{2}) # Timezone hour (?: (\8)? # Separator which should be the same that for the Minute (\d{2}) # Timezone minute )? )? # Timezone )? $/x.match(date_time) or raise ISO8601::Errors::UnknownPattern.new(date_time) @date_time = date_time @time = @dt[7] @date_separator = @dt[3] == @dt[5] ? @dt[3] : nil @time_separator = if (!@dt[8].nil? and (!@dt[10].nil? and !@dt[11].nil?) and (!@dt[15].nil? and !@dt[16].nil?)) or (!@dt[8].nil? and (!@dt[10].nil? and !@dt[11].nil?) and @dt[16].nil?) or (!@dt[8].nil? and @dt[11].nil? and @dt[16].nil?) @dt[8] else nil end @century = @dt[1].to_i @year = @dt[2].nil? ? nil : (@dt[1] + @dt[2]).to_i @month = @dt[4].nil? ? nil : @dt[4].to_i @day = @dt[6].nil? ? nil : @dt[6].to_i @hour = @dt[7].nil? ? nil : @dt[7].to_i @minute = @dt[9].nil? ? nil : @dt[9].to_i @second = @dt[11].nil? ? nil : @dt[11].to_i @timezone = { :full => @dt[12].nil? ? (Time.now.gmt_offset / 3600) : (@dt[12] == "Z" ? 0 : @dt[12]), :sign => @dt[13], :hour => @dt[12].nil? ? (Time.now.gmt_offset / 3600) : (@dt[12] == "Z" ? 0 : @dt[14].to_i), :minute => (@dt[12].nil? or @dt[12] == "Z") ? 0 : @dt[13].to_i } valid_pattern? valid_range? end |
Instance Attribute Details
#century ⇒ Object (readonly)
Returns the value of attribute century.
11 12 13 |
# File 'lib/iso8601/dateTime.rb', line 11 def century @century end |
#day ⇒ Object (readonly)
Returns the value of attribute day.
11 12 13 |
# File 'lib/iso8601/dateTime.rb', line 11 def day @day end |
#hour ⇒ Object (readonly)
Returns the value of attribute hour.
11 12 13 |
# File 'lib/iso8601/dateTime.rb', line 11 def hour @hour end |
#minute ⇒ Object (readonly)
Returns the value of attribute minute.
11 12 13 |
# File 'lib/iso8601/dateTime.rb', line 11 def minute @minute end |
#month ⇒ Object (readonly)
Returns the value of attribute month.
11 12 13 |
# File 'lib/iso8601/dateTime.rb', line 11 def month @month end |
#second ⇒ Object (readonly)
Returns the value of attribute second.
11 12 13 |
# File 'lib/iso8601/dateTime.rb', line 11 def second @second end |
#timezone ⇒ Object (readonly)
Returns the value of attribute timezone.
11 12 13 |
# File 'lib/iso8601/dateTime.rb', line 11 def timezone @timezone end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
11 12 13 |
# File 'lib/iso8601/dateTime.rb', line 11 def year @year end |
Instance Method Details
#+(d) ⇒ Object
Addition
95 96 97 98 |
# File 'lib/iso8601/dateTime.rb', line 95 def +(d) raise TypeError unless d.kind_of? Numeric ISO8601::DateTime.new((Time.utc(@year, @month, @day, @hour, @minute, @second) + d).iso8601) end |
#-(d) ⇒ Object
Substraction
103 104 105 106 |
# File 'lib/iso8601/dateTime.rb', line 103 def -(d) raise TypeError unless d.kind_of? Numeric ISO8601::DateTime.new((Time.utc(@year, @month, @day, @hour, @minute, @second) - d).iso8601) end |
#to_s ⇒ Object
Returns the datetime string representation
73 74 75 |
# File 'lib/iso8601/dateTime.rb', line 73 def to_s @date_time end |
#to_time ⇒ Time
Converts the object to a Time instance
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/iso8601/dateTime.rb', line 80 def to_time raise RangeError if @year.nil? if @month.nil? Time.utc(@year) elsif @day.nil? date = [@year, @month, '01'].join('-') Time.parse(date).getutc else Time.parse(@date_time).getutc end end |