module BarkestCore::DateParser

This module will add consistent date parsing functions to a class.

Constants

DATE_FORMAT

A simple hash that can be used to validate a date entry column.

validates :my_date, :format => DATE_FORMAT

DATE_REGEX

A regular expression to parse either American format MM/DD/YYYY or ISO format YYYY-MM-DD dates.

NULLABLE_DATE_FORMAT

A simple hash that can be used to validate a nullable date entry column.

validates :my_date, :format => NULLABLE_DATE_FORMAT

NULLABLE_DATE_REGEX

A regular expression to parse either American format MM/DD/YYYY or ISO format YYYY-MM-DD dates, but also allows for a blank value.

NULLABLE_TIME_REGEX

A regular expression to parse either American format MM/DD/YYY HH:MM:SS or ISO format YYYY-MM-DD HH:MM:SS times, but also allows for a blank value.

TIME_REGEX

A regular expression to parse either American format MM/DD/YYY HH:MM:SS or ISO format YYYY-MM-DD HH:MM:SS times.

Public Class Methods

parse_for_date_column(value) click to toggle source

Parses a value for storage in a date/datetime column.

Value should be a string in ‘M/D/YYYY’ or ‘YYYY-MM-DD’ format but can also be a Date or Time object.

Returns a Time object or nil if value is invalid.

# File lib/barkest_core/concerns/date_parser.rb, line 47
def self.parse_for_date_column(value)
  Time.utc_parse(value).date rescue nil
end
parse_for_date_filter(value) click to toggle source

Parses a value for use in a SQL query.

Returns NULL if the parsed date is nil. Otherwise returns the date in ‘YYYY-MM-DD’ format.

# File lib/barkest_core/concerns/date_parser.rb, line 68
def self.parse_for_date_filter(value)
  value = parse_for_date_column(value)
  return 'NULL' unless value
  value.strftime('\'%Y-%m-%d\'')
end
parse_for_time_column(value) click to toggle source

Parses a value for storage in a datetime column.

Value should be a string in ‘M/D/YYYY HH:MM:SS’ or ‘YYYY-MM-DD HH:MM:SS’ format, but can also be a Date or Time object.

Returns a Time object or nil if value is invalid.

# File lib/barkest_core/concerns/date_parser.rb, line 58
def self.parse_for_time_column(value)
  Time.utc_parse(value) rescue nil
end
parse_for_time_filter(value) click to toggle source

Parses a value for use in a SQL query.

Returns NULL if the parsed time is nil. Otherwise returns the time in ‘YYYY-MM-DD HH:MM:SS’ format.

# File lib/barkest_core/concerns/date_parser.rb, line 80
def self.parse_for_time_filter(value)
  value = parse_for_time_column(value)
  return 'NULL' unless value
  value.strftime('\'%Y-%m-%d %H:%M:%S\'')
end