debian-mirror-gitlab/app/helpers/time_zone_helper.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

49 lines
1.3 KiB
Ruby
Raw Normal View History

2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
module TimeZoneHelper
TIME_ZONE_FORMAT_ATTRS = {
short: %i[identifier name offset],
2022-01-26 12:08:38 +05:30
abbr: %i[identifier abbr],
2021-02-22 17:27:13 +05:30
full: %i[identifier name abbr offset formatted_offset]
}.freeze
private_constant :TIME_ZONE_FORMAT_ATTRS
# format:
# * :full - all available fields
# * :short (default)
#
# Example:
# timezone_data # :short by default
# timezone_data(format: :full)
#
def timezone_data(format: :short)
attrs = TIME_ZONE_FORMAT_ATTRS.fetch(format) do
valid_formats = TIME_ZONE_FORMAT_ATTRS.keys.map { |k| ":#{k}"}.join(", ")
2021-06-08 01:23:25 +05:30
raise ArgumentError, "Invalid format :#{format}. Valid formats are #{valid_formats}."
2021-02-22 17:27:13 +05:30
end
ActiveSupport::TimeZone.all.map do |timezone|
{
identifier: timezone.tzinfo.identifier,
name: timezone.name,
abbr: timezone.tzinfo.strftime('%Z'),
offset: timezone.now.utc_offset,
formatted_offset: timezone.now.formatted_offset
}.slice(*attrs)
end
end
2021-10-27 15:23:28 +05:30
2022-01-26 12:08:38 +05:30
def local_timezone_instance(timezone)
2021-12-11 22:18:48 +05:30
return Time.zone if timezone.blank?
ActiveSupport::TimeZone.new(timezone) || Time.zone
end
2021-10-27 15:23:28 +05:30
def local_time(timezone)
2021-11-18 22:05:49 +05:30
return if timezone.blank?
2022-01-26 12:08:38 +05:30
time_zone_instance = local_timezone_instance(timezone)
2021-10-27 15:23:28 +05:30
time_zone_instance.now.strftime("%-l:%M %p")
end
2021-02-22 17:27:13 +05:30
end