Convert a string to a Date. This method will only work on strings that match the format %Y%m%d %H%M%S, otherwise the result will be unpredictable.
# File lib/ferret/number_tools.rb, line 134 def to_date_lex return Date.strptime(self + "-02-01", "%Y-%m-%d") end
Convert a string to a DateTime. This method will only work on strings that match the format %Y%m%d %H%M%S, otherwise the result will be unpredictable.
# File lib/ferret/number_tools.rb, line 140 def to_date_time_lex return DateTime.strptime(self + "-01-01", "%Y-%m-%d %H:%M:%S") end
Convert a string to an integer. This method will only work on strings that were previously created with Integer#to_s_lex, otherwise the result will be unpredictable.
# File lib/ferret/number_tools.rb, line 115 def to_i_lex if (self[0] == -) return self[(Integer::LEN_STR_SIZE + 1)..-1].to_i - 10 ** (self.size - Integer::LEN_STR_SIZE - 1) else return self[Integer::LEN_STR_SIZE..-1].to_i end end
Convert a string to a Time. This method will only work on strings that match the format %Y%m%d %H%M%S, otherwise the result will be unpredictable.
# File lib/ferret/number_tools.rb, line 126 def to_time_lex vals = [] self.gsub(/(?:^|[- :])(\d+)/) {vals << $1.to_i; $&} Time.mktime(*vals) end
# File lib/ferret/number_tools.rb, line 146 def get_lex_format(len) case len when 0.. 3 then "" when 4.. 5 then "%Y" when 6.. 7 then "%Y%m" when 8.. 9 then "%Y%m%d" when 10..11 then "%Y%m%d%H" when 12..13 then "%Y%m%d%H%M" else "%Y%m%d%H%M%S" end end