Class: MailParser::RFC2822::DateTime

Inherits:
Object
  • Object
show all
Defined in:
lib/mailparser/rfc2822.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, month, day, hour, min, sec, zone_or_offset) ⇒ DateTime

Returns a new instance of DateTime.

Raises:

  • (ArgumentError)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/mailparser/rfc2822.rb', line 159

def initialize(year, month, day, hour, min, sec, zone_or_offset)
  y, m, d, h, mi, s = year.to_i, month.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i
  raise ArgumentError, "invalid year" if y < 0 or 9999 < y
  raise ArgumentError, "invalid month" if m < 1 or 12 < m
  raise ArgumentError, "invalid day of the month" if d < 1
  if [1,3,5,7,8,10,12].include? m
    raise ArgumentError, "invalid day of the month" if d > 31
  elsif [4,6,9,11].include? m
    raise ArgumentError, "invalid day of the month" if d > 30
  else # month == 2
    if y%4 == 0 and (y%100 !=0 or y%400 == 0)
      raise ArgumentError, "invalid day of the month" if d > 29
    else
      raise ArgumentError, "invalid day of the month" if d > 28
    end
  end
  raise ArgumentError, "invalid hour" if h > 23
  raise ArgumentError, "invalid minute" if mi > 59
  raise ArgumentError, "invalid second" if s > 60
  @year, @month, @day, @hour, @min, @sec = y, m, d, h, mi, s
  if zone_or_offset.is_a? Integer
    offset = zone_or_offset
    @zone_sec = offset
    @zone = format("%03d%02d", offset/3600, offset%3600).sub(/^0/, '+')
    return
  end
  zone = zone_or_offset
  if zone =~ /^[+-]\d\d(\d\d)$/ then
    raise ArgumentError, "invalid zone" if $1.to_i > 59
  else
    zone = (zone && ZONE[zone.upcase]) || "-0000"
  end
  z = zone[1,4].to_i
  @zone = zone
  @zone_sec = z/100*3600 + z%100*60
  @zone_sec = -@zone_sec if zone[0] == ?-
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



197
198
199
# File 'lib/mailparser/rfc2822.rb', line 197

def day
  @day
end

#hourObject (readonly)

Returns the value of attribute hour.



197
198
199
# File 'lib/mailparser/rfc2822.rb', line 197

def hour
  @hour
end

#minObject (readonly)

Returns the value of attribute min.



197
198
199
# File 'lib/mailparser/rfc2822.rb', line 197

def min
  @min
end

#monthObject (readonly)

Returns the value of attribute month.



197
198
199
# File 'lib/mailparser/rfc2822.rb', line 197

def month
  @month
end

#secObject (readonly)

Returns the value of attribute sec.



197
198
199
# File 'lib/mailparser/rfc2822.rb', line 197

def sec
  @sec
end

#yearObject (readonly)

Returns the value of attribute year.



197
198
199
# File 'lib/mailparser/rfc2822.rb', line 197

def year
  @year
end

#zoneObject (readonly)

Returns the value of attribute zone.



197
198
199
# File 'lib/mailparser/rfc2822.rb', line 197

def zone
  @zone
end

Class Method Details

.nowObject



154
155
156
157
# File 'lib/mailparser/rfc2822.rb', line 154

def self.now
  t = Time.now
  self.new(t.year, t.month, t.day, t.hour, t.min, t.sec, t.utc_offset)
end

Instance Method Details

#timeObject



199
200
201
202
# File 'lib/mailparser/rfc2822.rb', line 199

def time()
  t = Time.utc(@year, @month, @day, @hour, @min, @sec)
  Time.at(t.to_i - @zone_sec)
end