Module: BatchKit::Helpers::DateTime
- Defined in:
- lib/batch-kit/helpers/date_time.rb
Overview
Methods for displaying times/durations
Class Method Summary collapse
-
.display_duration(elapsed) ⇒ Object
Converts the elapsed time in seconds to a string showing days, hours, minutes and seconds.
-
.display_timestamp(ts) ⇒ String
Displays a date/time in abbreviated format, suppressing elements of the format string for more recent dates/times.
Class Method Details
.display_duration(elapsed) ⇒ Object
Converts the elapsed time in seconds to a string showing days, hours, minutes and seconds.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/batch-kit/helpers/date_time.rb', line 11 def display_duration(elapsed) return nil unless elapsed elapsed = elapsed.round display = '' [['days', 86400], ['h', 3600], ['m', 60], ['s', 1]].each do |int, seg| if elapsed >= seg count, elapsed = elapsed.divmod(seg) display << "#{count}#{int.length > 1 && count == 1 ? int[0..-2] : int} " elsif display.length > 0 display << "0#{int}" end end display = "0s" if display == '' display.strip end |
.display_timestamp(ts) ⇒ String
Displays a date/time in abbreviated format, suppressing elements of the format string for more recent dates/times.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/batch-kit/helpers/date_time.rb', line 34 def (ts) return unless ts ts_date = ts.to_date today = Date.today if today - ts_date < 7 # Date is within the last week ts.strftime('%a %H:%M:%S') elsif today.year != ts.year # Data is from a different year ts.strftime('%a %b %d %Y %H:%M:%S') else ts.strftime('%a %b %d %H:%M:%S') end end |