Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/timeliness/core_ext/string.rb

Instance Method Summary collapse

Instance Method Details

#to_dateObject



9
10
11
12
13
# File 'lib/timeliness/core_ext/string.rb', line 9

def to_date
  return nil if self.blank?
  values = Timeliness::Parser._parse(self, :date).map { |arg| arg || 0 }
  ::Date.new(*values[0..2])
end

#to_datetimeObject



15
16
17
18
19
20
21
# File 'lib/timeliness/core_ext/string.rb', line 15

def to_datetime
  return nil if self.blank?
  values = Timeliness::Parser._parse(self, :datetime).map { |arg| arg || 0 }
  values[7] = values[7]/24.hours.to_f if values[7] != 0
  values[5] += Rational(values.delete_at(6), 1000000)
  ::DateTime.civil(*values)
end

#to_time(form = :utc) ⇒ Object

Form can be either :utc (default) or :local.



4
5
6
7
# File 'lib/timeliness/core_ext/string.rb', line 4

def to_time(form = :utc)
  return nil if self.blank?
  Timeliness::Parser.parse(self, :datetime, zone: form)
end