Skip to content

Commit

Permalink
Improve number format helper
Browse files Browse the repository at this point in the history
  • Loading branch information
martijn committed Apr 16, 2021
1 parent 8b70e86 commit c653bb8
Showing 1 changed file with 9 additions and 27 deletions.
36 changes: 9 additions & 27 deletions lib/xsv/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,38 +103,20 @@ def parse_number(string)

# Apply date or time number formats, if applicable
def parse_number_format(number, format)
number = parse_number(number) if number.is_a?(String)
number = parse_number(number) # number is always a string since it comes out of the Sax Parser

if datetime_format?(format)
is_date_format = format.scan(/[dmy]+/).length > 1
is_time_format = format.scan(/[hms]+/).length > 1

if !is_date_format && !is_time_format
number
elsif is_date_format && is_time_format
parse_datetime(number)
elsif date_format?(format)
elsif is_date_format
parse_date(number)
elsif time_format?(format)
elsif is_time_format
parse_time(number)
else
number
end
end

# Tests if the given format string includes both date and time
def datetime_format?(format)
date_format?(format) && time_format?(format)
end

# Tests if the given format string is a date
def date_format?(format)
return false if format.nil?

# If it contains at least 2 sequences of d's, m's or y's it's a date!
format.scan(/[dmy]+/).length > 1
end

# Tests if the given format string is a time
def time_format?(format)
return false if format.nil?

# If it contains at least 2 sequences of h's, m's or s's it's a time!
format.scan(/[hms]+/).length > 1
end
end
end

0 comments on commit c653bb8

Please sign in to comment.