Skip to content

Instantly share code, notes, and snippets.

@PedroAugustoRamalhoDuarte
Last active April 28, 2024 13:46
Show Gist options
  • Save PedroAugustoRamalhoDuarte/d911b9e2f200f86f53ac57a116d6d545 to your computer and use it in GitHub Desktop.
Save PedroAugustoRamalhoDuarte/d911b9e2f200f86f53ac57a116d6d545 to your computer and use it in GitHub Desktop.
ActiveRecord time field issue with timezone
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails", "7.1.1"
gem "pg", '1.5.4'
end
require "rails"
require "active_record/railtie"
require "minitest/autorun"
require "logger"
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "postgres", username: "postgres", password: "postgres", host: "localhost")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: :cascade do |t|
t.time :start_time
t.time :end_time
end
end
class Post < ActiveRecord::Base
end
class Application < Rails::Application
config.load_defaults 7.1
config.time_zone = "Brasilia"
end
class BugTest < Minitest::Test
def test_timzezone_aware_attributes
ENV["TZ"] = "UTC"
post = Post.new(start_time: "12:00", end_time: "22:00")
puts Rails.application.config.time_zone, Time.now
puts post.inspect
assert_operator post.end_time, :>, post.start_time
post.save!
post.reload
puts post.inspect
assert_operator post.end_time, :>, post.start_time
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment