Skip to content

Instantly share code, notes, and snippets.

@reinh
Created August 24, 2012 16:08
Show Gist options
  • Save reinh/3452348 to your computer and use it in GitHub Desktop.
Save reinh/3452348 to your computer and use it in GitHub Desktop.
# Output a TIME tag for correct browser-local time.
#
# options - The hash of options that specifies formats and such (default: {}).
# :format - The Symbol for to_formatted_s or the String used
# for strftime.
# :moment_format - The String used by moment.js to format.
# See: http://momentjs.com/docs/#/displaying/format/
# :humanize - If true then will use humanized (relative)
# version of the time.
#
# Examples
#
# time_tag Time.now, format: :long, moment_format: 'LLL'
#
# time_tag Time.now, humanize: true
#
# Returns the TIME tag.
def time_tag(time, options={})
options.symbolize_keys!
options[:datetime] = time.getutc.iso8601
format = options.delete(:format)
options['data-format'] = options.delete(:moment_format)
options['data-humanize'] = options.delete(:humanize)
formatted_time = if Time::DATE_FORMATS.has_key?(format.to_sym)
then time.to_s(format.to_sym)
else time.strftime(format)
end
output = options['data-humanize'] ? time_ago_in_words(time) : formatted_time
content_tag(:time, output, options)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment