Module: TimepieceHelper
- Defined in:
- app/helpers/timepiece_helper.rb
Instance Method Summary collapse
- #timepiece(location = 'UTC', type: '24', lead: 'none', abbr_sep: 'none') ⇒ Object
- #timer(time_since = Time.now) ⇒ Object
Instance Method Details
#timepiece(location = 'UTC', type: '24', lead: 'none', abbr_sep: 'none') ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/helpers/timepiece_helper.rb', line 2 def timepiece(location = 'UTC', type: '24', lead: 'none', abbr_sep: 'none') Time.zone = location hours = Time.now.in_time_zone.strftime('%H') minutes = Time.now.in_time_zone.strftime('%M') seconds = Time.now.in_time_zone.strftime('%S') if type == '12' hours = hours.to_i if hours > 12 hours = hours - 12 var = 'pm' elsif hours == 0 hours = 12 var = 'am' elsif hours == 12 var = 'pm' elsif hours < 12 var = 'am' end if hours < 10 if lead == '0' || lead == 'zero' hours = '0' + hours.to_s elsif lead == '_' || lead == 'space' hours = ' ' + hours.to_s end end if abbr_sep == '.' var = var.gsub(/([apm])/, '\1.') end end time = "<span class='timepiece-hours'>#{hours}</span>"\ "<span class='timepiece-separator tp-separator-1'>:</span>"\ "<span class='timepiece-minutes'>#{minutes}</span>"\ "<span class='timepiece-separator tp-separator-2'>:</span>"\ "<span class='timepiece-seconds'>#{seconds}</span>" if type == '12' time = time + "<span class='timepiece-abbr timepiece-abbr-#{var}'>#{var}</span>" end content_tag(:span, time.html_safe, class: 'timepiece', 'data-timezone' => location, 'data-tptype' => type, 'data-lead' => lead, 'data-abbr_separator' => abbr_sep) end |
#timer(time_since = Time.now) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/helpers/timepiece_helper.rb', line 42 def timer(time_since = Time.now) seconds_diff = (Time.now - time_since).to_i days = seconds_diff / 86400 seconds_diff -= days * 86400 hours = seconds_diff / 3600 seconds_diff -= hours * 3600 minutes = seconds_diff / 60 seconds_diff -= minutes * 60 seconds = seconds_diff time = "<span class='timepiece-days'>#{days.to_s.rjust(2, '0')}</span>"\ "<span class='timepiece-descriptor tp-descriptor-days'> days </span>"\ "<span class='timepiece-hours'>#{hours.to_s.rjust(2, '0')}</span>"\ "<span class='timepiece-separator tp-separator-1'>:</span>"\ "<span class='timepiece-minutes'>#{minutes.to_s.rjust(2, '0')}</span>"\ "<span class='timepiece-separator tp-separator-2'>:</span>"\ "<span class='timepiece-seconds'>#{seconds.to_s.rjust(2, '0')}</span>" content_tag(:span, time.html_safe, class: 'timepiece-timer', 'data-days' => days, 'data-hours' => hours, 'data-minutes' => minutes, 'data-seconds' => seconds) end |