module Jekyll::Filters::DateFilters

def date_to_long_string(date, type = nil, style = nil)

Returns the formatted String.

Otherwise it will be in UK format.
style - if "US" the returned String will be in US format.
type - if "ordinal" the returned String will be in ordinal format
date - the Time to format.

UK format is the default.
(e.g. "27th January 2011") and US ("e.g. January 27th, 2011") formats.
Ordinal format is also supported, in both the UK
Format a date in long format e.g. "27 January 2011".
def date_to_long_string(date, type = nil, style = nil)
  stringify_date(date, "%B", type, style)
end

def date_to_rfc822(date)

Returns the formatted String.

# => "Sun, 24 Apr 2011 12:34:46 +0000"
date_to_rfc822(Time.now)

Examples

date - The Time to format.

Format a date according to RFC-822
def date_to_rfc822(date)
  return date if date.to_s.empty?
  time(date).rfc822
end

def date_to_string(date, type = nil, style = nil)

Returns the formatting String.

Otherwise it will be in UK format.
style - if "US" the returned String will be in US format.
type - if "ordinal" the returned String will be in ordinal format
date - the Time to format.

UK format is the default.
(e.g. "27th Jan 2011") and US ("e.g. Jan 27th, 2011") formats.
Ordinal format is also supported, in both the UK
Format a date in short format e.g. "27 Jan 2011".
def date_to_string(date, type = nil, style = nil)
  stringify_date(date, "%b", type, style)
end

def date_to_xmlschema(date)

Returns the formatted String.

# => "2011-04-24T20:34:46+08:00"
date_to_xmlschema(Time.now)

Examples

date - The Time to format.

Format a date for use in XML.
def date_to_xmlschema(date)
  return date if date.to_s.empty?
  time(date).xmlschema
end

def ordinal(number)

def ordinal(number)
  return "th" if (11..13).cover?(number)
  case number % 10
  when 1 then "st"
  when 2 then "nd"
  when 3 then "rd"
  else "th"
  end
end

def stringify_date(date, month_type, type = nil, style = nil)

Returns a stringified date or the empty input.

style: nil (default) or "US"
type: nil (default) or "ordinal"
month_type: Notations that evaluate to 'Month' via `Time#strftime` ("%b", "%B")
def stringify_date(date, month_type, type = nil, style = nil)
  return date if date.to_s.empty?
  time = time(date)
  if type == "ordinal"
    day = time.day
    ordinal_day = "#{day}#{ordinal(day)}"
    return time.strftime("#{month_type} #{ordinal_day}, %Y") if style == "US"
    return time.strftime("#{ordinal_day} #{month_type} %Y")
  end
  time.strftime("%d #{month_type} %Y")
end

def time(input)

def time(input)
  date = Liquid::Utils.to_date(input)
  unless date.respond_to?(:to_time)
    raise Errors::InvalidDateError,
          "Invalid Date: '#{input.inspect}' is not a valid datetime."
  end
  date.to_time.dup.localtime
end