Class: Rescuetime::DateParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rescuetime/date_parser.rb

Overview

Responsible for formatting user-inputted dates into proper Rescuetime format

Since:

  • v0.3.2

Constant Summary collapse

DATE_FORMATS =

Valid string formats for date parsing. The key is a human-readable pattern (ex. ‘yyyy-mm-dd’), and the value is the corresponding regular expression.

Since:

  • v0.3.2

{
  'yyyy-mm-dd'                => /\d{4}-\d{2}-\d{2}/,
  'yyyy/mm/dd'                => %r{\d{4}\/\d{2}\/\d{2}},
  'mm-dd-yyyy or mm/dd/yyyy'  => %r{\d{2}[-\/]\d{2}[-\/]\d{4}},
  'mm-dd or mm/dd'            => %r{\d{2}[-\/]\d{2}}
}.freeze

Class Method Summary collapse

Class Method Details

.parse(date) ⇒ String

Returns a date as a string in the correct Rescuetime API format

Allowed date formats:

  • Object that responds to #strftime

  • String in “YYYY-MM-DD” format

  • String in “YYYY/MM/DD” format

  • String in “MM-DD-YYYY” format

  • String in “MM/DD/YYYY” format

  • String in “MM-DD” format (defaults to current year)

  • String in “MM/DD” format (defaults to current year)

Examples:

Basic Use

parser = Rescuetime::DateParser
parser.parse '12-25' # assuming the current year is 2015
#=> '2015-12-25'

parser.parse '2015/12/25'
#=> '2015-12-25'

parser.parse Time.parse('2015-12-25')
#=> '2015-12-25'

parser.parse 'Dec. 25, 2015'
#=> Rescuetime::Errors::InvalidQueryError: Invalid date entered. Please
#                                  see docs for allowed formats.

Parameters:

  • date (#strftime, String)

    a date to be formatted

Returns:

  • (String)

    a date string in the YYYY-MM-DD format

Raises:

Since:

  • v0.3.2



47
48
49
50
51
52
53
# File 'lib/rescuetime/date_parser.rb', line 47

def parse(date)
  if date.respond_to? :strftime
    date.strftime '%Y-%m-%d'
  else
    reformat_string(date)
  end
end