Class: Cldr::Format::Time
Constant Summary
collapse
- PATTERN =
/a{1}|h{1,2}|H{1,2}|K{1,2}|k{1,2}|m{1,2}|s{1,2}|S+|z{1,4}|Z{1,4}/
- METHODS =
{ 'a' => :period,
'h' => :hour,
'H' => :hour,
'K' => :hour,
'k' => :hour,
'm' => :minute,
's' => :second,
'S' => :second_fraction,
'z' => :timezone,
'Z' => :timezone,
'v' => :timezone_generic_non_location,
'V' => :timezone_metazone
}
Instance Attribute Summary
#calendar
Instance Method Summary
collapse
-
#hour(time, pattern, length) ⇒ Object
-
#minute(time, pattern, length) ⇒ Object
-
#period(time, pattern, length) ⇒ Object
-
#round_to(number, precision) ⇒ Object
-
#second(time, pattern, length) ⇒ Object
-
#second_fraction(time, pattern, length) ⇒ Object
-
#timezone(time, pattern, length) ⇒ Object
-
#timezone_generic_non_location(time, pattern, length) ⇒ Object
#initialize
Instance Method Details
#hour(time, pattern, length) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/cldr/format/time.rb', line 24
def hour(time, pattern, length)
hour = time.hour
hour = case pattern[0, 1]
when 'h' hour > 12 ? (hour - 12) : (hour == 0 ? 12 : hour)
when 'H' hour
when 'K' hour > 11 ? hour - 12 : hour
when 'k' hour == 0 ? 24 : hour
end
length == 1 ? hour.to_s : hour.to_s.rjust(length, '0')
end
|
#minute(time, pattern, length) ⇒ Object
39
40
41
|
# File 'lib/cldr/format/time.rb', line 39
def minute(time, pattern, length)
length == 1 ? time.min.to_s : time.min.to_s.rjust(length, '0')
end
|
#period(time, pattern, length) ⇒ Object
20
21
22
|
# File 'lib/cldr/format/time.rb', line 20
def period(time, pattern, length)
calendar[:periods][time.strftime('%p').downcase.to_sym]
end
|
#round_to(number, precision) ⇒ Object
65
66
67
68
|
# File 'lib/cldr/format/time.rb', line 65
def round_to(number, precision)
factor = 10 ** precision
(number * factor).round.to_f / factor
end
|
#second(time, pattern, length) ⇒ Object
43
44
45
|
# File 'lib/cldr/format/time.rb', line 43
def second(time, pattern, length)
length == 1 ? time.sec.to_s : time.sec.to_s.rjust(length, '0')
end
|
#second_fraction(time, pattern, length) ⇒ Object
47
48
49
50
|
# File 'lib/cldr/format/time.rb', line 47
def second_fraction(time, pattern, length)
raise 'can not use the S format with more than 6 digits' if length > 6
(time.usec.to_f / 10 ** (6 - length)).round.to_s.rjust(length, '0')
end
|
#timezone(time, pattern, length) ⇒ Object
52
53
54
55
56
57
58
59
|
# File 'lib/cldr/format/time.rb', line 52
def timezone(time, pattern, length)
case length
when 1..3
time.zone
else
raise 'not yet implemented (requires timezone translation data")'
end
end
|
#timezone_generic_non_location(time, pattern, length) ⇒ Object
61
62
63
|
# File 'lib/cldr/format/time.rb', line 61
def timezone_generic_non_location(time, pattern, length)
raise 'not yet implemented (requires timezone translation data")'
end
|