Class: Origami::Date

Inherits:
LiteralString show all
Defined in:
lib/origami/string.rb

Overview

Class representing a Date string.

Constant Summary collapse

REGEXP_TOKEN =

:nodoc:

/D:                         # Date header
 (?<year>\d{4})             # Year
 (?<month>\d{2})?           # Month
 (?<day>\d{2})?             # Day
 (?<hour>\d{2})?            # Hour
 (?<min>\d{2})?             # Minute
 (?<sec>\d{2})?             # Second
 (?:
     (?<ut>[\+\-Z])             # UT relationship
     (?<ut_hour_off>\d{2})      # UT hour offset
     ('(?<ut_min_off>\d{2}))?   # UT minute offset
 )?
/x

Constants inherited from LiteralString

LiteralString::TOKENS

Constants included from Object

Object::TOKENS

Instance Attribute Summary collapse

Attributes included from String

#encoding

Attributes included from Object

#file_offset, #generation, #no, #objstm_offset, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LiteralString

#expand, #to_hex, #to_s, #value

Methods included from String

#detect_encoding, #to_pdfdoc, #to_utf16be, #to_utf8

Methods included from Object

#<=>, #cast_to, #copy, #document, #export, included, #indirect?, #indirect_parent, #logicalize, #logicalize!, #native_type, #post_build, #pre_build, #reference, #set_document, #set_indirect, skip_until_next_obj, #solve, #to_o, #to_s, #type, typeof, #version_required, #xrefs

Constructor Details

#initialize(year:, month: 1, day: 1, hour: 0, min: 0, sec: 0, utc_offset: 0) ⇒ Date

Returns a new instance of Date.


373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/origami/string.rb', line 373

def initialize(year:, month: 1, day: 1, hour: 0, min: 0, sec: 0, utc_offset: 0)
    @year, @month, @day, @hour, @min, @sec = year, month, day, hour, min, sec
    @utc_offset = utc_offset

    date = "D:%04d%02d%02d%02d%02d%02d" % [year, month, day, hour, min, sec ]

    if utc_offset == 0
        date << "Z00'00"
    else
        date << (if utc_offset < 0 then '-' else '+' end)
        off_hours, off_secs = utc_offset.abs.divmod(3600)
        off_mins = off_secs / 60
        date << "%02d'%02d" % [ off_hours, off_mins ]
    end

    super(date)
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day


371
372
373
# File 'lib/origami/string.rb', line 371

def day
  @day
end

#hourObject (readonly)

Returns the value of attribute hour


371
372
373
# File 'lib/origami/string.rb', line 371

def hour
  @hour
end

#minObject (readonly)

Returns the value of attribute min


371
372
373
# File 'lib/origami/string.rb', line 371

def min
  @min
end

#monthObject (readonly)

Returns the value of attribute month


371
372
373
# File 'lib/origami/string.rb', line 371

def month
  @month
end

#secObject (readonly)

Returns the value of attribute sec


371
372
373
# File 'lib/origami/string.rb', line 371

def sec
  @sec
end

#utc_offsetObject (readonly)

Returns the value of attribute utc_offset


371
372
373
# File 'lib/origami/string.rb', line 371

def utc_offset
  @utc_offset
end

#yearObject (readonly)

Returns the value of attribute year


371
372
373
# File 'lib/origami/string.rb', line 371

def year
  @year
end

Class Method Details

.nowObject

Returns current Date String in UTC time.


422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/origami/string.rb', line 422

def self.now
    now = Time.now.utc

    date =
    {
        year: now.strftime("%Y").to_i,
        month: now.strftime("%m").to_i,
        day: now.strftime("%d").to_i,
        hour: now.strftime("%H").to_i,
        min: now.strftime("%M").to_i,
        sec: now.strftime("%S").to_i,
        utc_offset: now.utc_offset
    }

    Origami::Date.new(date)
end

.parse(str) ⇒ Object

:nodoc:

Raises:


395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/origami/string.rb', line 395

def self.parse(str) #:nodoc:
    raise InvalidDateError, "Not a valid Date string" unless str =~ REGEXP_TOKEN

    date =
    {
        year: $~['year'].to_i
    }

    date[:month] = $~['month'].to_i if $~['month']
    date[:day] = $~['day'].to_i if $~['day']
    date[:hour] = $~['hour'].to_i if $~['hour']
    date[:min] = $~['min'].to_i if $~['min']
    date[:sec] = $~['sec'].to_i if $~['sec']

    if %w[+ -].include?($~['ut'])
        utc_offset = $~['ut_hour_off'].to_i * 3600 + $~['ut_min_off'].to_i * 60
        utc_offset = -utc_offset if $~['ut'] == '-'

        date[:utc_offset] = utc_offset
    end

    Origami::Date.new(date)
end

Instance Method Details

#to_datetimeObject


391
392
393
# File 'lib/origami/string.rb', line 391

def to_datetime
    ::DateTime.new(@year, @month, @day, @hour, @min, @sec, (@utc_offset / 3600).to_s)
end