Skip to content

Commit

Permalink
Merge pull request #381 from alphagov/set-time-zone-everywhere
Browse files Browse the repository at this point in the history
Set time_zone to London in all GOV.UK apps
  • Loading branch information
richardTowers authored Jun 24, 2024
2 parents e6d6181 + e804b35 commit d7fa01e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Adds the basics of a GOV.UK application:
- Statsd client for reporting stats (deprecated; use Prometheus instead)
- Rails logging
- Content Security Policy generation for frontend apps
- Sets the time zone to London

## Installation

Expand Down Expand Up @@ -181,6 +182,9 @@ GovukContentSecurityPolicy.configure

Some frontend apps support languages that are not defined in the i18n gem. This provides them with our own custom rules for these languages.

## Time zone

This gem sets `config.time_zone` to `"London"` - this cannot currently be overridden in application config.

## License

Expand Down
1 change: 1 addition & 0 deletions lib/govuk_app_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
if defined?(Rails)
require "govuk_app_config/govuk_json_logging"
require "govuk_app_config/govuk_content_security_policy"
require "govuk_app_config/govuk_timezone"
require "govuk_app_config/railtie"
end
14 changes: 14 additions & 0 deletions lib/govuk_app_config/govuk_timezone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module GovukTimezone
def self.configure(config)
case config.time_zone
when "UTC"
Rails.logger.info "govuk_app_config changing time_zone from UTC (the default) to London"
when "London"
Rails.logger.info "govuk_app_config always sets time_zone to London - there is no need to set config.time_zone in your app"
else
raise "govuk_app_config prevents configuring time_zones other than London - config.time_zone was set to #{config.time_zone}"
end

config.time_zone = "London"
end
end
4 changes: 4 additions & 0 deletions lib/govuk_app_config/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Railtie < Rails::Railtie
end
end

initializer "govuk_app_config.configure_timezone", before: "active_support.initialize_time_zone" do |app|
GovukTimezone.configure(app.config)
end

config.before_initialize do
GovukJsonLogging.configure if ENV["GOVUK_RAILS_JSON_LOGGING"]
end
Expand Down
32 changes: 32 additions & 0 deletions spec/lib/govuk_timezone_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "spec_helper"
require "govuk_app_config/govuk_timezone"

RSpec.describe GovukError do
describe ".configure" do
let(:config) { Rails::Railtie::Configuration.new }
let(:logger) { instance_double("ActiveSupport::Logger") }

before do
allow(Rails).to receive(:logger).and_return(logger)
end

it "should override the default UTC time_zone to London" do
config.time_zone = "UTC"
expect(logger).to receive(:info)
GovukTimezone.configure(config)
expect(config.time_zone).to eq("London")
end

it "should leave time_zones set to London as London" do
config.time_zone = "London"
expect(logger).to receive(:info)
GovukTimezone.configure(config)
expect(config.time_zone).to eq("London")
end

it "should raise an error if configured with any other time zone" do
config.time_zone = "Shanghai"
expect { GovukTimezone.configure(config) }.to raise_error(/govuk_app_config prevents configuring time_zones/)
end
end
end

0 comments on commit d7fa01e

Please sign in to comment.