2017-08-14 13:08:09 -04:00
# frozen_string_literal: true
2016-08-06 13:15:47 -04:00
desc " Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie sessions). "
2007-12-16 18:10:48 -05:00
task :secret do
2016-08-06 13:15:47 -04:00
require " securerandom "
2011-05-23 07:02:06 -04:00
puts SecureRandom . hex ( 64 )
2007-12-14 21:27:56 -05:00
end
2008-03-21 21:59:09 -04:00
2016-08-06 13:15:47 -04:00
desc " List versions of all Rails frameworks and the environment "
2012-10-14 06:03:39 -04:00
task about : :environment do
2010-02-02 19:03:47 -05:00
puts Rails :: Info
end
2008-03-21 21:59:09 -04:00
namespace :time do
2019-01-22 03:53:47 -05:00
desc " List all time zones, list by two-letter country code (`bin/rails time:zones[US]`), or list by UTC offset (`bin/rails time:zones[-8]`) "
2016-04-20 01:03:29 -04:00
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
2008-03-21 21:59:09 -04:00
namespace :zones do
2016-04-20 01:03:29 -04:00
# desc 'Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter, e.g., OFFSET=-6'
2008-03-27 21:45:04 -04:00
task :all do
2016-04-20 01:03:29 -04:00
build_time_zone_list ActiveSupport :: TimeZone . all
2008-03-21 21:59:09 -04:00
end
2010-03-20 13:34:21 -04:00
2010-06-09 16:19:03 -04:00
# 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'
2008-03-27 21:45:04 -04:00
task :us do
2016-04-20 01:03:29 -04:00
build_time_zone_list ActiveSupport :: TimeZone . us_zones
2008-03-21 21:59:09 -04:00
end
2010-03-20 13:34:21 -04:00
2010-06-09 16:19:03 -04:00
# desc 'Displays names of time zones recognized by the Rails TimeZone class with the same offset as the system local time'
2008-03-27 21:45:04 -04:00
task :local do
2016-08-06 13:15:47 -04:00
require " active_support "
require " active_support/time "
2016-04-20 01:03:29 -04:00
2008-04-12 17:06:29 -04:00
jan_offset = Time . now . beginning_of_year . utc_offset
2012-10-14 06:03:39 -04:00
jul_offset = Time . now . beginning_of_year . change ( month : 7 ) . utc_offset
2008-04-12 17:06:29 -04:00
offset = jan_offset < jul_offset ? jan_offset : jul_offset
2016-04-20 01:03:29 -04:00
build_time_zone_list ( ActiveSupport :: TimeZone . all , offset )
2008-03-21 21:59:09 -04:00
end
2010-03-20 13:34:21 -04:00
2008-03-21 21:59:09 -04:00
# to find UTC -06:00 zones, OFFSET can be set to either -6, -6:00 or 21600
2016-08-06 13:15:47 -04:00
def build_time_zone_list ( zones , offset = ENV [ " OFFSET " ] )
require " active_support "
require " active_support/time "
2008-03-21 21:59:09 -04:00
if offset
offset = if offset . to_s . match ( / ( \ +|-)?( \ d+):( \ d+) / )
2016-08-06 13:15:47 -04:00
sign = $1 == " - " ? - 1 : 1
2008-03-21 21:59:09 -04:00
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
2016-04-20 01:03:29 -04:00
zones . each do | zone |
2008-03-21 21:59:09 -04:00
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
2008-08-27 06:42:08 -04:00
end