Class: Time

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

Overview

overides xmlschema to support MTP xmlschema

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.xmlschema(date) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/mtp.rb', line 226

def self.xmlschema(date)
  if /\A\s*
        (-?\d+)(\d\d)(\d\d)
        T
        (\d\d)(\d\d)(\d\d)?
        (\.\d*)?
        (Z|[+-]\d\d\d\d)?
        \s*\z/ix =~ date
    year = $1.to_i
    mon = $2.to_i
    day = $3.to_i
    hour = $4.to_i
    min = $5.to_i
    sec = $6.to_i
    usec = 0
    usec = $7.to_f * 1000000 if $7
    if $8
      zone = $8
      year, mon, day, hour, min, sec =
        apply_offset(year, mon, day, hour, min, sec, zone_offset(zone))
      Time.utc(year, mon, day, hour, min, sec, usec)
    else
        Time.local(year, mon, day, hour, min, sec, usec)
    end
  else
    raise ArgumentError.new("invalid date: #{date.inspect}")
  end
end

Instance Method Details

#xmlschema(fraction_digits = 0) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/mtp.rb', line 255

def xmlschema(fraction_digits=0)
  sprintf('%d%02d%02dT%02d%02d', #%02d',
    year, mon, day, hour, min)+ #, sec) +
  if fraction_digits == 0
    ''
  elsif fraction_digits <= 6
    '.' + sprintf('%06d', usec)[0, fraction_digits]
  else
    '.' + sprintf('%06d', usec) + '0' * (fraction_digits - 6)
  end +
  if utc?
    'Z'
  else
    off = utc_offset
    sign = off < 0 ? '-' : '+'
    sprintf('%s%02d%02d', sign, *(off.abs / 60).divmod(60))
  end
end