aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/tasks/misc.rake
blob: 4ac7f60fe977dec4e7c0c40d57ec1bd3182439a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true
desc "Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie sessions)."
task :secret do
  require "securerandom"
  puts SecureRandom.hex(64)
end

desc "List versions of all Rails frameworks and the environment"
task about: :environment do
  puts Rails::Info
end

namespace :time do
  desc "List all time zones, list by two-letter country code (`rails time:zones[US]`), or list by UTC offset (`rails time:zones[-8]`)"
  task :zones, :country_or_offset do |t, args|
    zones, offset = ActiveSupport::TimeZone.all, nil

    if country_or_offset = args[:country_or_offset]
      begin
        zones = ActiveSupport::TimeZone.country_zones(country_or_offset)
      rescue TZInfo::InvalidCountryCode
        offset = country_or_offset
      end
    end

    build_time_zone_list zones, offset
  end

  namespace :zones do
    # desc 'Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter, e.g., OFFSET=-6'
    task :all do
      build_time_zone_list ActiveSupport::TimeZone.all
    end

    # desc 'Displays names of US time zones recognized by the Rails TimeZone class, grouped by offset. Results can be filtered with optional OFFSET parameter, e.g., OFFSET=-6'
    task :us do
      build_time_zone_list ActiveSupport::TimeZone.us_zones
    end

    # desc 'Displays names of time zones recognized by the Rails TimeZone class with the same offset as the system local time'
    task :local do
      require "active_support"
      require "active_support/time"

      jan_offset = Time.now.beginning_of_year.utc_offset
      jul_offset = Time.now.beginning_of_year.change(month: 7).utc_offset
      offset = jan_offset < jul_offset ? jan_offset : jul_offset

      build_time_zone_list(ActiveSupport::TimeZone.all, offset)
    end

    # to find UTC -06:00 zones, OFFSET can be set to either -6, -6:00 or 21600
    def build_time_zone_list(zones, offset = ENV["OFFSET"])
      require "active_support"
      require "active_support/time"
      if offset
        offset = if offset.to_s.match(/(\+|-)?(\d+):(\d+)/)
          sign = $1 == "-" ? -1 : 1
          hours, minutes = $2.to_f, $3.to_f
          ((hours * 3600) + (minutes.to_f * 60)) * sign
        elsif offset.to_f.abs <= 13
          offset.to_f * 3600
        else
          offset.to_f
        end
      end
      previous_offset = nil
      zones.each do |zone|
        if offset.nil? || offset == zone.utc_offset
          puts "\n* UTC #{zone.formatted_offset} *" unless zone.utc_offset == previous_offset
          puts zone.name
          previous_offset = zone.utc_offset
        end
      end
      puts "\n"
    end
  end
end