Class: Kiss::Format::DateTime

Inherits:
Kiss::Format show all
Defined in:
lib/kiss/format.rb

Direct Known Subclasses

Date, Time

Class Method Summary collapse

Methods inherited from Kiss::Format

inherited, lookup

Class Method Details

.convert_value_local_to_utc(value, context) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/kiss/format.rb', line 123

def convert_value_local_to_utc(value, context)
  if value && !value.zero? && (timezone = context[:timezone])
    timezone = TZInfo::Timezone.get(timezone) if timezone.is_a?(String)
    timezone.local_to_utc(value)
  else
    value
  end
end

.convert_value_utc_to_local(value, context) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/kiss/format.rb', line 132

def convert_value_utc_to_local(value, context)
  if value && !value.zero? && (timezone = context[:timezone])
    timezone = TZInfo::Timezone.get(timezone) if timezone.is_a?(String)
    timezone.utc_to_local(value)
  else
    value
  end
end

.parse(value, context = {}) ⇒ Object



117
118
119
120
121
# File 'lib/kiss/format.rb', line 117

def parse(value, context = {})
  return nil unless value =~ /\S/
  relative_time = context[:year] ? ::Time.parse("1/1/#{context[:year]}") : ::Time.now
  convert_value_local_to_utc(::Time.parse(value.gsub(/[-\.]/, '/'), relative_time), context)
end

.validate(value, context = {}) ⇒ Object



145
146
147
# File 'lib/kiss/format.rb', line 145

def validate(value, context = {})
  value.blank? ? nil : super(value, context)
end

.value_to_s(value, context = {}) ⇒ Object



141
142
143
# File 'lib/kiss/format.rb', line 141

def value_to_s(value, context = {})
  convert_value_utc_to_local(value, context).strftime("%m/%d/%Y %I:%M %p").gsub(/0(\d[\:\/])/, '\1')
end