Class: ICU::TimeFormatting::DateTimeFormatter
- Inherits:
-
BaseFormatter
- Object
- BaseFormatter
- ICU::TimeFormatting::DateTimeFormatter
- Defined in:
- lib/ffi-icu/time_formatting.rb
Instance Method Summary collapse
- #date_format(localized = true) ⇒ Object
- #format(dt) ⇒ Object
-
#initialize(options = {}) ⇒ DateTimeFormatter
constructor
A new instance of DateTimeFormatter.
- #parse(str) ⇒ Object
- #set_date_format(localized, pattern_str) ⇒ Object
-
#update_tz_format(format, tz_style) ⇒ Object
time-zone formating.
Methods inherited from BaseFormatter
Constructor Details
#initialize(options = {}) ⇒ DateTimeFormatter
Returns a new instance of DateTimeFormatter.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ffi-icu/time_formatting.rb', line 87 def initialize(={}) time_style = [:time] || :short date_style = [:date] || :short locale = [:locale] || 'C' tz_style = [:tz_style] time_zone = [:zone] @f = make_formatter(time_style, date_style, locale, time_zone) if tz_style f0 = date_format(true) f1 = update_tz_format(f0, tz_style) if f1 != f0 set_date_format(true, f1) end end end |
Instance Method Details
#date_format(localized = true) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/ffi-icu/time_formatting.rb', line 151 def date_format(localized=true) needed_length = 0 out_ptr = UCharPointer.new(needed_length) retried = false begin Lib.check_error do |error| needed_length = Lib.udat_toPattern(@f, localized, out_ptr, needed_length, error) end out_ptr.string rescue BufferOverflowError raise BufferOverflowError, "needed: #{needed_length}" if retried out_ptr = out_ptr.resized_to needed_length retried = true retry end end |
#format(dt) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ffi-icu/time_formatting.rb', line 112 def format(dt) needed_length = 0 out_ptr = UCharPointer.new(needed_length) retried = false begin Lib.check_error do |error| case dt when Date needed_length = Lib.udat_format(@f, Time.mktime( dt.year, dt.month, dt.day, 0, 0, 0, 0 ).to_f * 1000.0, out_ptr, needed_length, nil, error) when Time needed_length = Lib.udat_format(@f, dt.to_f * 1000.0, out_ptr, needed_length, nil, error) end end out_ptr.string rescue BufferOverflowError raise BufferOverflowError, "needed: #{needed_length}" if retried out_ptr = out_ptr.resized_to needed_length retried = true retry end end |
#parse(str) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/ffi-icu/time_formatting.rb', line 103 def parse(str) str_u = UCharPointer.from_string(str) str_l = str_u.size Lib.check_error do |error| ret = Lib.udat_parse(@f, str_u, str_l, nil, error) Time.at(ret / 1000.0) end end |
#set_date_format(localized, pattern_str) ⇒ Object
171 172 173 174 175 176 177 178 |
# File 'lib/ffi-icu/time_formatting.rb', line 171 def set_date_format(localized, pattern_str) pattern = UCharPointer.from_string(pattern_str) pattern_len = pattern_str.size Lib.check_error do |error| needed_length = Lib.udat_applyPattern(@f, localized, pattern, pattern_len) end end |
#update_tz_format(format, tz_style) ⇒ Object
time-zone formating
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/ffi-icu/time_formatting.rb', line 138 def update_tz_format(format, tz_style) return format if format !~ /(.*?)(\s*(?:[zZOVV]+\s*))(.*?)/ pre, tz, suff = $1, $2, $3 if tz_style == :none tz = ((tz =~ /\s/) && !pre.empty? && !suff.empty?) ? ' ' : '' else repl = TZ_MAP[tz_style] raise 'no such tz_style' unless repl tz.gsub!(/^(\s*)(.*?)(\s*)$/, '\1'+repl+'\3') end pre + tz + suff end |