Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Printing the right offset on format

  • Loading branch information...
commit 1172eb9c4aa727f1c272ee6b70635c632b0e1afa 1 parent 22ef5f1
Cameron Dutro authored
4 README.md
View
@@ -89,8 +89,8 @@ In addition to formatting regular decimals, TwitterCLDR supports short and long
`Date`, `Time`, and `DateTime` objects are supported:
```ruby
-DateTime.now.localize(:es).to_full_s # "lunes, 12 de diciembre de 2011 21:44:57 UTC -0800"
-DateTime.now.localize(:es).to_long_s # "12 de diciembre de 2011 21:44:57 -08:00"
+DateTime.now.localize(:es).to_full_s # "lunes, 12 de diciembre de 2011 21:44:57 UTC -08:00"
+DateTime.now.localize(:es).to_long_s # "12 de diciembre de 2011 21:44:57 UTC"
DateTime.now.localize(:es).to_medium_s # "12/12/2011 21:44:57"
DateTime.now.localize(:es).to_short_s # "12/12/11 21:44"
6 lib/twitter_cldr/formatters/base.rb
View
@@ -9,12 +9,12 @@ class Base
attr_reader :tokenizer
def format(obj, options = {})
- process_tokens(self.get_tokens(obj, options), obj)
+ process_tokens(self.get_tokens(obj, options), obj, options)
end
protected
- def process_tokens(tokens, obj)
+ def process_tokens(tokens, obj, options = {})
result = ""
tokens.each_with_index do |token, index|
@@ -22,7 +22,7 @@ def process_tokens(tokens, obj)
when :composite
result << eval(process_tokens(token.tokens, obj)).to_s
when :pattern
- result << self.result_for_token(token, index, obj)
+ result << self.result_for_token(token, index, obj, options)
else
if token.value.size > 0 && token.value[0].chr == "'" && token.value[-1].chr == "'"
result << token.value[1..-2]
57 lib/twitter_cldr/formatters/calendars/datetime_formatter.rb
View
@@ -6,6 +6,10 @@
# This class has been adapted from Sven Fuch's ruby-cldr gem
# See LICENSE for the accompanying license for his contributions
+require 'tzinfo'
+require 'pry'
+require 'pry-nav'
+
module TwitterCldr
module Formatters
class DateTimeFormatter < Base
@@ -49,8 +53,8 @@ def initialize(options = {})
)
end
- def result_for_token(token, index, date)
- self.send(METHODS[token.value[0].chr], date, token.value, token.value.size)
+ def result_for_token(token, index, date, options = {})
+ self.send(METHODS[token.value[0].chr], date, token.value, token.value.size, options)
end
def additional_format_selector
@@ -69,7 +73,7 @@ def tokenizer_cache
# there is incomplete era data in CLDR for certain locales like Hindi
# fall back if that happens
- def era(date, pattern, length)
+ def era(date, pattern, length, options = {})
choices = case length
when 0
["", ""]
@@ -86,22 +90,22 @@ def era(date, pattern, length)
end
end
- def year(date, pattern, length)
+ def year(date, pattern, length, options = {})
year = date.year.to_s
year = year.length == 1 ? year : year[-2, 2] if length == 2
year = year.rjust(length, '0') if length > 1
year
end
- def year_of_week_of_year(date, pattern, length)
+ def year_of_week_of_year(date, pattern, length, options = {})
raise NotImplementedError
end
- def day_of_week_in_month(date, pattern, length) # e.g. 2nd Wed in July
+ def day_of_week_in_month(date, pattern, length, options = {}) # e.g. 2nd Wed in July
raise NotImplementedError
end
- def quarter(date, pattern, length)
+ def quarter(date, pattern, length, options = {})
quarter = (date.month.to_i - 1) / 3 + 1
case length
when 1
@@ -115,7 +119,7 @@ def quarter(date, pattern, length)
end
end
- def quarter_stand_alone(date, pattern, length)
+ def quarter_stand_alone(date, pattern, length, options = {})
quarter = (date.month.to_i - 1) / 3 + 1
case length
when 1
@@ -133,7 +137,7 @@ def quarter_stand_alone(date, pattern, length)
end
end
- def month(date, pattern, length)
+ def month(date, pattern, length, options = {})
case length
when 1
date.month.to_s
@@ -151,7 +155,7 @@ def month(date, pattern, length)
end
end
- def month_stand_alone(date, pattern, length)
+ def month_stand_alone(date, pattern, length, options = {})
case length
when 1
date.month.to_s
@@ -168,7 +172,7 @@ def month_stand_alone(date, pattern, length)
end
end
- def day(date, pattern, length)
+ def day(date, pattern, length, options = {})
case length
when 1
date.day.to_s
@@ -177,7 +181,7 @@ def day(date, pattern, length)
end
end
- def weekday(date, pattern, length)
+ def weekday(date, pattern, length, options = {})
key = WEEKDAY_KEYS[date.wday]
case length
when 1..3
@@ -189,7 +193,7 @@ def weekday(date, pattern, length)
end
end
- def weekday_local(date, pattern, length)
+ def weekday_local(date, pattern, length, options = {})
# "Like E except adds a numeric value depending on the local starting day of the week"
# CLDR does not contain data as to which day is the first day of the week, so we will assume Monday (Ruby default)
case length
@@ -200,7 +204,7 @@ def weekday_local(date, pattern, length)
end
end
- def weekday_local_stand_alone(date, pattern, length)
+ def weekday_local_stand_alone(date, pattern, length, options = {})
case length
when 1
weekday_local(date, pattern, length)
@@ -209,14 +213,14 @@ def weekday_local_stand_alone(date, pattern, length)
end
end
- def period(time, pattern, length)
+ def period(time, pattern, length, options = {})
# Always use :wide form. Day-period design was changed in CLDR -
# http://cldr.unicode.org/development/development-process/design-proposals/day-period-design that means some
# major changes are required for a full support of day periods.
@tokenizer.calendar[:periods][:format][:wide][time.strftime('%p').downcase.to_sym]
end
- def hour(time, pattern, length)
+ def hour(time, pattern, length, options = {})
hour = time.hour
hour = case pattern[0, 1]
when 'h' # [1-12]
@@ -231,29 +235,36 @@ def hour(time, pattern, length)
length == 1 ? hour.to_s : hour.to_s.rjust(length, '0')
end
- def minute(time, pattern, length)
+ def minute(time, pattern, length, options = {})
length == 1 ? time.min.to_s : time.min.to_s.rjust(length, '0')
end
- def second(time, pattern, length)
+ def second(time, pattern, length, options = {})
length == 1 ? time.sec.to_s : time.sec.to_s.rjust(length, '0')
end
- def second_fraction(time, pattern, length)
+ def second_fraction(time, pattern, length, options = {})
raise ArgumentError.new('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
- def timezone(time, pattern, length)
+ def timezone(time, pattern, length, options = {})
+ # ruby is dumb and doesn't let you set non-UTC timezones in dates/times, so we have to pass it as an option instead
+ timezone_info = TZInfo::Timezone.get(options[:timezone] || "UTC")
+
case length
when 1..3
- time.zone
+ timezone_info.current_period.abbreviation.to_s
else
- "UTC #{time.strftime("%z")}"
+ hours = (timezone_info.current_period.utc_offset.to_f / 60 ** 2).abs
+ divisor = hours.to_i
+ minutes = (hours % (divisor == 0 ? 1 : divisor)) * 60
+ sign = timezone_info.current_period.utc_offset < 0 ? "-" : "+"
+ "UTC #{sign}#{divisor.to_s.rjust(2, "0")}:#{minutes.floor.to_s.rjust(2, "0")}"
end
end
- def timezone_generic_non_location(time, pattern, length)
+ def timezone_generic_non_location(time, pattern, length, options = {})
raise NotImplementedError, 'requires timezone translation data'
end
end
2  lib/twitter_cldr/localized/localized_datetime.rb
View
@@ -19,7 +19,7 @@ def initialize(obj, locale, options = {})
TwitterCldr::Tokenizers::DateTimeTokenizer::VALID_TYPES.each do |format_type|
define_method "to_#{format_type}_s" do
- @formatter.format(base_in_timezone, :type => format_type.to_sym)
+ @formatter.format(base_in_timezone, chain_params.merge(:type => format_type.to_sym))
end
end
2  lib/twitter_cldr/localized/localized_time.rb
View
@@ -10,7 +10,7 @@ class LocalizedTime < LocalizedDateTime
def to_datetime(date)
date_obj = date.is_a?(LocalizedDate) ? date.base_obj : date
dt = DateTime.parse("#{date_obj.strftime("%Y-%m-%d")}T#{@base_obj.strftime("%H:%M:%S%z")}")
- LocalizedDateTime.new(dt, @locale, :calendar_type => @calendar_type, :timezone => @timezone)
+ LocalizedDateTime.new(dt, @locale, chain_params)
end
def to_time(base_time = Time.now)
2  spec/formatters/calendars/datetime_formatter_spec.rb
View
@@ -346,7 +346,7 @@
@formatter.send(:timezone, Time.gm(2000, 1, 1, 1, 1, 1), 'z', 1).should == 'UTC'
@formatter.send(:timezone, Time.gm(2000, 1, 1, 1, 1, 1), 'zz', 2).should == 'UTC'
@formatter.send(:timezone, Time.gm(2000, 1, 1, 1, 1, 1), 'zzz', 3).should == 'UTC'
- @formatter.send(:timezone, Time.gm(2000, 1, 1, 1, 1, 1), 'zzzz', 4).should =~ /^UTC (-|\+)\d{4}$/
+ @formatter.send(:timezone, Time.gm(2000, 1, 1, 1, 1, 1), 'zzzz', 4).should =~ /^UTC (-|\+)\d{2}:\d{2}$/
end
end
6 spec/readme_spec.rb
View
@@ -60,8 +60,8 @@ def spaces(str)
date_time = DateTime.new(2011, 12, 12, 21, 44, 57, 0.0)
time = Time.at(date_time.localize.to_time.base_obj.utc)
- date_time.localize(:es).to_full_s.should == "lunes, 12 de diciembre de 2011 21:44:57 UTC +0000"
- date_time.localize(:es).to_long_s.should == "12 de diciembre de 2011 21:44:57 +00:00"
+ date_time.localize(:es).to_full_s.should == "lunes, 12 de diciembre de 2011 21:44:57 UTC +00:00"
+ date_time.localize(:es).to_long_s.should == "12 de diciembre de 2011 21:44:57 UTC"
date_time.localize(:es).to_medium_s.should == "12/12/2011 21:44:57"
date_time.localize(:es).to_short_s.should == "12/12/11 21:44"
@@ -70,7 +70,7 @@ def spaces(str)
date_time.localize(:es).to_date.to_medium_s.should == "12/12/2011"
date_time.localize(:es).to_date.to_short_s.should == "12/12/11"
- time.localize(:es).to_full_s.should match(/21:44:57 UTC [-+]\d{4}/)
+ time.localize(:es).to_full_s.should match(/21:44:57 UTC [-+]\d{2}:\d{2}/)
time.localize(:es).to_long_s.should == "21:44:57 UTC"
time.localize(:es).to_medium_s.should == "21:44:57"
time.localize(:es).to_short_s.should == "21:44"
Please sign in to comment.
Something went wrong with that request. Please try again.