Class: Cldr::Format::Time

Inherits:
Datetime::Base show all
Defined in:
lib/cldr/format/time.rb

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 =

ignoring u, l, g, j, A

{ # ignoring u, l, g, j, A
  '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

Attributes inherited from Datetime::Base

#calendar

Instance Method Summary collapse

Methods inherited from Datetime::Base

#initialize

Constructor Details

This class inherits a constructor from Cldr::Format::Datetime::Base

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' # [1-12]
    hour > 12 ? (hour - 12) : (hour == 0 ? 12 : hour)
  when 'H' # [0-23]
    hour
  when 'K' # [0-11]
    hour > 11 ? hour - 12 : hour
  when 'k' # [1-24]
    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][:format][:wide][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