Skip to content

Commit

Permalink
Merge pull request #96 from robacarp/timestamp_consistency
Browse files Browse the repository at this point in the history
forces timestamps to be in UTC
  • Loading branch information
drujensen authored Nov 18, 2017
2 parents 84fa352 + dfc64a2 commit 1c9ce41
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
46 changes: 46 additions & 0 deletions spec/adapter/mysql_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,50 @@ describe Granite::Adapter::Mysql do
end
end
end

describe "timestamps" do
it "uses UTC for created_at" do
role = Owner.new(name: "test").tap(&.save)
same_role = Owner.find(role.id).not_nil!

original_timestamp = role.created_at.not_nil!
read_timestamp = same_role.created_at.not_nil!

original_timestamp.kind.should eq Time::Kind::Utc
read_timestamp.kind.should eq Time::Kind::Unspecified
end

it "uses UTC for updated_at" do
role = Owner.new(name: "test").tap(&.save)
same_role = Owner.find(role.id).not_nil!

original_timestamp = role.updated_at.not_nil!
read_timestamp = same_role.updated_at.not_nil!

original_timestamp.kind.should eq Time::Kind::Utc
read_timestamp.kind.should eq Time::Kind::Unspecified
end

it "truncates the subsecond parts of created_at" do
role = Owner.new(name: "test").tap(&.save)
same_role = Owner.find(role.id).not_nil!

original_timestamp = role.created_at.not_nil!
read_timestamp = same_role.created_at.not_nil!
hacked_timestamp = Time.new(read_timestamp.ticks, kind: Time::Kind::Utc)

original_timestamp.epoch_f.to_i.should eq hacked_timestamp.epoch
end

it "truncates the subsecond parts of updated_at" do
role = Owner.new(name: "test").tap(&.save)

original_timestamp = role.updated_at.not_nil!
same_role = Owner.find(role.id).not_nil!
read_timestamp = same_role.updated_at.not_nil!
hacked_timestamp = Time.new(read_timestamp.ticks, kind: Time::Kind::Utc)

original_timestamp.epoch_f.to_i.should eq hacked_timestamp.epoch
end
end
end
44 changes: 44 additions & 0 deletions spec/adapter/pg_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,48 @@ describe Granite::Adapter::Pg do
end
end
end

describe "timestamps" do
it "consistently uses UTC for created_at" do
role = Parent.new(name: "test").tap(&.save)
same_role = Parent.find(role.id).not_nil!

original_timestamp = role.created_at.not_nil!
read_timestamp = same_role.created_at.not_nil!

original_timestamp.kind.should eq Time::Kind::Utc
read_timestamp.kind.should eq Time::Kind::Utc
end

it "consistently uses UTC for updated_at" do
role = Parent.new(name: "test").tap(&.save)
same_role = Parent.find(role.id).not_nil!

original_timestamp = role.updated_at.not_nil!
read_timestamp = same_role.updated_at.not_nil!

original_timestamp.kind.should eq Time::Kind::Utc
read_timestamp.kind.should eq Time::Kind::Utc
end

it "truncates the subsecond parts of created_at" do
role = Parent.new(name: "test").tap(&.save)
same_role = Parent.find(role.id).not_nil!

original_timestamp = role.created_at.not_nil!
read_timestamp = same_role.created_at.not_nil!

original_timestamp.epoch_f.to_i.should eq read_timestamp.epoch
end

it "truncates the subsecond parts of updated_at" do
role = Parent.new(name: "test").tap(&.save)
same_role = Parent.find(role.id).not_nil!

original_timestamp = role.updated_at.not_nil!
read_timestamp = same_role.updated_at.not_nil!

original_timestamp.epoch_f.to_i.should eq read_timestamp.epoch
end
end
end
9 changes: 6 additions & 3 deletions src/granite_orm/transactions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module Granite::ORM::Transactions
{% primary_name = PRIMARY[:name] %}
{% primary_type = PRIMARY[:type] %}

@updated_at : Time?
@created_at : Time?

# The save method will check to see if the primary exists yet. If it does it
# will call the update method, otherwise it will call the create method.
# This will update the timestamps apropriately.
Expand All @@ -11,15 +14,15 @@ module Granite::ORM::Transactions
__run_before_save
if value = @{{primary_name}}
__run_before_update
@updated_at = Time.now
@updated_at = Time.now.to_utc
params_and_pk = params
params_and_pk << value
@@adapter.update @@table_name, @@primary_name, self.class.fields, params_and_pk
__run_after_update
else
__run_before_create
@created_at = Time.now
@updated_at = Time.now
@created_at = Time.now.to_utc
@updated_at = Time.now.to_utc
{% if primary_type.id == "Int32" %}
@{{primary_name}} = @@adapter.insert(@@table_name, self.class.fields, params).to_i32
{% else %}
Expand Down

0 comments on commit 1c9ce41

Please sign in to comment.