Skip to content

Commit

Permalink
Stop caching the local tzone
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed May 4, 2018
1 parent 5dd4c32 commit 46c7f27
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 45 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## et-orbi 1.1.1 not yet released

- Stop caching the local tzone, cache the tools used for determining it


## et-orbi 1.1.0 released 2018-03-25

Expand Down
75 changes: 75 additions & 0 deletions bak/bench.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

require 'benchmark'

require 'tzinfo'
require 'et-orbi'

n = 10_000

p ENV['TZ']
p EtOrbi.determine_local_tzone
p EtOrbi.send(:determine_local_tzones)

A = [
'ENV["TZ"]',
proc do
ENV['TZ']
end ]

B = [
'EtOrbi.os_tz',
proc do
EtOrbi.os_tz
end ]

C = [
'TZInfo::Timezone.get("xxx")',
proc do
::TZInfo::Timezone.get('America/Los_Angeles')
end ]

D = [
'EtOrbi.determine_local_tzone',
proc do
EtOrbi.determine_local_tzone
end ]

E = [
'TZInfo::Timezone.all',
proc do
#::TZInfo::Timezone.all
end ]

puts
puts "Ruby #{RUBY_VERSION}p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
puts `uname -a`
puts
puts "the smaller the better"
puts

2.times do

Benchmark.bmbm do |bm|
bm.report(A[0]) { n.times(&A[1]) }
bm.report(B[0]) { n.times(&B[1]) }
bm.report(C[0]) { n.times(&C[1]) }
bm.report(D[0]) { n.times(&D[1]) }
bm.report(E[0]) { n.times(&E[1]) }
end

puts
end

## Iterations Per Second Testing
## gem install benchmark-ips
#require 'benchmark/ips'
#
#Benchmark.ips do |ips|
# ips.report('Version A') do
# # Code to benchmark
# end
#
# ips.report('Version B') do
# # Code to benchmark
# end
#end
56 changes: 19 additions & 37 deletions lib/et-orbi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def parse(str, opts={})
opts[:zone] ||
str_zone ||
find_olson_zone(str) ||
local_tzone
determine_local_tzone
#p [ :parse, :zone, zone ]

str = str.sub(zone.name, '') unless zone.name.match(/\A[-+]/)
Expand Down Expand Up @@ -143,7 +143,7 @@ def get_tzone(o)

return o if o.is_a?(::TZInfo::Timezone)
return nil if o == nil
return local_tzone if o == :local
return determine_local_tzone if o == :local
return ::TZInfo::Timezone.get('Zulu') if o == 'Z'
return o.tzinfo if o.respond_to?(:tzinfo)

Expand All @@ -156,35 +156,17 @@ def get_tzone(o)
(::TZInfo::Timezone.get(o) rescue nil)
end

def local_tzone

@local_tzone_tz ||= nil
@local_tzone_loaded_at ||= nil

@local_tzone = nil \
if @local_tzone_loaded_at && (Time.now > @local_tzone_loaded_at + 1800)
@local_tzone = nil \
if @local_tzone_tz != ENV['TZ']

@local_tzone ||=
begin
@local_tzone_tz = ENV['TZ']
@local_tzone_loaded_at = Time.now
determine_local_tzone
end
end

def render_nozone_time(seconds)

t =
Time.utc(0) + seconds
ts =
t.strftime('%Y-%m-%d %H:%M:%S') +
".#{(seconds % 1).to_s.split('.').last}"
tz =
EtOrbi.determine_local_tzone
z =
EtOrbi.local_tzone ?
EtOrbi.local_tzone.period_for_local(t).abbreviation.to_s :
nil
tz ? tz.period_for_local(t).abbreviation.to_s : nil

"(secs:#{seconds},utc~:#{ts.inspect},ltz~:#{z.inspect})"
end
Expand Down Expand Up @@ -240,7 +222,7 @@ def get_local_tzone(t)

l = Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec, t.usec)

t.zone == l.zone ? local_tzone : nil
(t.zone == l.zone) ? determine_local_tzone : nil
end

def get_as_tzone(t)
Expand Down Expand Up @@ -282,7 +264,7 @@ def get_tzone(o)

def local_tzone

EtOrbi.local_tzone
EtOrbi.determine_local_tzone
end

def platform_info
Expand All @@ -302,7 +284,7 @@ def utc(*a)

def local(*a)

EtOrbi.make_from_array(a, EtOrbi.local_tzone)
EtOrbi.make_from_array(a, local_tzone)
end
end

Expand Down Expand Up @@ -651,7 +633,7 @@ def determine_local_tzone

def os_tz

debian_tz || centos_tz || osx_tz
@os_tz ||= (debian_tz || centos_tz || osx_tz)
end

#
Expand Down Expand Up @@ -707,7 +689,6 @@ def create_offset_tzone(utc_off, id)
tzi.offset(id, utc_off, 0, id)
tzi.create_timezone
end

end

def determine_local_tzones
Expand All @@ -716,23 +697,24 @@ def determine_local_tzones
.collect { |i| (Time.now + i * 30 * 24 * 3600).zone }
.uniq
.sort
.join('|')

t = Time.now
#tu = t.dup.utc # /!\ dup is necessary, #utc modifies its target

twin = Time.utc(t.year, 1, 1) # winter
tsum = Time.utc(t.year, 7, 1) # summer

::TZInfo::Timezone.all.select do |tz|
@tz_all ||= ::TZInfo::Timezone.all
@tz_winter_summer ||= {}

pabbs =
[
tz.period_for_utc(twin).abbreviation.to_s,
tz.period_for_utc(tsum).abbreviation.to_s
].uniq.sort

pabbs == tabbs
end
@tz_winter_summer[tabbs] ||= @tz_all
.select { |tz|
tabbs ==
[
tz.period_for_utc(twin).abbreviation.to_s,
tz.period_for_utc(tsum).abbreviation.to_s
].uniq.sort.join('|') }
end

#
Expand Down
16 changes: 8 additions & 8 deletions spec/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def <=>(tz)
end
end

describe '.local_tzone' do
describe '.determine_local_tzone' do

after :each do

Expand All @@ -320,13 +320,13 @@ class << self
end rescue nil
end

it 'caches and returns the local timezone' do
it 'returns the local timezone' do

in_zone('Europe/Berlin') do
expect(EtOrbi.local_tzone.name).to eq('Europe/Berlin')
expect(EtOrbi.determine_local_tzone.name).to eq('Europe/Berlin')
end
in_zone('America/Jamaica') do
expect(EtOrbi.local_tzone.name).to eq('America/Jamaica')
expect(EtOrbi.determine_local_tzone.name).to eq('America/Jamaica')
end
end

Expand All @@ -344,12 +344,12 @@ def zone
end

in_zone(:no_env_tz) do
expect(EtOrbi.local_tzone.class).to eq(::TZInfo::DataTimezone)
expect(EtOrbi.local_tzone.name).to eq('Europe/Vilnius')
expect(EtOrbi.determine_local_tzone.class).to eq(::TZInfo::DataTimezone)
expect(EtOrbi.determine_local_tzone.name).to eq('Europe/Vilnius')
end
in_zone('Asia/Tehran') do
expect(EtOrbi.local_tzone.class).to eq(::TZInfo::DataTimezone)
expect(EtOrbi.local_tzone.name).to eq('Asia/Tehran')
expect(EtOrbi.determine_local_tzone.class).to eq(::TZInfo::DataTimezone)
expect(EtOrbi.determine_local_tzone.name).to eq('Asia/Tehran')
end
end
end
Expand Down

0 comments on commit 46c7f27

Please sign in to comment.