struct Athena::Routing::TimeConverter

Overview

Converts a date(time) string into a Time instance.

Optionally allows specifying the format and location to use when parsing the string. If no format is specified, defaults to Time.rfc_3339. Defaults to UTC if no location is specified with the format.

Raises an ART::Exceptions::BadRequest if the date(time) string could not be parsed.

NOTE The format can be anything supported via Time::Format.

Example

require "athena"

class ExampleController < ART::Controller
  @[ARTA::Get(path: "/event/:start_time/:end_time")]
  @[ARTA::ParamConverter("start_time", converter: ART::TimeConverter, format: "%F", location: Time::Location.load("Europe/Berlin"))]
  @[ARTA::ParamConverter("end_time", converter: ART::TimeConverter)]
  def event(start_time : Time, end_time : Time) : Nil
    start_time # => 2020-04-07 00:00:00.0 +02:00 Europe/Berlin
    end_time   # => 2020-04-08 12:34:56.0 UTC
  end
end

ART.run

# GET /event/2020-04-07/2020-04-08T12:34:56Z

Defined in:

time_converter.cr

Instance Method Summary

Instance methods inherited from struct Athena::Routing::ParamConverterInterface

apply(request : HTTP::Request, configuration : Configuration) : Nil apply, initialize initialize

Constructor methods inherited from struct Athena::Routing::ParamConverterInterface

new new

Instance Method Detail

def apply(request : HTTP::Request, configuration : Configuration) : Nil #

Applies the conversion logic based on the provided request and configuration.

Most commonly this involves setting/overriding a value stored in the request's ART::ParameterBag via request.attributes.


[View source]