Skip to content

Commit

Permalink
Merge pull request #392 from alphagov/better-timezone-config
Browse files Browse the repository at this point in the history
Configure time_zone with govuk_time_zone
  • Loading branch information
richardTowers authored Jul 24, 2024
2 parents 50314ba + a6df713 commit 4232300
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 9.14.0

* Configure `time_zone` with `govuk_time_zone` [#392](/~https://github.com/alphagov/govuk_app_config/pull/392)

# 9.13.1

* Update dependencies
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,17 @@ Some frontend apps support languages that are not defined in the i18n gem. This

## Time zone

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

If you require a different time zone, you can set it with `config.govuk_time_zone`:

```ruby
config.govuk_time_zone = "UTC"
```

Note that we've introduced a new config field (`govuk_time_zone`) here, because
it's otherwise not possible to distinguish between an app using UTC as the
default and an app explicitly asking for UTC.

## License

Expand Down
14 changes: 6 additions & 8 deletions lib/govuk_app_config/govuk_timezone.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
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"
raise "govuk_app_config prevents configuring time_zone with config.time_zone - use config.govuk_time_zone instead" unless config.time_zone == "UTC"

if config.respond_to?(:govuk_time_zone) && config.govuk_time_zone.present?
config.time_zone = config.govuk_time_zone
else
raise "govuk_app_config prevents configuring time_zones other than London - config.time_zone was set to #{config.time_zone}"
Rails.logger.info 'govuk_app_config changing time_zone from UTC (the rails default) to London (the GOV.UK default). Set config.govuk_time_zone = "UTC" if you need UTC.'
config.time_zone = "London"
end

config.time_zone = "London"
end
end
2 changes: 1 addition & 1 deletion lib/govuk_app_config/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GovukAppConfig
VERSION = "9.13.1".freeze
VERSION = "9.14.0".freeze
end
21 changes: 16 additions & 5 deletions spec/lib/govuk_timezone_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require "ostruct"
require "spec_helper"
require "govuk_app_config/govuk_timezone"

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

before do
Expand All @@ -17,16 +18,26 @@
expect(config.time_zone).to eq("London")
end

it "should leave time_zones set to London as London" do
config.time_zone = "London"
it "should allow apps to set time_zone explicitly with config.govuk_time_zone" do
config.time_zone = "UTC"
config.govuk_time_zone = "Shanghai"
GovukTimezone.configure(config)
expect(config.time_zone).to eq("Shanghai")
end

it "should default to London if config.govuk_time_zone is nil" do
config.time_zone = "UTC"
config.govuk_time_zone = nil
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
it "should raise an error if config.time_zone is set to anything other than the default UTC" do
config.time_zone = "London"
expect { GovukTimezone.configure(config) }.to raise_error(/govuk_app_config prevents configuring time_zone with config[.]time_zone/)
config.time_zone = "Shanghai"
expect { GovukTimezone.configure(config) }.to raise_error(/govuk_app_config prevents configuring time_zones/)
expect { GovukTimezone.configure(config) }.to raise_error(/govuk_app_config prevents configuring time_zone with config[.]time_zone/)
end
end
end

0 comments on commit 4232300

Please sign in to comment.