Class: Vcard::V4_0::PropertyValue::DateTimeLocal

Inherits:
Vobject::PropertyValue show all
Includes:
Comparable
Defined in:
lib/vobject/vcard/v4_0/propertyvalue.rb

Instance Attribute Summary

Attributes inherited from Vobject::PropertyValue

#errors, #norm, #type, #value

Instance Method Summary collapse

Methods inherited from Vobject::PropertyValue

#name, #to_norm

Constructor Details

#initialize(val) ⇒ DateTimeLocal

Returns a new instance of DateTimeLocal.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/vobject/vcard/v4_0/propertyvalue.rb', line 197

def initialize(val)
  self.value = val.clone
  # val consists of :time && :zone values. If :zone is empty, floating local time (i.e. system local time) is assumed
  self.type = "date-time"
  # fill in unspecified month && year && date; only for purposes of comparison
  val[:year] = sprintf("%04d", ::Date.today.year) unless val.has_key?(:year)
  val[:month] = sprintf("%02d", ::Date.today.month) unless val.has_key?(:month)
  val[:day] = sprintf("%02d", ::Date.today.day) unless val.has_key?(:day)
  val[:hour] = 0 unless val.has_key?(:hour)
  val[:min] = 0 unless val.has_key?(:min)
  val[:sec] = 0 unless val.has_key?(:sec)
  value[:time] = if val[:zone].empty?
                   ::Time.utc(val[:year], val[:month], val[:day], val[:hour], val[:min], val[:sec])
                 else
                   ::Time.local(val[:year], val[:month], val[:day], val[:hour], val[:min], val[:sec])
                 end
  if val[:zone] && val[:zone] != "Z"
    offset = val[:zone][:hour] * 3600 + val[:zone][:min] * 60
    offset += val[:zone][:sec] if val[:zone][:sec]
    offset = -offset if val[:sign] == "-"
    value[:time] += offset.to_i
  end
end

Instance Method Details

#<=>(another) ⇒ Object



193
194
195
# File 'lib/vobject/vcard/v4_0/propertyvalue.rb', line 193

def <=>(another)
  value[:time] <=> another.value[:time]
end

#to_hashObject



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/vobject/vcard/v4_0/propertyvalue.rb', line 250

def to_hash
  ret = {}
  ret[:year] = value[:year] if value[:year]
  ret[:month] = value[:month] if value[:month]
  ret[:day] = value[:day] if value[:day]
  ret[:hour] = value[:hour] if value[:hour]
  ret[:min] = value[:min] if value[:min]
  ret[:sec] = value[:sec] if value[:sec]
  ret[:zone] = value[:zone] if value[:zone]
  ret
end

#to_sObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/vobject/vcard/v4_0/propertyvalue.rb', line 221

def to_s
  ret = ""
  ret << if value[:year]
           value[:year]
         else
           "--"
         end
  if value[:month]
    ret << value[:month]
  elsif value[:day]
    ret << "-"
  end
  if value[:day]
    ret << value[:day]
  end
  ret << "T"
  ret << value[:hour] if value[:hour]
  ret << value[:min] if value[:min]
  ret << value[:sec] if value[:sec]
  ret << value[:zone] if value[:zone] == "Z"
  if value[:zone].is_a?(Hash)
    ret << value[:zone][:sign]
    ret << value[:zone][:hour]
    ret << value[:zone][:min]
    ret << value[:zone][:sec] if value[:zone][:sec]
  end
  ret
end