Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match timezones #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/protokoll/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.zone.now.strftime(update_event(options)).to_i > record.updated_at.strftime(update_event(options)).to_i
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Time.zone.now.strftime(update_event(options)).to_i > record.updated_at.strftime(update_event(options)).to_i
Time.current.strftime(update_event(options)).to_i > record.updated_at.strftime(update_event(options)).to_i

https://apidock.com/rails/Time/current/class

end

def self.update_event(options)
Expand Down
34 changes: 34 additions & 0 deletions test/protokoll_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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#####"
Expand Down