module Sinatra::Helpers

def last_modified(time)

with a '304 Not Modified' response.
equal or later than the time specified, execution is immediately halted
When the current request includes an 'If-Modified-Since' header that is

DateTime, or other object that responds to +to_time+.
and halt if conditional GET matches. The +time+ argument is a Time,
Set the last modified time of the resource (HTTP 'Last-Modified' header)
def last_modified(time)
  return unless time
  if time.respond_to?(:to_time)
      time = time.to_time
  else
      ## make a best effort to convert something else to a time object
      ## if this fails, this should throw an ArgumentError, then the
      # rescue will result in an http 200, which should be safe
      time = Time.parse(time.to_s)
  end
  response['Last-Modified'] = time.httpdate
  # compare based on seconds since epoch
  halt 304 if Time.httpdate(request.env['HTTP_IF_MODIFIED_SINCE']).to_i >= time.to_i
rescue ArgumentError
end