2

Rails 6.1.4.1

I want to have all my components using the same timezone

The dockerfile of my web services shows this:

FROM ruby:2.7.4
...
ENV TZ=Europe/Berlin
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
...

My docker-compose shows this:

...
services:
  web:
    environment:
      - TZ=Europe/Berlin

  pg:
    image: postgres:9.5-alpine
    environment:
      - TZ=Europe/Berlin
      - PGTZ=Europe/Berlin
...

My config environment shows this:

...
config.time_zone = 'Berlin' # Your local time zone
config.active_record.default_timezone = :local
...

Then I can check the timezone on each layer.

Inside my rails container via docker compose exec

root@a69e9bbcc14c:/appuser# cat /etc/timezone
Europe/Berlin
root@a69e9bbcc14c:/appuser# date +"%Z %z"
CEST +0200

looks fine :-)

Inside my pg instance via docker compose exec

psql (9.5.16)
Type "help" for help.

development=# show timezone;
   TimeZone    
---------------
 Europe/Berlin
(1 row)

looks fine :-)

Inside my rails console via docker exec on my rails container (the same rails container above)

Loading development environment (Rails 6.1.4.1)
irb(main):003:0> Rails.application.config.time_zone
=> "Berlin"

irb(main):005:0> Rails.application.config.active_record.default_timezone
=> :local

looks fine :-)

irb(main):004:0> puts Time.current.inspect
Tue, 26 Oct 2021 19:43:33.355250213 CEST +02:00

looks fine :-)

irb(main):001:0> Model.first.start_time
  Model Load (4.6ms)  SELECT "models".* FROM "models" ORDER BY "models"."id" ASC LIMIT $1  [["LIMIT", 1]]
=> 2020-09-24 00:00:00 +0200

looks fine :-)

However

irb(main):002:0> ActiveRecord::Base.connection.execute("show timezone;").first
   (0.5ms)  show timezone;
=> {"TimeZone"=>"UTC"}

How does it look like? Is this correct? Am I doing the wrong configuration or the wrong test? Am I missing something?

I really expect to have {"TimeZone"=>"Europe/Berlin"} for the last check or something equivalent.

Montells
  • 6,389
  • 4
  • 48
  • 53

0 Answers0