Module: TimepieceHelper

Defined in:
app/helpers/timepiece_helper.rb

Instance Method Summary collapse

Instance Method Details

#analog(location = 'UTC', id: '', size: '10em') ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/timepiece_helper.rb', line 43

def analog(location = 'UTC', id: '', size: '10em')
  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 hours.to_i >= 6 && hours.to_i < 18
    time_of_day_class = 'timepiece-analog-day'
  else
    time_of_day_class = 'timepiece-analog-night'
  end
  if hours.to_i >= 12
    var = 'pm'
  elsif hours.to_i < 12
    var = 'am'
  end
  hours_angle = (hours.to_i * 30) + (minutes.to_i / 2)
  minutes_angle = minutes.to_i * 6
  seconds_angle = seconds.to_i * 6
  time = "<div class='timepiece-hours-container' style='-ms-transform:rotateZ(#{hours_angle}deg);-webkit-transform:rotateZ(#{hours_angle}deg);transform:rotateZ(#{hours_angle}deg);'><div class='timepiece-analog-hours'></div></div>"\
         "<div class='timepiece-minutes-container' style='-ms-transform:rotateZ(#{minutes_angle}deg);-webkit-transform:rotateZ(#{minutes_angle}deg);transform:rotateZ(#{minutes_angle}deg);'><div class='timepiece-analog-minutes'></div></div>"\
         "<div class='timepiece-seconds-container' style='-ms-transform:rotateZ(#{seconds_angle}deg);-webkit-transform:rotateZ(#{seconds_angle}deg);transform:rotateZ(#{seconds_angle}deg);'><div class='timepiece-analog-seconds'></div></div>"\
         "<div class='timepiece-analog-abbr'>" + var + "</div>"
  (:div, time.html_safe, class: 'timepiece-analog ' + time_of_day_class, 'data-timezone' => location, 'id' => (id unless id.blank?), 'style' => 'width:' + size + ';padding-bottom:' + size + ';')
end

#countdown(time_until = Time.new(2016), id: '') ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/timepiece_helper.rb', line 117

def countdown(time_until = Time.new(2016), id: '')
  seconds_diff = (time_until - Time.now).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}</span>"\
         "<span class='timepiece-separator tp-separator-days-hours'>:</span>"\
         "<span class='timepiece-hours'>#{"%02d" % hours}</span>"\
         "<span class='timepiece-separator tp-separator-hours-minutes'>:</span>"\
         "<span class='timepiece-minutes'>#{"%02d" % minutes}</span>"\
         "<span class='timepiece-separator tp-separator-minutes-seconds'>:</span>"\
         "<span class='timepiece-seconds'>#{"%02d" % seconds}</span>"
         # "<span class='timepiece-seconds'>#{seconds.to_s.rjust(2, '0')}</span>" # Note: rjust; it might be useful.

  (:span, time.html_safe, class: 'timepiece-countdown', 'data-days' => days, 'data-hours' => hours, 'data-minutes' => minutes, 'data-seconds' => seconds, 'id' => (id unless id.blank?))
end

#countdown_in_words(time_until = Time.new(2016), id: '') ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/helpers/timepiece_helper.rb', line 142

def countdown_in_words(time_until = Time.new(2016), id: '')
  seconds_diff = (time_until - Time.now).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}</span>"\
         "<span class='timepiece-descriptor tp-descriptor-days'> days </span>"\
         "<span class='timepiece-hours'>#{hours.to_s}</span>"\
         "<span class='timepiece-descriptor tp-descriptor-hours'> hours </span>"\
         "<span class='timepiece-minutes'>#{minutes.to_s}</span>"\
         "<span class='timepiece-descriptor tp-descriptor-minutes'> minutes </span>"\
         "<span class='timepiece-seconds'>#{seconds.to_s}</span>"\
         "<span class='timepiece-descriptor tp-descriptor-seconds'> seconds </span>"
         # "<span class='timepiece-seconds'>#{seconds.to_s.rjust(2, '0')}</span>" # Note: rjust; it might be useful.

  (:span, time.html_safe, class: 'timepiece-countdown', 'data-days' => days, 'data-hours' => hours, 'data-minutes' => minutes, 'data-seconds' => seconds, 'id' => (id unless id.blank?))
end

#timepiece(location = 'UTC', type: '24', lead: 'none', abbr_sep: 'none', id: '') ⇒ 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
41
# File 'app/helpers/timepiece_helper.rb', line 2

def timepiece(location = 'UTC', type: '24', lead: 'none', abbr_sep: 'none', id: '')
  # Note: On the inclusion of IDs, you should /not/ display them if none is given - HTML compliance.
	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 = '&#8199;' + 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 tp-hours-minutes'>:</span>"\
         "<span class='timepiece-minutes'>#{minutes}</span>"\
         "<span class='timepiece-separator tp-separator-2 tp-minutes-seconds'>:</span>"\
         "<span class='timepiece-seconds'>#{seconds}</span>"
  if type == '12'
    time = time + "<span class='timepiece-abbr timepiece-abbr-#{var}'>#{var}</span>"
  end
	(:span, time.html_safe, class: 'timepiece', 'data-timezone' => location, 'data-tptype' => type, 'data-lead' => lead, 'data-abbr_separator' => abbr_sep, 'id' => (id unless id.blank?))
end

#timer(time_since = Time.now, id: '') ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/timepiece_helper.rb', line 68

def timer(time_since = Time.now, id: '')
  seconds_diff = (Time.now - time_since).to_i

  hours = seconds_diff / 3600
  seconds_diff -= hours * 3600

  minutes = seconds_diff / 60
  seconds_diff -= minutes * 60

  seconds = seconds_diff

  time = "<span class='timepiece-hours'>#{"%02d" % hours}</span>"\
         "<span class='timepiece-separator tp-separator-hours-minutes'>:</span>"\
         "<span class='timepiece-minutes'>#{"%02d" % minutes}</span>"\
         "<span class='timepiece-separator tp-separator-minutes-seconds'>:</span>"\
         "<span class='timepiece-seconds'>#{"%02d" % seconds}</span>"
         # "<span class='timepiece-seconds'>#{seconds.to_s.rjust(2, '0')}</span>" # Note: rjust; it might be useful.

  (:span, time.html_safe, class: 'timepiece-timer', 'data-hours' => hours, 'data-minutes' => minutes, 'data-seconds' => seconds, 'id' => (id unless id.blank?))
end