Skip to content

Commit

Permalink
Set timezone from initializer Ruby DSL (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushn21 authored Jan 25, 2023
1 parent 953fd3a commit 7a1f6ef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ def with_unbundled_env(&block)
def set_timezone(timezone)
ENV["TZ"] = timezone
end

# Get the current TZ environment variable
#
# @return [String]
def timezone
ENV["TZ"]
end

# rubocop:enable Naming/AccessorMethodName

# Fetch the logger instance for this Bridgetown process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def roda(&block)
@scope.roda_initializers << block
end

def timezone(tz) # rubocop:disable Naming/MethodParameterName
Bridgetown.set_timezone(tz)
end

def method_missing(key, *value, &block) # rubocop:disable Style/MissingRespondToMissing
return get(key) if value.empty? && block.nil?

Expand Down
11 changes: 11 additions & 0 deletions bridgetown-core/test/test_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -543,5 +543,16 @@ def default_config_fixture(overrides = {})

assert @config.init_params.key?("something")
end

should "set the global timezone" do
dsl = Configuration::ConfigurationDSL.new(scope: @config, data: @config)

dsl.instance_variable_set(:@context, :testing)
dsl.instance_exec(dsl) do
timezone "GMT"
end

assert_equal "GMT", Bridgetown.timezone
end
end
end

0 comments on commit 7a1f6ef

Please sign in to comment.