activesupport 3.2.22.5 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +364 -135
- data/MIT-LICENSE +1 -1
- data/README.rdoc +6 -4
- data/lib/active_support/backtrace_cleaner.rb +33 -25
- data/lib/active_support/basic_object.rb +7 -17
- data/lib/active_support/benchmarkable.rb +19 -15
- data/lib/active_support/buffered_logger.rb +9 -113
- data/lib/active_support/cache/file_store.rb +14 -14
- data/lib/active_support/cache/mem_cache_store.rb +24 -30
- data/lib/active_support/cache/memory_store.rb +2 -0
- data/lib/active_support/cache/strategy/local_cache.rb +48 -37
- data/lib/active_support/cache.rb +228 -183
- data/lib/active_support/callbacks.rb +201 -253
- data/lib/active_support/concern.rb +16 -23
- data/lib/active_support/concurrency/latch.rb +27 -0
- data/lib/active_support/configurable.rb +69 -12
- data/lib/active_support/core_ext/array/access.rb +17 -9
- data/lib/active_support/core_ext/array/conversions.rb +115 -55
- data/lib/active_support/core_ext/array/extract_options.rb +2 -2
- data/lib/active_support/core_ext/array/grouping.rb +21 -22
- data/lib/active_support/core_ext/array/uniq_by.rb +12 -9
- data/lib/active_support/core_ext/array/wrap.rb +13 -16
- data/lib/active_support/core_ext/array.rb +0 -1
- data/lib/active_support/core_ext/benchmark.rb +7 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -24
- data/lib/active_support/core_ext/class/attribute.rb +40 -32
- data/lib/active_support/core_ext/class/attribute_accessors.rb +14 -12
- data/lib/active_support/core_ext/class/delegating_attributes.rb +15 -19
- data/lib/active_support/core_ext/class/subclasses.rb +11 -5
- data/lib/active_support/core_ext/date/calculations.rb +44 -187
- data/lib/active_support/core_ext/date/conversions.rb +16 -38
- data/lib/active_support/core_ext/date/zones.rb +25 -2
- data/lib/active_support/core_ext/date.rb +5 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +232 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +0 -1
- data/lib/active_support/core_ext/date_time/calculations.rb +77 -62
- data/lib/active_support/core_ext/date_time/conversions.rb +21 -33
- data/lib/active_support/core_ext/date_time/zones.rb +11 -8
- data/lib/active_support/core_ext/date_time.rb +4 -0
- data/lib/active_support/core_ext/enumerable.rb +26 -73
- data/lib/active_support/core_ext/file/atomic.rb +27 -11
- data/lib/active_support/core_ext/file.rb +0 -1
- data/lib/active_support/core_ext/hash/conversions.rb +145 -79
- data/lib/active_support/core_ext/hash/deep_merge.rb +14 -8
- data/lib/active_support/core_ext/hash/diff.rb +5 -4
- data/lib/active_support/core_ext/hash/except.rb +1 -9
- data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -5
- data/lib/active_support/core_ext/hash/keys.rb +108 -24
- data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
- data/lib/active_support/core_ext/hash/slice.rb +12 -12
- data/lib/active_support/core_ext/hash.rb +0 -1
- data/lib/active_support/core_ext/integer/inflections.rb +13 -1
- data/lib/active_support/core_ext/integer/time.rb +17 -12
- data/lib/active_support/core_ext/kernel/debugger.rb +2 -2
- data/lib/active_support/core_ext/kernel/reporting.rb +36 -22
- data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
- data/lib/active_support/core_ext/load_error.rb +7 -5
- data/lib/active_support/core_ext/logger.rb +7 -23
- data/lib/active_support/core_ext/marshal.rb +21 -0
- data/lib/active_support/core_ext/module/aliasing.rb +8 -9
- data/lib/active_support/core_ext/module/anonymous.rb +2 -7
- data/lib/active_support/core_ext/module/attr_internal.rb +0 -1
- data/lib/active_support/core_ext/module/attribute_accessors.rb +12 -10
- data/lib/active_support/core_ext/module/delegation.rb +92 -49
- data/lib/active_support/core_ext/module/deprecation.rb +19 -3
- data/lib/active_support/core_ext/module/introspection.rb +17 -27
- data/lib/active_support/core_ext/module/qualified_const.rb +8 -20
- data/lib/active_support/core_ext/module/remove_method.rb +1 -5
- data/lib/active_support/core_ext/module.rb +1 -3
- data/lib/active_support/core_ext/numeric/conversions.rb +135 -0
- data/lib/active_support/core_ext/numeric/time.rb +6 -6
- data/lib/active_support/core_ext/numeric.rb +1 -0
- data/lib/active_support/core_ext/object/acts_like.rb +4 -4
- data/lib/active_support/core_ext/object/blank.rb +7 -23
- data/lib/active_support/core_ext/object/deep_dup.rb +46 -0
- data/lib/active_support/core_ext/object/duplicable.rb +1 -30
- data/lib/active_support/core_ext/object/inclusion.rb +8 -7
- data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
- data/lib/active_support/core_ext/object/to_json.rb +8 -0
- data/lib/active_support/core_ext/object/to_param.rb +5 -2
- data/lib/active_support/core_ext/object/try.rb +46 -25
- data/lib/active_support/core_ext/object/with_options.rb +7 -8
- data/lib/active_support/core_ext/object.rb +1 -0
- data/lib/active_support/core_ext/proc.rb +3 -0
- data/lib/active_support/core_ext/range/conversions.rb +0 -2
- data/lib/active_support/core_ext/range/include_range.rb +3 -1
- data/lib/active_support/core_ext/range/overlaps.rb +1 -1
- data/lib/active_support/core_ext/range.rb +0 -2
- data/lib/active_support/core_ext/string/access.rb +95 -90
- data/lib/active_support/core_ext/string/conversions.rb +27 -38
- data/lib/active_support/core_ext/string/encoding.rb +6 -9
- data/lib/active_support/core_ext/string/filters.rb +24 -18
- data/lib/active_support/core_ext/string/indent.rb +43 -0
- data/lib/active_support/core_ext/string/inflections.rb +70 -60
- data/lib/active_support/core_ext/string/inquiry.rb +2 -2
- data/lib/active_support/core_ext/string/multibyte.rb +41 -64
- data/lib/active_support/core_ext/string/output_safety.rb +59 -51
- data/lib/active_support/core_ext/string/zones.rb +13 -0
- data/lib/active_support/core_ext/string.rb +2 -3
- data/lib/active_support/core_ext/struct.rb +6 -0
- data/lib/active_support/core_ext/thread.rb +74 -0
- data/lib/active_support/core_ext/time/calculations.rb +108 -184
- data/lib/active_support/core_ext/time/conversions.rb +27 -51
- data/lib/active_support/core_ext/time/marshal.rb +0 -27
- data/lib/active_support/core_ext/time/zones.rb +27 -17
- data/lib/active_support/core_ext/time.rb +5 -0
- data/lib/active_support/core_ext/uri.rb +13 -17
- data/lib/active_support/core_ext.rb +3 -2
- data/lib/active_support/dependencies/autoload.rb +47 -20
- data/lib/active_support/dependencies.rb +160 -141
- data/lib/active_support/deprecation/behaviors.rb +44 -30
- data/lib/active_support/deprecation/instance_delegator.rb +24 -0
- data/lib/active_support/deprecation/method_wrappers.rb +33 -18
- data/lib/active_support/deprecation/proxy_wrappers.rb +58 -13
- data/lib/active_support/deprecation/reporting.rb +40 -11
- data/lib/active_support/deprecation.rb +39 -14
- data/lib/active_support/descendants_tracker.rb +34 -19
- data/lib/active_support/duration.rb +6 -8
- data/lib/active_support/file_update_checker.rb +63 -47
- data/lib/active_support/gzip.rb +11 -5
- data/lib/active_support/hash_with_indifferent_access.rb +127 -42
- data/lib/active_support/i18n.rb +4 -0
- data/lib/active_support/i18n_railtie.rb +5 -22
- data/lib/active_support/inflections.rb +14 -12
- data/lib/active_support/inflector/inflections.rb +108 -71
- data/lib/active_support/inflector/methods.rb +181 -160
- data/lib/active_support/inflector/transliterate.rb +16 -17
- data/lib/active_support/json/decoding.rb +18 -17
- data/lib/active_support/json/encoding.rb +93 -39
- data/lib/active_support/json/variable.rb +10 -1
- data/lib/active_support/key_generator.rb +75 -0
- data/lib/active_support/lazy_load_hooks.rb +21 -19
- data/lib/active_support/locale/en.yml +100 -3
- data/lib/active_support/log_subscriber/test_helper.rb +18 -15
- data/lib/active_support/log_subscriber.rb +35 -52
- data/lib/active_support/logger.rb +57 -0
- data/lib/active_support/logger_silence.rb +24 -0
- data/lib/active_support/message_encryptor.rb +38 -35
- data/lib/active_support/message_verifier.rb +9 -15
- data/lib/active_support/multibyte/chars.rb +80 -333
- data/lib/active_support/multibyte/unicode.rb +74 -64
- data/lib/active_support/multibyte.rb +5 -28
- data/lib/active_support/notifications/fanout.rb +112 -18
- data/lib/active_support/notifications/instrumenter.rb +33 -14
- data/lib/active_support/notifications.rb +79 -26
- data/lib/active_support/number_helper.rb +637 -0
- data/lib/active_support/ordered_hash.rb +8 -190
- data/lib/active_support/ordered_options.rb +21 -23
- data/lib/active_support/per_thread_registry.rb +52 -0
- data/lib/active_support/proxy_object.rb +13 -0
- data/lib/active_support/rails.rb +27 -0
- data/lib/active_support/railtie.rb +12 -32
- data/lib/active_support/rescuable.rb +9 -4
- data/lib/active_support/string_inquirer.rb +13 -8
- data/lib/active_support/subscriber.rb +93 -0
- data/lib/active_support/tagged_logging.rb +51 -73
- data/lib/active_support/test_case.rb +46 -17
- data/lib/active_support/testing/assertions.rb +56 -26
- data/lib/active_support/testing/autorun.rb +5 -0
- data/lib/active_support/testing/constant_lookup.rb +54 -0
- data/lib/active_support/testing/declarative.rb +1 -1
- data/lib/active_support/testing/deprecation.rb +0 -19
- data/lib/active_support/testing/isolation.rb +29 -58
- data/lib/active_support/testing/pending.rb +5 -43
- data/lib/active_support/testing/setup_and_teardown.rb +6 -92
- data/lib/active_support/testing/tagged_logging.rb +25 -0
- data/lib/active_support/time.rb +6 -21
- data/lib/active_support/time_with_zone.rb +80 -43
- data/lib/active_support/values/time_zone.rb +81 -60
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +7 -6
- data/lib/active_support/xml_mini/jdom.rb +9 -11
- data/lib/active_support/xml_mini/libxml.rb +1 -2
- data/lib/active_support/xml_mini/libxmlsax.rb +2 -3
- data/lib/active_support/xml_mini/nokogiri.rb +1 -2
- data/lib/active_support/xml_mini/nokogirisax.rb +2 -3
- data/lib/active_support/xml_mini/rexml.rb +6 -8
- data/lib/active_support/xml_mini.rb +35 -17
- data/lib/active_support.rb +8 -21
- metadata +104 -72
- data/lib/active_support/base64.rb +0 -54
- data/lib/active_support/core_ext/array/random_access.rb +0 -30
- data/lib/active_support/core_ext/date/freeze.rb +0 -33
- data/lib/active_support/core_ext/exception.rb +0 -3
- data/lib/active_support/core_ext/file/path.rb +0 -5
- data/lib/active_support/core_ext/float/rounding.rb +0 -19
- data/lib/active_support/core_ext/float.rb +0 -1
- data/lib/active_support/core_ext/hash/deep_dup.rb +0 -18
- data/lib/active_support/core_ext/io.rb +0 -15
- data/lib/active_support/core_ext/module/method_names.rb +0 -14
- data/lib/active_support/core_ext/module/synchronization.rb +0 -45
- data/lib/active_support/core_ext/process/daemon.rb +0 -23
- data/lib/active_support/core_ext/process.rb +0 -1
- data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
- data/lib/active_support/core_ext/range/cover.rb +0 -3
- data/lib/active_support/core_ext/rexml.rb +0 -46
- data/lib/active_support/core_ext/string/interpolation.rb +0 -2
- data/lib/active_support/core_ext/string/xchar.rb +0 -18
- data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
- data/lib/active_support/memoizable.rb +0 -116
- data/lib/active_support/multibyte/exceptions.rb +0 -8
- data/lib/active_support/multibyte/utils.rb +0 -60
- data/lib/active_support/ruby/shim.rb +0 -22
- data/lib/active_support/security_utils.rb +0 -27
- data/lib/active_support/testing/mochaing.rb +0 -7
- data/lib/active_support/testing/performance/jruby.rb +0 -115
- data/lib/active_support/testing/performance/rubinius.rb +0 -113
- data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
- data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
- data/lib/active_support/testing/performance/ruby.rb +0 -152
- data/lib/active_support/testing/performance.rb +0 -317
- data/lib/active_support/time/autoload.rb +0 -5
- data/lib/active_support/whiny_nil.rb +0 -24
|
@@ -2,10 +2,13 @@ require 'active_support/duration'
|
|
|
2
2
|
require 'active_support/core_ext/time/conversions'
|
|
3
3
|
require 'active_support/time_with_zone'
|
|
4
4
|
require 'active_support/core_ext/time/zones'
|
|
5
|
+
require 'active_support/core_ext/date_and_time/calculations'
|
|
6
|
+
require 'active_support/deprecation'
|
|
5
7
|
|
|
6
8
|
class Time
|
|
9
|
+
include DateAndTime::Calculations
|
|
10
|
+
|
|
7
11
|
COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
8
|
-
DAYS_INTO_WEEK = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }
|
|
9
12
|
|
|
10
13
|
class << self
|
|
11
14
|
# Overriding case equality method so that it returns true for ActiveSupport::TimeWithZone instances
|
|
@@ -16,28 +19,45 @@ class Time
|
|
|
16
19
|
# Return the number of days in the given month.
|
|
17
20
|
# If no year is specified, it will use the current year.
|
|
18
21
|
def days_in_month(month, year = now.year)
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
if month == 2 && ::Date.gregorian_leap?(year)
|
|
23
|
+
29
|
|
24
|
+
else
|
|
25
|
+
COMMON_YEAR_DAYS_IN_MONTH[month]
|
|
26
|
+
end
|
|
21
27
|
end
|
|
22
28
|
|
|
29
|
+
# *DEPRECATED*: Use +Time#utc+ or +Time#local+ instead.
|
|
30
|
+
#
|
|
23
31
|
# Returns a new Time if requested year can be accommodated by Ruby's Time class
|
|
24
32
|
# (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture);
|
|
25
33
|
# otherwise returns a DateTime.
|
|
26
34
|
def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
|
|
35
|
+
ActiveSupport::Deprecation.warn 'time_with_datetime_fallback is deprecated. Use Time#utc or Time#local instead', caller
|
|
27
36
|
time = ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
|
|
37
|
+
|
|
28
38
|
# This check is needed because Time.utc(y) returns a time object in the 2000s for 0 <= y <= 138.
|
|
29
|
-
time.year == year
|
|
39
|
+
if time.year == year
|
|
40
|
+
time
|
|
41
|
+
else
|
|
42
|
+
::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
|
|
43
|
+
end
|
|
30
44
|
rescue
|
|
31
45
|
::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
|
|
32
46
|
end
|
|
33
47
|
|
|
48
|
+
# *DEPRECATED*: Use +Time#utc+ instead.
|
|
49
|
+
#
|
|
34
50
|
# Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:utc</tt>.
|
|
35
51
|
def utc_time(*args)
|
|
52
|
+
ActiveSupport::Deprecation.warn 'utc_time is deprecated. Use Time#utc instead', caller
|
|
36
53
|
time_with_datetime_fallback(:utc, *args)
|
|
37
54
|
end
|
|
38
55
|
|
|
56
|
+
# *DEPRECATED*: Use +Time#local+ instead.
|
|
57
|
+
#
|
|
39
58
|
# Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:local</tt>.
|
|
40
59
|
def local_time(*args)
|
|
60
|
+
ActiveSupport::Deprecation.warn 'local_time is deprecated. Use Time#local instead', caller
|
|
41
61
|
time_with_datetime_fallback(:local, *args)
|
|
42
62
|
end
|
|
43
63
|
|
|
@@ -49,55 +69,57 @@ class Time
|
|
|
49
69
|
# Layers additional behavior on Time.at so that ActiveSupport::TimeWithZone and DateTime
|
|
50
70
|
# instances can be used when called with a single argument
|
|
51
71
|
def at_with_coercion(*args)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
# Time.at can be called with a time or numerical value
|
|
55
|
-
time_or_number = args.first
|
|
56
|
-
|
|
57
|
-
if time_or_number.is_a?(ActiveSupport::TimeWithZone) || time_or_number.is_a?(DateTime)
|
|
58
|
-
at_without_coercion(time_or_number.to_f).getlocal
|
|
72
|
+
if args.size == 1 && args.first.acts_like?(:time)
|
|
73
|
+
at_without_coercion(args.first.to_i)
|
|
59
74
|
else
|
|
60
|
-
at_without_coercion(
|
|
75
|
+
at_without_coercion(*args)
|
|
61
76
|
end
|
|
62
77
|
end
|
|
63
78
|
alias_method :at_without_coercion, :at
|
|
64
79
|
alias_method :at, :at_with_coercion
|
|
65
80
|
end
|
|
66
81
|
|
|
67
|
-
# Tells whether the Time object's time lies in the past
|
|
68
|
-
def past?
|
|
69
|
-
self < ::Time.current
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
# Tells whether the Time object's time is today
|
|
73
|
-
def today?
|
|
74
|
-
to_date == ::Date.current
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
# Tells whether the Time object's time lies in the future
|
|
78
|
-
def future?
|
|
79
|
-
self > ::Time.current
|
|
80
|
-
end
|
|
81
|
-
|
|
82
82
|
# Seconds since midnight: Time.now.seconds_since_midnight
|
|
83
83
|
def seconds_since_midnight
|
|
84
84
|
to_i - change(:hour => 0).to_i + (usec / 1.0e+6)
|
|
85
85
|
end
|
|
86
86
|
|
|
87
|
-
# Returns
|
|
88
|
-
#
|
|
89
|
-
#
|
|
87
|
+
# Returns the number of seconds until 23:59:59.
|
|
88
|
+
#
|
|
89
|
+
# Time.new(2012, 8, 29, 0, 0, 0).seconds_until_end_of_day # => 86399
|
|
90
|
+
# Time.new(2012, 8, 29, 12, 34, 56).seconds_until_end_of_day # => 41103
|
|
91
|
+
# Time.new(2012, 8, 29, 23, 59, 59).seconds_until_end_of_day # => 0
|
|
92
|
+
def seconds_until_end_of_day
|
|
93
|
+
end_of_day.to_i - to_i
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Returns a new Time where one or more of the elements have been changed according
|
|
97
|
+
# to the +options+ parameter. The time options (<tt>:hour</tt>, <tt>:min</tt>,
|
|
98
|
+
# <tt>:sec</tt>, <tt>:usec</tt>) reset cascadingly, so if only the hour is passed,
|
|
99
|
+
# then minute, sec, and usec is set to 0. If the hour and minute is passed, then
|
|
100
|
+
# sec and usec is set to 0. The +options+ parameter takes a hash with any of these
|
|
101
|
+
# keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:min</tt>,
|
|
102
|
+
# <tt>:sec</tt>, <tt>:usec</tt>.
|
|
103
|
+
#
|
|
104
|
+
# Time.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => Time.new(2012, 8, 1, 22, 35, 0)
|
|
105
|
+
# Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => Time.new(1981, 8, 1, 22, 35, 0)
|
|
106
|
+
# Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => Time.new(1981, 8, 29, 0, 0, 0)
|
|
90
107
|
def change(options)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
108
|
+
new_year = options.fetch(:year, year)
|
|
109
|
+
new_month = options.fetch(:month, month)
|
|
110
|
+
new_day = options.fetch(:day, day)
|
|
111
|
+
new_hour = options.fetch(:hour, hour)
|
|
112
|
+
new_min = options.fetch(:min, options[:hour] ? 0 : min)
|
|
113
|
+
new_sec = options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec)
|
|
114
|
+
new_usec = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
|
|
115
|
+
|
|
116
|
+
if utc?
|
|
117
|
+
::Time.utc(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec)
|
|
118
|
+
elsif zone
|
|
119
|
+
::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec)
|
|
120
|
+
else
|
|
121
|
+
::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec + (new_usec.to_r / 1000000), utc_offset)
|
|
122
|
+
end
|
|
101
123
|
end
|
|
102
124
|
|
|
103
125
|
# Uses Date to provide precise Time calculations for years, months, and days.
|
|
@@ -107,18 +129,26 @@ class Time
|
|
|
107
129
|
def advance(options)
|
|
108
130
|
unless options[:weeks].nil?
|
|
109
131
|
options[:weeks], partial_weeks = options[:weeks].divmod(1)
|
|
110
|
-
options[:days] = (
|
|
132
|
+
options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
|
|
111
133
|
end
|
|
112
134
|
|
|
113
135
|
unless options[:days].nil?
|
|
114
136
|
options[:days], partial_days = options[:days].divmod(1)
|
|
115
|
-
options[:hours] = (
|
|
137
|
+
options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
|
|
116
138
|
end
|
|
117
139
|
|
|
118
140
|
d = to_date.advance(options)
|
|
119
141
|
time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
|
|
120
|
-
seconds_to_advance =
|
|
121
|
-
|
|
142
|
+
seconds_to_advance = \
|
|
143
|
+
options.fetch(:seconds, 0) +
|
|
144
|
+
options.fetch(:minutes, 0) * 60 +
|
|
145
|
+
options.fetch(:hours, 0) * 3600
|
|
146
|
+
|
|
147
|
+
if seconds_to_advance.zero?
|
|
148
|
+
time_advanced_by_date
|
|
149
|
+
else
|
|
150
|
+
time_advanced_by_date.since(seconds_to_advance)
|
|
151
|
+
end
|
|
122
152
|
end
|
|
123
153
|
|
|
124
154
|
# Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension
|
|
@@ -134,98 +164,9 @@ class Time
|
|
|
134
164
|
end
|
|
135
165
|
alias :in :since
|
|
136
166
|
|
|
137
|
-
# Returns a new Time representing the time a number of specified weeks ago.
|
|
138
|
-
def weeks_ago(weeks)
|
|
139
|
-
advance(:weeks => -weeks)
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
# Returns a new Time representing the time a number of specified months ago
|
|
143
|
-
def months_ago(months)
|
|
144
|
-
advance(:months => -months)
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
# Returns a new Time representing the time a number of specified months in the future
|
|
148
|
-
def months_since(months)
|
|
149
|
-
advance(:months => months)
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
# Returns a new Time representing the time a number of specified years ago
|
|
153
|
-
def years_ago(years)
|
|
154
|
-
advance(:years => -years)
|
|
155
|
-
end
|
|
156
|
-
|
|
157
|
-
# Returns a new Time representing the time a number of specified years in the future
|
|
158
|
-
def years_since(years)
|
|
159
|
-
advance(:years => years)
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
# Short-hand for years_ago(1)
|
|
163
|
-
def prev_year
|
|
164
|
-
years_ago(1)
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
# Short-hand for years_since(1)
|
|
168
|
-
def next_year
|
|
169
|
-
years_since(1)
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
# Short-hand for months_ago(1)
|
|
173
|
-
def prev_month
|
|
174
|
-
months_ago(1)
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
# Short-hand for months_since(1)
|
|
178
|
-
def next_month
|
|
179
|
-
months_since(1)
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
# Returns number of days to start of this week, week starts on start_day (default is :monday).
|
|
183
|
-
def days_to_week_start(start_day = :monday)
|
|
184
|
-
start_day_number = DAYS_INTO_WEEK[start_day]
|
|
185
|
-
current_day_number = wday != 0 ? wday - 1 : 6
|
|
186
|
-
days_span = current_day_number - start_day_number
|
|
187
|
-
days_span >= 0 ? days_span : 7 + days_span
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
# Returns a new Time representing the "start" of this week, week starts on start_day (default is :monday, i.e. Monday, 0:00).
|
|
191
|
-
def beginning_of_week(start_day = :monday)
|
|
192
|
-
days_to_start = days_to_week_start(start_day)
|
|
193
|
-
(self - days_to_start.days).midnight
|
|
194
|
-
end
|
|
195
|
-
alias :at_beginning_of_week :beginning_of_week
|
|
196
|
-
|
|
197
|
-
# Returns a new +Date+/+DateTime+ representing the start of this week. Week is
|
|
198
|
-
# assumed to start on a Monday. +DateTime+ objects have their time set to 0:00.
|
|
199
|
-
def monday
|
|
200
|
-
beginning_of_week
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
# Returns a new Time representing the end of this week, week starts on start_day (default is :monday, i.e. end of Sunday).
|
|
204
|
-
def end_of_week(start_day = :monday)
|
|
205
|
-
days_to_end = 6 - days_to_week_start(start_day)
|
|
206
|
-
(self + days_to_end.days).end_of_day
|
|
207
|
-
end
|
|
208
|
-
alias :at_end_of_week :end_of_week
|
|
209
|
-
|
|
210
|
-
# Returns a new +Date+/+DateTime+ representing the end of this week. Week is
|
|
211
|
-
# assumed to start on a Monday. +DateTime+ objects have their time set to 23:59:59.
|
|
212
|
-
def sunday
|
|
213
|
-
end_of_week
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
# Returns a new Time representing the start of the given day in the previous week (default is :monday).
|
|
217
|
-
def prev_week(day = :monday)
|
|
218
|
-
ago(1.week).beginning_of_week.since(DAYS_INTO_WEEK[day].day).change(:hour => 0)
|
|
219
|
-
end
|
|
220
|
-
|
|
221
|
-
# Returns a new Time representing the start of the given day in next week (default is :monday).
|
|
222
|
-
def next_week(day = :monday)
|
|
223
|
-
since(1.week).beginning_of_week.since(DAYS_INTO_WEEK[day].day).change(:hour => 0)
|
|
224
|
-
end
|
|
225
|
-
|
|
226
167
|
# Returns a new Time representing the start of the day (0:00)
|
|
227
168
|
def beginning_of_day
|
|
228
|
-
#(self - seconds_since_midnight).change(:
|
|
169
|
+
#(self - seconds_since_midnight).change(usec: 0)
|
|
229
170
|
change(:hour => 0)
|
|
230
171
|
end
|
|
231
172
|
alias :midnight :beginning_of_day
|
|
@@ -234,8 +175,14 @@ class Time
|
|
|
234
175
|
|
|
235
176
|
# Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9)
|
|
236
177
|
def end_of_day
|
|
237
|
-
change(
|
|
178
|
+
change(
|
|
179
|
+
:hour => 23,
|
|
180
|
+
:min => 59,
|
|
181
|
+
:sec => 59,
|
|
182
|
+
:usec => Rational(999999999, 1000)
|
|
183
|
+
)
|
|
238
184
|
end
|
|
185
|
+
alias :at_end_of_day :end_of_day
|
|
239
186
|
|
|
240
187
|
# Returns a new Time representing the start of the hour (x:00)
|
|
241
188
|
def beginning_of_hour
|
|
@@ -245,65 +192,37 @@ class Time
|
|
|
245
192
|
|
|
246
193
|
# Returns a new Time representing the end of the hour, x:59:59.999999 (.999999999 in ruby1.9)
|
|
247
194
|
def end_of_hour
|
|
248
|
-
change(
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
#self - ((self.mday-1).days + self.seconds_since_midnight)
|
|
254
|
-
change(:day => 1, :hour => 0)
|
|
255
|
-
end
|
|
256
|
-
alias :at_beginning_of_month :beginning_of_month
|
|
257
|
-
|
|
258
|
-
# Returns a new Time representing the end of the month (end of the last day of the month)
|
|
259
|
-
def end_of_month
|
|
260
|
-
#self - ((self.mday-1).days + self.seconds_since_midnight)
|
|
261
|
-
last_day = ::Time.days_in_month(month, year)
|
|
262
|
-
change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => Rational(999999999, 1000))
|
|
263
|
-
end
|
|
264
|
-
alias :at_end_of_month :end_of_month
|
|
265
|
-
|
|
266
|
-
# Returns a new Time representing the start of the quarter (1st of january, april, july, october, 0:00)
|
|
267
|
-
def beginning_of_quarter
|
|
268
|
-
beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= month })
|
|
269
|
-
end
|
|
270
|
-
alias :at_beginning_of_quarter :beginning_of_quarter
|
|
271
|
-
|
|
272
|
-
# Returns a new Time representing the end of the quarter (end of the last day of march, june, september, december)
|
|
273
|
-
def end_of_quarter
|
|
274
|
-
beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= month }).end_of_month
|
|
275
|
-
end
|
|
276
|
-
alias :at_end_of_quarter :end_of_quarter
|
|
277
|
-
|
|
278
|
-
# Returns a new Time representing the start of the year (1st of january, 0:00)
|
|
279
|
-
def beginning_of_year
|
|
280
|
-
change(:month => 1, :day => 1, :hour => 0)
|
|
281
|
-
end
|
|
282
|
-
alias :at_beginning_of_year :beginning_of_year
|
|
283
|
-
|
|
284
|
-
# Returns a new Time representing the end of the year (end of the 31st of december)
|
|
285
|
-
def end_of_year
|
|
286
|
-
change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59, :usec => Rational(999999999, 1000))
|
|
195
|
+
change(
|
|
196
|
+
:min => 59,
|
|
197
|
+
:sec => 59,
|
|
198
|
+
:usec => Rational(999999999, 1000)
|
|
199
|
+
)
|
|
287
200
|
end
|
|
288
|
-
alias :
|
|
201
|
+
alias :at_end_of_hour :end_of_hour
|
|
289
202
|
|
|
290
|
-
#
|
|
291
|
-
def
|
|
292
|
-
|
|
203
|
+
# Returns a new Time representing the start of the minute (x:xx:00)
|
|
204
|
+
def beginning_of_minute
|
|
205
|
+
change(:sec => 0)
|
|
293
206
|
end
|
|
207
|
+
alias :at_beginning_of_minute :beginning_of_minute
|
|
294
208
|
|
|
295
|
-
#
|
|
296
|
-
def
|
|
297
|
-
|
|
209
|
+
# Returns a new Time representing the end of the minute, x:xx:59.999999 (.999999999 in ruby1.9)
|
|
210
|
+
def end_of_minute
|
|
211
|
+
change(
|
|
212
|
+
:sec => 59,
|
|
213
|
+
:usec => Rational(999999999, 1000)
|
|
214
|
+
)
|
|
298
215
|
end
|
|
216
|
+
alias :at_end_of_minute :end_of_minute
|
|
299
217
|
|
|
300
218
|
# Returns a Range representing the whole day of the current time.
|
|
301
219
|
def all_day
|
|
302
220
|
beginning_of_day..end_of_day
|
|
303
221
|
end
|
|
304
222
|
|
|
305
|
-
# Returns a Range representing the whole week of the current time.
|
|
306
|
-
|
|
223
|
+
# Returns a Range representing the whole week of the current time.
|
|
224
|
+
# Week starts on start_day, default is <tt>Date.week_start</tt> or <tt>config.week_start</tt> when set.
|
|
225
|
+
def all_week(start_day = Date.beginning_of_week)
|
|
307
226
|
beginning_of_week(start_day)..end_of_week(start_day)
|
|
308
227
|
end
|
|
309
228
|
|
|
@@ -356,7 +275,11 @@ class Time
|
|
|
356
275
|
# can be chronologically compared with a Time
|
|
357
276
|
def compare_with_coercion(other)
|
|
358
277
|
# we're avoiding Time#to_datetime cause it's expensive
|
|
359
|
-
other.is_a?(Time)
|
|
278
|
+
if other.is_a?(Time)
|
|
279
|
+
compare_without_coercion(other.to_time)
|
|
280
|
+
else
|
|
281
|
+
to_datetime <=> other
|
|
282
|
+
end
|
|
360
283
|
end
|
|
361
284
|
alias_method :compare_without_coercion, :<=>
|
|
362
285
|
alias_method :<=>, :compare_with_coercion
|
|
@@ -370,4 +293,5 @@ class Time
|
|
|
370
293
|
end
|
|
371
294
|
alias_method :eql_without_coercion, :eql?
|
|
372
295
|
alias_method :eql?, :eql_with_coercion
|
|
296
|
+
|
|
373
297
|
end
|
|
@@ -1,35 +1,39 @@
|
|
|
1
1
|
require 'active_support/inflector/methods'
|
|
2
|
-
require 'active_support/core_ext/time/publicize_conversion_methods'
|
|
3
2
|
require 'active_support/values/time_zone'
|
|
4
3
|
|
|
5
4
|
class Time
|
|
6
5
|
DATE_FORMATS = {
|
|
7
|
-
:db =>
|
|
8
|
-
:number =>
|
|
9
|
-
:
|
|
10
|
-
:
|
|
11
|
-
:
|
|
12
|
-
:
|
|
13
|
-
:
|
|
6
|
+
:db => '%Y-%m-%d %H:%M:%S',
|
|
7
|
+
:number => '%Y%m%d%H%M%S',
|
|
8
|
+
:nsec => '%Y%m%d%H%M%S%9N',
|
|
9
|
+
:time => '%H:%M',
|
|
10
|
+
:short => '%d %b %H:%M',
|
|
11
|
+
:long => '%B %d, %Y %H:%M',
|
|
12
|
+
:long_ordinal => lambda { |time|
|
|
13
|
+
day_format = ActiveSupport::Inflector.ordinalize(time.day)
|
|
14
|
+
time.strftime("%B #{day_format}, %Y %H:%M")
|
|
15
|
+
},
|
|
16
|
+
:rfc822 => lambda { |time|
|
|
17
|
+
offset_format = time.formatted_offset(false)
|
|
18
|
+
time.strftime("%a, %d %b %Y %H:%M:%S #{offset_format}")
|
|
19
|
+
}
|
|
14
20
|
}
|
|
15
21
|
|
|
16
|
-
DATE_FORMATS[:nsec] = '%Y%m%d%H%M%S%9N' if RUBY_VERSION >= '1.9'
|
|
17
|
-
|
|
18
22
|
# Converts to a formatted string. See DATE_FORMATS for builtin formats.
|
|
19
23
|
#
|
|
20
24
|
# This method is aliased to <tt>to_s</tt>.
|
|
21
25
|
#
|
|
22
|
-
# time = Time.now
|
|
26
|
+
# time = Time.now # => Thu Jan 18 06:10:17 CST 2007
|
|
23
27
|
#
|
|
24
|
-
# time.to_formatted_s(:time)
|
|
25
|
-
# time.to_s(:time)
|
|
28
|
+
# time.to_formatted_s(:time) # => "06:10"
|
|
29
|
+
# time.to_s(:time) # => "06:10"
|
|
26
30
|
#
|
|
27
|
-
# time.to_formatted_s(:db)
|
|
28
|
-
# time.to_formatted_s(:number)
|
|
29
|
-
# time.to_formatted_s(:short)
|
|
30
|
-
# time.to_formatted_s(:long)
|
|
31
|
-
# time.to_formatted_s(:long_ordinal)
|
|
32
|
-
# time.to_formatted_s(:rfc822)
|
|
31
|
+
# time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
|
|
32
|
+
# time.to_formatted_s(:number) # => "20070118061017"
|
|
33
|
+
# time.to_formatted_s(:short) # => "18 Jan 06:10"
|
|
34
|
+
# time.to_formatted_s(:long) # => "January 18, 2007 06:10"
|
|
35
|
+
# time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
|
|
36
|
+
# time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
|
|
33
37
|
#
|
|
34
38
|
# == Adding your own time formats to +to_formatted_s+
|
|
35
39
|
# You can add your own formats to the Time::DATE_FORMATS hash.
|
|
@@ -37,8 +41,8 @@ class Time
|
|
|
37
41
|
# or Proc instance that takes a time argument as the value.
|
|
38
42
|
#
|
|
39
43
|
# # config/initializers/time_formats.rb
|
|
40
|
-
# Time::DATE_FORMATS[:month_and_year] =
|
|
41
|
-
# Time::DATE_FORMATS[:short_ordinal]
|
|
44
|
+
# Time::DATE_FORMATS[:month_and_year] = '%B %Y'
|
|
45
|
+
# Time::DATE_FORMATS[:short_ordinal] = ->(time) { time.strftime("%B #{time.day.ordinalize}") }
|
|
42
46
|
def to_formatted_s(format = :default)
|
|
43
47
|
if formatter = DATE_FORMATS[format]
|
|
44
48
|
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
|
|
@@ -51,37 +55,9 @@ class Time
|
|
|
51
55
|
|
|
52
56
|
# Returns the UTC offset as an +HH:MM formatted string.
|
|
53
57
|
#
|
|
54
|
-
# Time.local(2000).formatted_offset
|
|
55
|
-
# Time.local(2000).formatted_offset(false)
|
|
58
|
+
# Time.local(2000).formatted_offset # => "-06:00"
|
|
59
|
+
# Time.local(2000).formatted_offset(false) # => "-0600"
|
|
56
60
|
def formatted_offset(colon = true, alternate_utc_string = nil)
|
|
57
61
|
utc? && alternate_utc_string || ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset, colon)
|
|
58
62
|
end
|
|
59
|
-
|
|
60
|
-
# Converts a Time object to a Date, dropping hour, minute, and second precision.
|
|
61
|
-
#
|
|
62
|
-
# my_time = Time.now # => Mon Nov 12 22:59:51 -0500 2007
|
|
63
|
-
# my_time.to_date # => Mon, 12 Nov 2007
|
|
64
|
-
#
|
|
65
|
-
# your_time = Time.parse("1/13/2009 1:13:03 P.M.") # => Tue Jan 13 13:13:03 -0500 2009
|
|
66
|
-
# your_time.to_date # => Tue, 13 Jan 2009
|
|
67
|
-
def to_date
|
|
68
|
-
::Date.new(year, month, day)
|
|
69
|
-
end unless method_defined?(:to_date)
|
|
70
|
-
|
|
71
|
-
# A method to keep Time, Date and DateTime instances interchangeable on conversions.
|
|
72
|
-
# In this case, it simply returns +self+.
|
|
73
|
-
def to_time
|
|
74
|
-
self
|
|
75
|
-
end unless method_defined?(:to_time)
|
|
76
|
-
|
|
77
|
-
# Converts a Time instance to a Ruby DateTime instance, preserving UTC offset.
|
|
78
|
-
#
|
|
79
|
-
# my_time = Time.now # => Mon Nov 12 23:04:21 -0500 2007
|
|
80
|
-
# my_time.to_datetime # => Mon, 12 Nov 2007 23:04:21 -0500
|
|
81
|
-
#
|
|
82
|
-
# your_time = Time.parse("1/13/2009 1:13:03 P.M.") # => Tue Jan 13 13:13:03 -0500 2009
|
|
83
|
-
# your_time.to_datetime # => Tue, 13 Jan 2009 13:13:03 -0500
|
|
84
|
-
def to_datetime
|
|
85
|
-
::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400))
|
|
86
|
-
end unless method_defined?(:to_datetime)
|
|
87
63
|
end
|
|
@@ -1,30 +1,3 @@
|
|
|
1
|
-
# Pre-1.9 versions of Ruby have a bug with marshaling Time instances, where utc instances are
|
|
2
|
-
# unmarshalled in the local zone, instead of utc. We're layering behavior on the _dump and _load
|
|
3
|
-
# methods so that utc instances can be flagged on dump, and coerced back to utc on load.
|
|
4
|
-
if !Marshal.load(Marshal.dump(Time.now.utc)).utc?
|
|
5
|
-
class Time
|
|
6
|
-
class << self
|
|
7
|
-
alias_method :_load_without_utc_flag, :_load
|
|
8
|
-
def _load(marshaled_time)
|
|
9
|
-
time = _load_without_utc_flag(marshaled_time)
|
|
10
|
-
time.instance_eval do
|
|
11
|
-
if defined?(@marshal_with_utc_coercion)
|
|
12
|
-
val = remove_instance_variable("@marshal_with_utc_coercion")
|
|
13
|
-
end
|
|
14
|
-
val ? utc : self
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
alias_method :_dump_without_utc_flag, :_dump
|
|
20
|
-
def _dump(*args)
|
|
21
|
-
obj = dup
|
|
22
|
-
obj.instance_variable_set('@marshal_with_utc_coercion', utc?)
|
|
23
|
-
obj.send :_dump_without_utc_flag, *args
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
1
|
# Ruby 1.9.2 adds utc_offset and zone to Time, but marshaling only
|
|
29
2
|
# preserves utc_offset. Preserve zone also, even though it may not
|
|
30
3
|
# work in some edge cases.
|
|
@@ -26,11 +26,11 @@ class Time
|
|
|
26
26
|
# around_filter :set_time_zone
|
|
27
27
|
#
|
|
28
28
|
# def set_time_zone
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
29
|
+
# if logged_in?
|
|
30
|
+
# Time.use_zone(current_user.time_zone) { yield }
|
|
31
|
+
# else
|
|
32
|
+
# yield
|
|
33
|
+
# end
|
|
34
34
|
# end
|
|
35
35
|
# end
|
|
36
36
|
def zone=(time_zone)
|
|
@@ -50,13 +50,21 @@ class Time
|
|
|
50
50
|
|
|
51
51
|
# Returns a TimeZone instance or nil, or raises an ArgumentError for invalid timezones.
|
|
52
52
|
def find_zone!(time_zone)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
if !time_zone || time_zone.is_a?(ActiveSupport::TimeZone)
|
|
54
|
+
time_zone
|
|
55
|
+
else
|
|
56
|
+
# lookup timezone based on identifier (unless we've been passed a TZInfo::Timezone)
|
|
57
|
+
unless time_zone.respond_to?(:period_for_local)
|
|
58
|
+
time_zone = ActiveSupport::TimeZone[time_zone] || TZInfo::Timezone.get(time_zone)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone
|
|
62
|
+
if time_zone.is_a?(ActiveSupport::TimeZone)
|
|
63
|
+
time_zone
|
|
64
|
+
else
|
|
65
|
+
ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone)
|
|
66
|
+
end
|
|
57
67
|
end
|
|
58
|
-
# Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone
|
|
59
|
-
time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone)
|
|
60
68
|
rescue TZInfo::InvalidTimezoneIdentifier
|
|
61
69
|
raise ArgumentError, "Invalid Timezone: #{time_zone}"
|
|
62
70
|
end
|
|
@@ -68,8 +76,8 @@ class Time
|
|
|
68
76
|
|
|
69
77
|
# Returns the simultaneous time in <tt>Time.zone</tt>.
|
|
70
78
|
#
|
|
71
|
-
# Time.zone = 'Hawaii'
|
|
72
|
-
# Time.utc(2000).in_time_zone
|
|
79
|
+
# Time.zone = 'Hawaii' # => 'Hawaii'
|
|
80
|
+
# Time.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00
|
|
73
81
|
#
|
|
74
82
|
# This method is similar to Time#localtime, except that it uses <tt>Time.zone</tt> as the local zone
|
|
75
83
|
# instead of the operating system's time zone.
|
|
@@ -77,10 +85,12 @@ class Time
|
|
|
77
85
|
# You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,
|
|
78
86
|
# and the conversion will be based on that zone instead of <tt>Time.zone</tt>.
|
|
79
87
|
#
|
|
80
|
-
# Time.utc(2000).in_time_zone('Alaska')
|
|
88
|
+
# Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
|
|
81
89
|
def in_time_zone(zone = ::Time.zone)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
90
|
+
if zone
|
|
91
|
+
ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.find_zone!(zone))
|
|
92
|
+
else
|
|
93
|
+
self
|
|
94
|
+
end
|
|
85
95
|
end
|
|
86
96
|
end
|
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
require 'uri'
|
|
4
|
+
str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese.
|
|
5
|
+
parser = URI::Parser.new
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
# YK: My initial experiments say yes, but let's be sure please
|
|
16
|
-
enc = str.encoding
|
|
17
|
-
enc = Encoding::UTF_8 if enc == Encoding::US_ASCII
|
|
18
|
-
str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(enc)
|
|
19
|
-
end
|
|
7
|
+
unless str == parser.unescape(parser.escape(str))
|
|
8
|
+
URI::Parser.class_eval do
|
|
9
|
+
remove_method :unescape
|
|
10
|
+
def unescape(str, escaped = /%[a-fA-F\d]{2}/)
|
|
11
|
+
# TODO: Are we actually sure that ASCII == UTF-8?
|
|
12
|
+
# YK: My initial experiments say yes, but let's be sure please
|
|
13
|
+
enc = str.encoding
|
|
14
|
+
enc = Encoding::UTF_8 if enc == Encoding::US_ASCII
|
|
15
|
+
str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(enc)
|
|
20
16
|
end
|
|
21
17
|
end
|
|
22
18
|
end
|
|
@@ -24,7 +20,7 @@ end
|
|
|
24
20
|
module URI
|
|
25
21
|
class << self
|
|
26
22
|
def parser
|
|
27
|
-
@parser ||= URI
|
|
23
|
+
@parser ||= URI::Parser.new
|
|
28
24
|
end
|
|
29
25
|
end
|
|
30
26
|
end
|