Skip to content

Instantly share code, notes, and snippets.

@aquach
Created September 4, 2015 01:16
Show Gist options
  • Save aquach/8781c7fda5e1b3ce4932 to your computer and use it in GitHub Desktop.
Save aquach/8781c7fda5e1b3ce4932 to your computer and use it in GitHub Desktop.
Automatic Unix timestamp conversion for Hirb tables.
module AutoTimestamp
def query_callback(rows, options)
rows.each do |r|
r.each do |k, v|
if v.is_a?(Integer)
dates = [ Time.at(v), Time.at(v / 1000) ]
plausible_date = dates.find { |d|
d >= Time.now - 10.years && d <= Time.now + 10.years
}
if plausible_date
r[k] = "#{plausible_date.localtime('-07:00').to_s} (#{v})"
end
end
end
end
end
end
Hirb::Helpers::Table.send :include, AutoTimestamp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment