class Origami::Date
:nodoc:
Class representing a Date string.
def self.now
Returns current Date String in UTC time.
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
def self.parse(str) #:nodoc:
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
def initialize(year:, month: 1, day: 1, hour: 0, min: 0, sec: 0, utc_offset: 0)
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
def to_datetime
def to_datetime ::DateTime.new(@year, @month, @day, @hour, @min, @sec, (@utc_offset / 3600).to_s) end