From 548910a67a194d5daf588e3d9a8441a941809a7c Mon Sep 17 00:00:00 2001 From: thedarkone Date: Wed, 7 Aug 2013 15:11:03 +0200 Subject: Use TS::Cache instead plain Hash in TimeZone. Plain ruby Hashes are not thread safe. --- activesupport/lib/active_support/values/time_zone.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'activesupport/lib/active_support/values') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 3cf82a24b9..2c78fe3ee8 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -1,3 +1,4 @@ +require 'thread_safe' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/try' @@ -362,10 +363,11 @@ module ActiveSupport def zones_map @zones_map ||= begin - new_zones_names = MAPPING.keys - lazy_zones_map.keys - new_zones = Hash[new_zones_names.map { |place| [place, create(place)] }] - - lazy_zones_map.merge(new_zones) + map = {} + MAPPING.each_key do |place| + map[place] = lazy_zones_map.fetch(place) { create(place) } + end + map end end @@ -414,7 +416,7 @@ module ActiveSupport def lazy_zones_map require_tzinfo - @lazy_zones_map ||= Hash.new do |hash, place| + @lazy_zones_map ||= ThreadSafe::Cache.new do |hash, place| hash[place] = create(place) if MAPPING.has_key?(place) end end -- cgit v1.2.3 From c4e5db4d451e1537ab96405193150e3785f3ecf9 Mon Sep 17 00:00:00 2001 From: thedarkone Date: Wed, 7 Aug 2013 17:15:58 +0200 Subject: Unify AS::TZ's lazy init maps. There's no point in having 2 almost identical (@lazy_zones_map and @zones_map) lazy initialized TZ instance caches. --- activesupport/lib/active_support/values/time_zone.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'activesupport/lib/active_support/values') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 2c78fe3ee8..b8e6b06087 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -185,6 +185,8 @@ module ActiveSupport UTC_OFFSET_WITH_COLON = '%s%02d:%02d' UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.sub(':', '') + @lazy_zones_map = ThreadSafe::Cache.new + # Assumes self represents an offset from UTC in seconds (as returned from # Time#utc_offset) and turns this into an +HH:MM formatted string. # @@ -363,11 +365,8 @@ module ActiveSupport def zones_map @zones_map ||= begin - map = {} - MAPPING.each_key do |place| - map[place] = lazy_zones_map.fetch(place) { create(place) } - end - map + MAPPING.each_key {|place| self[place]} # load all the zones + @lazy_zones_map end end @@ -415,10 +414,7 @@ module ActiveSupport def lazy_zones_map require_tzinfo - - @lazy_zones_map ||= ThreadSafe::Cache.new do |hash, place| - hash[place] = create(place) if MAPPING.has_key?(place) - end + @lazy_zones_map end end -- cgit v1.2.3 From 2a8c2582c9023f4ac02fe9f09f61c4f9c88e4ebd Mon Sep 17 00:00:00 2001 From: thedarkone Date: Wed, 7 Aug 2013 17:21:57 +0200 Subject: Remove AS::TZ.lookup(name). The method doesn't really make much sense (find_tzinfo will succeed for any imput provided). --- activesupport/lib/active_support/values/time_zone.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'activesupport/lib/active_support/values') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index b8e6b06087..b6d9257f00 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -379,7 +379,7 @@ module ActiveSupport case arg when String begin - lazy_zones_map[arg] ||= lookup(arg).tap { |tz| tz.utc_offset } + lazy_zones_map[arg] ||= create(arg).tap { |tz| tz.utc_offset } rescue TZInfo::InvalidTimezoneIdentifier nil end @@ -408,10 +408,6 @@ module ActiveSupport private - def lookup(name) - (tzinfo = find_tzinfo(name)) && create(tzinfo.name.freeze) - end - def lazy_zones_map require_tzinfo @lazy_zones_map -- cgit v1.2.3 From b1b9a0aeca879b1c1bc2c8a74f2c9cabd143b9bb Mon Sep 17 00:00:00 2001 From: Lauro Caetano Date: Tue, 3 Dec 2013 12:04:25 -0200 Subject: Typos. return -> returns. [ci skip] --- activesupport/lib/active_support/values/time_zone.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support/values') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 3cf82a24b9..a834e526fb 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -346,14 +346,14 @@ module ActiveSupport class << self alias_method :create, :new - # Return a TimeZone instance with the given name, or +nil+ if no + # Returns a TimeZone instance with the given name, or +nil+ if no # such TimeZone instance exists. (This exists to support the use of # this class with the +composed_of+ macro.) def new(name) self[name] end - # Return an array of all TimeZone objects. There are multiple + # Returns an array of all TimeZone objects. There are multiple # TimeZone objects per time zone, in many cases, to make it easier # for users to find their own time zone. def all -- cgit v1.2.3 From 029f24ede99e99b3c988f84b4709add754459fc6 Mon Sep 17 00:00:00 2001 From: Colin Bartlett Date: Sat, 9 Nov 2013 09:23:15 -0500 Subject: Add support for localized date references Ruby's Date class automatically gives us #yesterday, #today, and #tomorrow. And ActiveSupport has a handy Time.zone.today for getting a localized version. But there was no localized version of #yesterday or #tomorrow. Until now. --- activesupport/lib/active_support/values/time_zone.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activesupport/lib/active_support/values') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index b6d9257f00..a22e61d286 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -317,6 +317,16 @@ module ActiveSupport tzinfo.now.to_date end + # Returns the next date in this time zone. + def tomorrow + today + 1 + end + + # Returns the previous date in this time zone. + def yesterday + today - 1 + end + # Adjust the given time to the simultaneous time in the time zone # represented by +self+. Returns a Time.utc() instance -- if you want an # ActiveSupport::TimeWithZone instance, use Time#in_time_zone() instead. -- cgit v1.2.3 From ef28028c45c6b19f161c74ada460c4590f5ef49d Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Fri, 27 Dec 2013 14:56:54 -0300 Subject: Update to Unicode 6.3.0 6.3.0 was released on September 30, 2013. http://unicode-inc.blogspot.com.ar/2013/09/announcing-unicode-standard-version-63.html --- .../lib/active_support/values/unicode_tables.dat | Bin 904483 -> 904640 bytes 1 file changed, 0 insertions(+), 0 deletions(-) (limited to 'activesupport/lib/active_support/values') diff --git a/activesupport/lib/active_support/values/unicode_tables.dat b/activesupport/lib/active_support/values/unicode_tables.dat index 2571faa019..394ee95f4b 100644 Binary files a/activesupport/lib/active_support/values/unicode_tables.dat and b/activesupport/lib/active_support/values/unicode_tables.dat differ -- cgit v1.2.3 From 75ad0e642c69cd037ce02172ce68e664634389ed Mon Sep 17 00:00:00 2001 From: Andrew White Date: Wed, 1 Jan 2014 20:29:56 +0000 Subject: Don't lazy load the tzinfo library Lazy loading the tzinfo library doesn't really buy us anything because the gem is installed as a dependency via the gemspec and if a developer is using Active Support outside of Rails then they can cherry pick which files to load anyway. Fixes #13553 --- .../lib/active_support/values/time_zone.rb | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) (limited to 'activesupport/lib/active_support/values') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 3862ab5c42..8ca4973162 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -1,3 +1,4 @@ +require 'tzinfo' require 'thread_safe' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/try' @@ -208,8 +209,6 @@ module ActiveSupport # (GMT). Seconds were chosen as the offset unit because that is the unit # that Ruby uses to represent time zone offsets (see Time#utc_offset). def initialize(name, utc_offset = nil, tzinfo = nil) - self.class.send(:require_tzinfo) - @name = name @utc_offset = utc_offset @tzinfo = tzinfo || TimeZone.find_tzinfo(name) @@ -389,7 +388,7 @@ module ActiveSupport case arg when String begin - lazy_zones_map[arg] ||= create(arg).tap { |tz| tz.utc_offset } + @lazy_zones_map[arg] ||= create(arg).tap { |tz| tz.utc_offset } rescue TZInfo::InvalidTimezoneIdentifier nil end @@ -406,22 +405,6 @@ module ActiveSupport def us_zones @us_zones ||= all.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ } end - - protected - - def require_tzinfo - require 'tzinfo' unless defined?(::TZInfo) - rescue LoadError - $stderr.puts "You don't have tzinfo installed in your application. Please add it to your Gemfile and run bundle install" - raise - end - - private - - def lazy_zones_map - require_tzinfo - @lazy_zones_map - end end private -- cgit v1.2.3 From b0acc77edced44e47c8570bf7dddd4ce19f06cb0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 9 Jan 2014 14:12:05 -0800 Subject: Check `respond_to` before delegation due to: https://github.com/ruby/ruby/commit/d781caaf313b8649948c107bba277e5ad7307314 --- activesupport/lib/active_support/values/time_zone.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activesupport/lib/active_support/values') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 8ca4973162..beaac42fa1 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -234,6 +234,7 @@ module ActiveSupport # Compare this time zone to the parameter. The two are compared first on # their offsets, and then by name. def <=>(zone) + return unless zone.respond_to? :utc_offset result = (utc_offset <=> zone.utc_offset) result = (name <=> zone.name) if result == 0 result -- cgit v1.2.3