From fe3b440fa3c1269376de7e03b394e2c777382476 Mon Sep 17 00:00:00 2001 From: Manpreet Date: Tue, 8 Aug 2017 21:25:37 -0400 Subject: [PATCH 1/2] matches the timezone --- lib/protokoll/counter.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/protokoll/counter.rb b/lib/protokoll/counter.rb index 372e26c..f87d7b2 100644 --- a/lib/protokoll/counter.rb +++ b/lib/protokoll/counter.rb @@ -4,7 +4,6 @@ module Protokoll class Counter def self.next(object, options) element = Models::CustomAutoIncrement.find_or_create_by(build_attrs(object, options)) - element.counter = options[:start] if outdated?(element, options) || element.counter == 0 element.counter += 1 @@ -30,7 +29,7 @@ def self.build_attrs(object, options) end def self.outdated?(record, options) - Time.now.utc.strftime(update_event(options)).to_i > record.updated_at.strftime(update_event(options)).to_i + Time.now.utc.strftime(update_event(options)).to_i > record.updated_at.utc.strftime(update_event(options)).to_i end def self.update_event(options) From 2a3ccffa105d40212b0425b700ef70d008e1fb4e Mon Sep 17 00:00:00 2001 From: Manpreet Date: Tue, 8 Aug 2017 22:51:39 -0400 Subject: [PATCH 2/2] added test for timezone issue, issue# 33 - https://github.com/celsodantas/protokoll/issues/33 --- lib/protokoll/counter.rb | 2 +- test/protokoll_test.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/protokoll/counter.rb b/lib/protokoll/counter.rb index f87d7b2..82a0872 100644 --- a/lib/protokoll/counter.rb +++ b/lib/protokoll/counter.rb @@ -29,7 +29,7 @@ def self.build_attrs(object, options) end def self.outdated?(record, options) - Time.now.utc.strftime(update_event(options)).to_i > record.updated_at.utc.strftime(update_event(options)).to_i + Time.zone.now.strftime(update_event(options)).to_i > record.updated_at.strftime(update_event(options)).to_i end def self.update_event(options) diff --git a/test/protokoll_test.rb b/test/protokoll_test.rb index 858674d..bc6bb4a 100644 --- a/test/protokoll_test.rb +++ b/test/protokoll_test.rb @@ -49,6 +49,40 @@ class Protocol < ActiveRecord::Base assert_equal "20110900002", protocol2.number end + test "timezone should not be a problem" do + class Protocol < ActiveRecord::Base + protokoll :number, pattern: "%y%m%d##" + end + + Time.zone = 'America/New_York' + time = Time.local(2011, 9, 25, 9, 0, 0) + Timecop.travel(time) + protocol1 = Protocol.create + + time = Time.local(2011, 9, 25, 22, 0, 0) + Timecop.travel(time) + protocol2 = Protocol.create + + assert_not_equal protocol1.number, protocol2.number + end + + test "timezone should not be a problem when the time is next day" do + class Protocol < ActiveRecord::Base + protokoll :number, pattern: "%y%m%d##" + end + + Time.zone = 'America/New_York' + time = Time.local(2011, 9, 25, 9, 0, 0) + Timecop.travel(time) + protocol1 = Protocol.create + + time = Time.local(2011, 9, 26, 02, 0, 0) + Timecop.travel(time) + protocol2 = Protocol.create + + assert_not_equal protocol1.number, protocol2.number + end + test "should get 201100002 for second save (format number %Y#####)" do class Protocol < ActiveRecord::Base protokoll :number, :pattern => "%Y#####"