Class: ParameterSubstitution::Formatters::ParseTime

Inherits:
DateTimeFormat show all
Defined in:
lib/parameter_substitution/formatters/parse_time.rb

Constant Summary

Constants inherited from DateTimeFormat

DateTimeFormat::MINIMUM_INTEGER_TIME

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DateTimeFormat

from_custom_time, from_parse, from_unix_time_ms, from_unix_time_sec, from_yyyymmddhhmmssss, parse_to_time

Methods inherited from Base

encoding, format, key, parse_duration

Constructor Details

#initialize(format_string) ⇒ ParseTime

Returns a new instance of ParseTime.



12
13
14
# File 'lib/parameter_substitution/formatters/parse_time.rb', line 12

def initialize(format_string)
  @format_string = format_string
end

Class Method Details

.descriptionObject



4
5
6
# File 'lib/parameter_substitution/formatters/parse_time.rb', line 4

def self.description
"Takes format_string as a parameter and uses format_string to parse the input as a time. Does not change the input if the value is not a time."
end

.has_parameters?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/parameter_substitution/formatters/parse_time.rb', line 8

def self.has_parameters?
  true
end

Instance Method Details

#format(value) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/parameter_substitution/formatters/parse_time.rb', line 16

def format(value)
  value && Time.strptime(value.to_s, @format_string).strftime('%Y-%m-%d %H:%M:%S')
rescue ArgumentError => ex
  # strptime raises argument error if either argument is wrong.
  ex.message =~ /invalid .*strptime format/ or raise
  value
end