Class: Origami::Date
- Inherits:
-
LiteralString
- Object
- String
- LiteralString
- Origami::Date
- 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
Constants included from Object
Instance Attribute Summary collapse
-
#day ⇒ Object
readonly
Returns the value of attribute day.
-
#hour ⇒ Object
readonly
Returns the value of attribute hour.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#sec ⇒ Object
readonly
Returns the value of attribute sec.
-
#utc_offset ⇒ Object
readonly
Returns the value of attribute utc_offset.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Attributes included from String
Attributes included from Object
#file_offset, #generation, #no, #objstm_offset, #parent
Class Method Summary collapse
-
.now ⇒ Object
Returns current Date String in UTC time.
-
.parse(str) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#initialize(year:, month: 1, day: 1, hour: 0, min: 0, sec: 0, utc_offset: 0) ⇒ Date
constructor
A new instance of Date.
- #to_datetime ⇒ Object
Methods inherited from LiteralString
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, #numbered?, #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.
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/origami/string.rb', line 377 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
#day ⇒ Object (readonly)
Returns the value of attribute day.
375 376 377 |
# File 'lib/origami/string.rb', line 375 def day @day end |
#hour ⇒ Object (readonly)
Returns the value of attribute hour.
375 376 377 |
# File 'lib/origami/string.rb', line 375 def hour @hour end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
375 376 377 |
# File 'lib/origami/string.rb', line 375 def min @min end |
#month ⇒ Object (readonly)
Returns the value of attribute month.
375 376 377 |
# File 'lib/origami/string.rb', line 375 def month @month end |
#sec ⇒ Object (readonly)
Returns the value of attribute sec.
375 376 377 |
# File 'lib/origami/string.rb', line 375 def sec @sec end |
#utc_offset ⇒ Object (readonly)
Returns the value of attribute utc_offset.
375 376 377 |
# File 'lib/origami/string.rb', line 375 def utc_offset @utc_offset end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
375 376 377 |
# File 'lib/origami/string.rb', line 375 def year @year end |
Class Method Details
.now ⇒ Object
Returns current Date String in UTC time.
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/origami/string.rb', line 426 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:
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/origami/string.rb', line 399 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_datetime ⇒ Object
395 396 397 |
# File 'lib/origami/string.rb', line 395 def to_datetime ::DateTime.new(@year, @month, @day, @hour, @min, @sec, (@utc_offset / 3600).to_s) end |