activesupport 5.0.7.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (236) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1013 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +39 -0
  5. data/lib/active_support.rb +99 -0
  6. data/lib/active_support/all.rb +3 -0
  7. data/lib/active_support/array_inquirer.rb +44 -0
  8. data/lib/active_support/backtrace_cleaner.rb +103 -0
  9. data/lib/active_support/benchmarkable.rb +49 -0
  10. data/lib/active_support/builder.rb +6 -0
  11. data/lib/active_support/cache.rb +701 -0
  12. data/lib/active_support/cache/file_store.rb +204 -0
  13. data/lib/active_support/cache/mem_cache_store.rb +207 -0
  14. data/lib/active_support/cache/memory_store.rb +167 -0
  15. data/lib/active_support/cache/null_store.rb +41 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +172 -0
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
  18. data/lib/active_support/callbacks.rb +791 -0
  19. data/lib/active_support/concern.rb +142 -0
  20. data/lib/active_support/concurrency/latch.rb +26 -0
  21. data/lib/active_support/concurrency/share_lock.rb +226 -0
  22. data/lib/active_support/configurable.rb +148 -0
  23. data/lib/active_support/core_ext.rb +4 -0
  24. data/lib/active_support/core_ext/array.rb +7 -0
  25. data/lib/active_support/core_ext/array/access.rb +90 -0
  26. data/lib/active_support/core_ext/array/conversions.rb +211 -0
  27. data/lib/active_support/core_ext/array/extract_options.rb +29 -0
  28. data/lib/active_support/core_ext/array/grouping.rb +107 -0
  29. data/lib/active_support/core_ext/array/inquiry.rb +17 -0
  30. data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
  31. data/lib/active_support/core_ext/array/wrap.rb +46 -0
  32. data/lib/active_support/core_ext/benchmark.rb +14 -0
  33. data/lib/active_support/core_ext/big_decimal.rb +1 -0
  34. data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
  35. data/lib/active_support/core_ext/class.rb +2 -0
  36. data/lib/active_support/core_ext/class/attribute.rb +128 -0
  37. data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -0
  38. data/lib/active_support/core_ext/class/subclasses.rb +41 -0
  39. data/lib/active_support/core_ext/date.rb +5 -0
  40. data/lib/active_support/core_ext/date/acts_like.rb +8 -0
  41. data/lib/active_support/core_ext/date/blank.rb +12 -0
  42. data/lib/active_support/core_ext/date/calculations.rb +143 -0
  43. data/lib/active_support/core_ext/date/conversions.rb +95 -0
  44. data/lib/active_support/core_ext/date/zones.rb +6 -0
  45. data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
  46. data/lib/active_support/core_ext/date_and_time/compatibility.rb +14 -0
  47. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  48. data/lib/active_support/core_ext/date_time.rb +5 -0
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +14 -0
  50. data/lib/active_support/core_ext/date_time/blank.rb +12 -0
  51. data/lib/active_support/core_ext/date_time/calculations.rb +199 -0
  52. data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
  53. data/lib/active_support/core_ext/date_time/conversions.rb +105 -0
  54. data/lib/active_support/core_ext/digest/uuid.rb +51 -0
  55. data/lib/active_support/core_ext/enumerable.rb +146 -0
  56. data/lib/active_support/core_ext/file.rb +1 -0
  57. data/lib/active_support/core_ext/file/atomic.rb +68 -0
  58. data/lib/active_support/core_ext/hash.rb +9 -0
  59. data/lib/active_support/core_ext/hash/compact.rb +24 -0
  60. data/lib/active_support/core_ext/hash/conversions.rb +262 -0
  61. data/lib/active_support/core_ext/hash/deep_merge.rb +38 -0
  62. data/lib/active_support/core_ext/hash/except.rb +22 -0
  63. data/lib/active_support/core_ext/hash/indifferent_access.rb +23 -0
  64. data/lib/active_support/core_ext/hash/keys.rb +170 -0
  65. data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -0
  66. data/lib/active_support/core_ext/hash/slice.rb +48 -0
  67. data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
  68. data/lib/active_support/core_ext/integer.rb +3 -0
  69. data/lib/active_support/core_ext/integer/inflections.rb +29 -0
  70. data/lib/active_support/core_ext/integer/multiple.rb +10 -0
  71. data/lib/active_support/core_ext/integer/time.rb +29 -0
  72. data/lib/active_support/core_ext/kernel.rb +4 -0
  73. data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
  74. data/lib/active_support/core_ext/kernel/concern.rb +12 -0
  75. data/lib/active_support/core_ext/kernel/debugger.rb +3 -0
  76. data/lib/active_support/core_ext/kernel/reporting.rb +43 -0
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +6 -0
  78. data/lib/active_support/core_ext/load_error.rb +31 -0
  79. data/lib/active_support/core_ext/marshal.rb +22 -0
  80. data/lib/active_support/core_ext/module.rb +12 -0
  81. data/lib/active_support/core_ext/module/aliasing.rb +74 -0
  82. data/lib/active_support/core_ext/module/anonymous.rb +28 -0
  83. data/lib/active_support/core_ext/module/attr_internal.rb +36 -0
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +135 -0
  87. data/lib/active_support/core_ext/module/delegation.rb +216 -0
  88. data/lib/active_support/core_ext/module/deprecation.rb +23 -0
  89. data/lib/active_support/core_ext/module/introspection.rb +68 -0
  90. data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
  91. data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
  92. data/lib/active_support/core_ext/module/reachable.rb +8 -0
  93. data/lib/active_support/core_ext/module/remove_method.rb +35 -0
  94. data/lib/active_support/core_ext/name_error.rb +31 -0
  95. data/lib/active_support/core_ext/numeric.rb +4 -0
  96. data/lib/active_support/core_ext/numeric/bytes.rb +64 -0
  97. data/lib/active_support/core_ext/numeric/conversions.rb +144 -0
  98. data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
  99. data/lib/active_support/core_ext/numeric/time.rb +74 -0
  100. data/lib/active_support/core_ext/object.rb +14 -0
  101. data/lib/active_support/core_ext/object/acts_like.rb +10 -0
  102. data/lib/active_support/core_ext/object/blank.rb +143 -0
  103. data/lib/active_support/core_ext/object/conversions.rb +4 -0
  104. data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
  105. data/lib/active_support/core_ext/object/duplicable.rb +124 -0
  106. data/lib/active_support/core_ext/object/inclusion.rb +27 -0
  107. data/lib/active_support/core_ext/object/instance_variables.rb +28 -0
  108. data/lib/active_support/core_ext/object/json.rb +205 -0
  109. data/lib/active_support/core_ext/object/to_param.rb +1 -0
  110. data/lib/active_support/core_ext/object/to_query.rb +84 -0
  111. data/lib/active_support/core_ext/object/try.rb +146 -0
  112. data/lib/active_support/core_ext/object/with_options.rb +69 -0
  113. data/lib/active_support/core_ext/range.rb +4 -0
  114. data/lib/active_support/core_ext/range/conversions.rb +31 -0
  115. data/lib/active_support/core_ext/range/each.rb +21 -0
  116. data/lib/active_support/core_ext/range/include_range.rb +23 -0
  117. data/lib/active_support/core_ext/range/overlaps.rb +8 -0
  118. data/lib/active_support/core_ext/regexp.rb +5 -0
  119. data/lib/active_support/core_ext/securerandom.rb +23 -0
  120. data/lib/active_support/core_ext/string.rb +13 -0
  121. data/lib/active_support/core_ext/string/access.rb +104 -0
  122. data/lib/active_support/core_ext/string/behavior.rb +6 -0
  123. data/lib/active_support/core_ext/string/conversions.rb +57 -0
  124. data/lib/active_support/core_ext/string/exclude.rb +11 -0
  125. data/lib/active_support/core_ext/string/filters.rb +102 -0
  126. data/lib/active_support/core_ext/string/indent.rb +43 -0
  127. data/lib/active_support/core_ext/string/inflections.rb +244 -0
  128. data/lib/active_support/core_ext/string/inquiry.rb +13 -0
  129. data/lib/active_support/core_ext/string/multibyte.rb +53 -0
  130. data/lib/active_support/core_ext/string/output_safety.rb +260 -0
  131. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
  132. data/lib/active_support/core_ext/string/strip.rb +23 -0
  133. data/lib/active_support/core_ext/string/zones.rb +14 -0
  134. data/lib/active_support/core_ext/struct.rb +3 -0
  135. data/lib/active_support/core_ext/time.rb +5 -0
  136. data/lib/active_support/core_ext/time/acts_like.rb +8 -0
  137. data/lib/active_support/core_ext/time/calculations.rb +290 -0
  138. data/lib/active_support/core_ext/time/compatibility.rb +14 -0
  139. data/lib/active_support/core_ext/time/conversions.rb +67 -0
  140. data/lib/active_support/core_ext/time/marshal.rb +3 -0
  141. data/lib/active_support/core_ext/time/zones.rb +111 -0
  142. data/lib/active_support/core_ext/uri.rb +24 -0
  143. data/lib/active_support/dependencies.rb +755 -0
  144. data/lib/active_support/dependencies/autoload.rb +77 -0
  145. data/lib/active_support/dependencies/interlock.rb +55 -0
  146. data/lib/active_support/deprecation.rb +43 -0
  147. data/lib/active_support/deprecation/behaviors.rb +90 -0
  148. data/lib/active_support/deprecation/instance_delegator.rb +37 -0
  149. data/lib/active_support/deprecation/method_wrappers.rb +70 -0
  150. data/lib/active_support/deprecation/proxy_wrappers.rb +149 -0
  151. data/lib/active_support/deprecation/reporting.rb +112 -0
  152. data/lib/active_support/descendants_tracker.rb +60 -0
  153. data/lib/active_support/duration.rb +235 -0
  154. data/lib/active_support/duration/iso8601_parser.rb +122 -0
  155. data/lib/active_support/duration/iso8601_serializer.rb +51 -0
  156. data/lib/active_support/evented_file_update_checker.rb +199 -0
  157. data/lib/active_support/execution_wrapper.rb +126 -0
  158. data/lib/active_support/executor.rb +6 -0
  159. data/lib/active_support/file_update_checker.rb +157 -0
  160. data/lib/active_support/gem_version.rb +15 -0
  161. data/lib/active_support/gzip.rb +36 -0
  162. data/lib/active_support/hash_with_indifferent_access.rb +329 -0
  163. data/lib/active_support/i18n.rb +13 -0
  164. data/lib/active_support/i18n_railtie.rb +115 -0
  165. data/lib/active_support/inflections.rb +70 -0
  166. data/lib/active_support/inflector.rb +7 -0
  167. data/lib/active_support/inflector/inflections.rb +242 -0
  168. data/lib/active_support/inflector/methods.rb +390 -0
  169. data/lib/active_support/inflector/transliterate.rb +112 -0
  170. data/lib/active_support/json.rb +2 -0
  171. data/lib/active_support/json/decoding.rb +74 -0
  172. data/lib/active_support/json/encoding.rb +127 -0
  173. data/lib/active_support/key_generator.rb +71 -0
  174. data/lib/active_support/lazy_load_hooks.rb +76 -0
  175. data/lib/active_support/locale/en.yml +135 -0
  176. data/lib/active_support/log_subscriber.rb +109 -0
  177. data/lib/active_support/log_subscriber/test_helper.rb +104 -0
  178. data/lib/active_support/logger.rb +106 -0
  179. data/lib/active_support/logger_silence.rb +28 -0
  180. data/lib/active_support/logger_thread_safe_level.rb +31 -0
  181. data/lib/active_support/message_encryptor.rb +114 -0
  182. data/lib/active_support/message_verifier.rb +134 -0
  183. data/lib/active_support/multibyte.rb +21 -0
  184. data/lib/active_support/multibyte/chars.rb +231 -0
  185. data/lib/active_support/multibyte/unicode.rb +413 -0
  186. data/lib/active_support/notifications.rb +212 -0
  187. data/lib/active_support/notifications/fanout.rb +157 -0
  188. data/lib/active_support/notifications/instrumenter.rb +91 -0
  189. data/lib/active_support/number_helper.rb +368 -0
  190. data/lib/active_support/number_helper/number_converter.rb +182 -0
  191. data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
  192. data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
  193. data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
  194. data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
  195. data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
  196. data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
  197. data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
  198. data/lib/active_support/option_merger.rb +25 -0
  199. data/lib/active_support/ordered_hash.rb +48 -0
  200. data/lib/active_support/ordered_options.rb +81 -0
  201. data/lib/active_support/per_thread_registry.rb +58 -0
  202. data/lib/active_support/proxy_object.rb +13 -0
  203. data/lib/active_support/rails.rb +27 -0
  204. data/lib/active_support/railtie.rb +51 -0
  205. data/lib/active_support/reloader.rb +129 -0
  206. data/lib/active_support/rescuable.rb +173 -0
  207. data/lib/active_support/security_utils.rb +27 -0
  208. data/lib/active_support/string_inquirer.rb +26 -0
  209. data/lib/active_support/subscriber.rb +120 -0
  210. data/lib/active_support/tagged_logging.rb +77 -0
  211. data/lib/active_support/test_case.rb +88 -0
  212. data/lib/active_support/testing/assertions.rb +99 -0
  213. data/lib/active_support/testing/autorun.rb +5 -0
  214. data/lib/active_support/testing/constant_lookup.rb +50 -0
  215. data/lib/active_support/testing/declarative.rb +26 -0
  216. data/lib/active_support/testing/deprecation.rb +36 -0
  217. data/lib/active_support/testing/file_fixtures.rb +34 -0
  218. data/lib/active_support/testing/isolation.rb +115 -0
  219. data/lib/active_support/testing/method_call_assertions.rb +41 -0
  220. data/lib/active_support/testing/setup_and_teardown.rb +50 -0
  221. data/lib/active_support/testing/stream.rb +42 -0
  222. data/lib/active_support/testing/tagged_logging.rb +25 -0
  223. data/lib/active_support/testing/time_helpers.rb +136 -0
  224. data/lib/active_support/time.rb +18 -0
  225. data/lib/active_support/time_with_zone.rb +511 -0
  226. data/lib/active_support/values/time_zone.rb +484 -0
  227. data/lib/active_support/values/unicode_tables.dat +0 -0
  228. data/lib/active_support/version.rb +8 -0
  229. data/lib/active_support/xml_mini.rb +209 -0
  230. data/lib/active_support/xml_mini/jdom.rb +181 -0
  231. data/lib/active_support/xml_mini/libxml.rb +77 -0
  232. data/lib/active_support/xml_mini/libxmlsax.rb +82 -0
  233. data/lib/active_support/xml_mini/nokogiri.rb +81 -0
  234. data/lib/active_support/xml_mini/nokogirisax.rb +85 -0
  235. data/lib/active_support/xml_mini/rexml.rb +128 -0
  236. metadata +350 -0
@@ -0,0 +1,4 @@
1
+ class String
2
+ alias_method :starts_with?, :start_with?
3
+ alias_method :ends_with?, :end_with?
4
+ end
@@ -0,0 +1,23 @@
1
+ class String
2
+ # Strips indentation in heredocs.
3
+ #
4
+ # For example in
5
+ #
6
+ # if options[:usage]
7
+ # puts <<-USAGE.strip_heredoc
8
+ # This command does such and such.
9
+ #
10
+ # Supported options are:
11
+ # -h This message
12
+ # ...
13
+ # USAGE
14
+ # end
15
+ #
16
+ # the user would see the usage message aligned against the left margin.
17
+ #
18
+ # Technically, it looks for the least indented non-empty line
19
+ # in the whole string, and removes that amount of leading whitespace.
20
+ def strip_heredoc
21
+ gsub(/^#{scan(/^[ \t]*(?=\S)/).min}/, ''.freeze)
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_support/core_ext/string/conversions'
2
+ require 'active_support/core_ext/time/zones'
3
+
4
+ class String
5
+ # Converts String to a TimeWithZone in the current zone if Time.zone or Time.zone_default
6
+ # is set, otherwise converts String to a Time via String#to_time
7
+ def in_time_zone(zone = ::Time.zone)
8
+ if zone
9
+ ::Time.find_zone!(zone).parse(self)
10
+ else
11
+ to_time
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ require 'active_support/deprecation'
2
+
3
+ ActiveSupport::Deprecation.warn("This file is deprecated and will be removed in Rails 5.1 with no replacement.")
@@ -0,0 +1,5 @@
1
+ require 'active_support/core_ext/time/acts_like'
2
+ require 'active_support/core_ext/time/calculations'
3
+ require 'active_support/core_ext/time/compatibility'
4
+ require 'active_support/core_ext/time/conversions'
5
+ require 'active_support/core_ext/time/zones'
@@ -0,0 +1,8 @@
1
+ require 'active_support/core_ext/object/acts_like'
2
+
3
+ class Time
4
+ # Duck-types as a Time-like class. See Object#acts_like?.
5
+ def acts_like_time?
6
+ true
7
+ end
8
+ end
@@ -0,0 +1,290 @@
1
+ require 'active_support/duration'
2
+ require 'active_support/core_ext/time/conversions'
3
+ require 'active_support/time_with_zone'
4
+ require 'active_support/core_ext/time/zones'
5
+ require 'active_support/core_ext/date_and_time/calculations'
6
+ require 'active_support/core_ext/date/calculations'
7
+
8
+ class Time
9
+ include DateAndTime::Calculations
10
+
11
+ COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
12
+
13
+ class << self
14
+ # Overriding case equality method so that it returns true for ActiveSupport::TimeWithZone instances
15
+ def ===(other)
16
+ super || (self == Time && other.is_a?(ActiveSupport::TimeWithZone))
17
+ end
18
+
19
+ # Returns the number of days in the given month.
20
+ # If no year is specified, it will use the current year.
21
+ def days_in_month(month, year = current.year)
22
+ if month == 2 && ::Date.gregorian_leap?(year)
23
+ 29
24
+ else
25
+ COMMON_YEAR_DAYS_IN_MONTH[month]
26
+ end
27
+ end
28
+
29
+ # Returns the number of days in the given year.
30
+ # If no year is specified, it will use the current year.
31
+ def days_in_year(year = current.year)
32
+ days_in_month(2, year) + 337
33
+ end
34
+
35
+ # Returns <tt>Time.zone.now</tt> when <tt>Time.zone</tt> or <tt>config.time_zone</tt> are set, otherwise just returns <tt>Time.now</tt>.
36
+ def current
37
+ ::Time.zone ? ::Time.zone.now : ::Time.now
38
+ end
39
+
40
+ # Layers additional behavior on Time.at so that ActiveSupport::TimeWithZone and DateTime
41
+ # instances can be used when called with a single argument
42
+ def at_with_coercion(*args)
43
+ return at_without_coercion(*args) if args.size != 1
44
+
45
+ # Time.at can be called with a time or numerical value
46
+ time_or_number = args.first
47
+
48
+ if time_or_number.is_a?(ActiveSupport::TimeWithZone) || time_or_number.is_a?(DateTime)
49
+ at_without_coercion(time_or_number.to_f).getlocal
50
+ else
51
+ at_without_coercion(time_or_number)
52
+ end
53
+ end
54
+ alias_method :at_without_coercion, :at
55
+ alias_method :at, :at_with_coercion
56
+ end
57
+
58
+ # Returns the number of seconds since 00:00:00.
59
+ #
60
+ # Time.new(2012, 8, 29, 0, 0, 0).seconds_since_midnight # => 0.0
61
+ # Time.new(2012, 8, 29, 12, 34, 56).seconds_since_midnight # => 45296.0
62
+ # Time.new(2012, 8, 29, 23, 59, 59).seconds_since_midnight # => 86399.0
63
+ def seconds_since_midnight
64
+ to_i - change(:hour => 0).to_i + (usec / 1.0e+6)
65
+ end
66
+
67
+ # Returns the number of seconds until 23:59:59.
68
+ #
69
+ # Time.new(2012, 8, 29, 0, 0, 0).seconds_until_end_of_day # => 86399
70
+ # Time.new(2012, 8, 29, 12, 34, 56).seconds_until_end_of_day # => 41103
71
+ # Time.new(2012, 8, 29, 23, 59, 59).seconds_until_end_of_day # => 0
72
+ def seconds_until_end_of_day
73
+ end_of_day.to_i - to_i
74
+ end
75
+
76
+ # Returns the fraction of a second as a +Rational+
77
+ #
78
+ # Time.new(2012, 8, 29, 0, 0, 0.5).sec_fraction # => (1/2)
79
+ def sec_fraction
80
+ subsec
81
+ end
82
+
83
+ # Returns a new Time where one or more of the elements have been changed according
84
+ # to the +options+ parameter. The time options (<tt>:hour</tt>, <tt>:min</tt>,
85
+ # <tt>:sec</tt>, <tt>:usec</tt>, <tt>:nsec</tt>) reset cascadingly, so if only
86
+ # the hour is passed, then minute, sec, usec and nsec is set to 0. If the hour
87
+ # and minute is passed, then sec, usec and nsec is set to 0. The +options+
88
+ # parameter takes a hash with any of these keys: <tt>:year</tt>, <tt>:month</tt>,
89
+ # <tt>:day</tt>, <tt>:hour</tt>, <tt>:min</tt>, <tt>:sec</tt>, <tt>:usec</tt>
90
+ # <tt>:nsec</tt>. Pass either <tt>:usec</tt> or <tt>:nsec</tt>, not both.
91
+ #
92
+ # Time.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => Time.new(2012, 8, 1, 22, 35, 0)
93
+ # Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => Time.new(1981, 8, 1, 22, 35, 0)
94
+ # Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => Time.new(1981, 8, 29, 0, 0, 0)
95
+ def change(options)
96
+ new_year = options.fetch(:year, year)
97
+ new_month = options.fetch(:month, month)
98
+ new_day = options.fetch(:day, day)
99
+ new_hour = options.fetch(:hour, hour)
100
+ new_min = options.fetch(:min, options[:hour] ? 0 : min)
101
+ new_sec = options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec)
102
+
103
+ if new_nsec = options[:nsec]
104
+ raise ArgumentError, "Can't change both :nsec and :usec at the same time: #{options.inspect}" if options[:usec]
105
+ new_usec = Rational(new_nsec, 1000)
106
+ else
107
+ new_usec = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
108
+ end
109
+
110
+ if utc?
111
+ ::Time.utc(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec)
112
+ elsif zone
113
+ ::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec)
114
+ else
115
+ raise ArgumentError, 'argument out of range' if new_usec >= 1000000
116
+ ::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec + (new_usec.to_r / 1000000), utc_offset)
117
+ end
118
+ end
119
+
120
+ # Uses Date to provide precise Time calculations for years, months, and days
121
+ # according to the proleptic Gregorian calendar. The +options+ parameter
122
+ # takes a hash with any of these keys: <tt>:years</tt>, <tt>:months</tt>,
123
+ # <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>, <tt>:minutes</tt>,
124
+ # <tt>:seconds</tt>.
125
+ #
126
+ # Time.new(2015, 8, 1, 14, 35, 0).advance(seconds: 1) # => 2015-08-01 14:35:01 -0700
127
+ # Time.new(2015, 8, 1, 14, 35, 0).advance(minutes: 1) # => 2015-08-01 14:36:00 -0700
128
+ # Time.new(2015, 8, 1, 14, 35, 0).advance(hours: 1) # => 2015-08-01 15:35:00 -0700
129
+ # Time.new(2015, 8, 1, 14, 35, 0).advance(days: 1) # => 2015-08-02 14:35:00 -0700
130
+ # Time.new(2015, 8, 1, 14, 35, 0).advance(weeks: 1) # => 2015-08-08 14:35:00 -0700
131
+ def advance(options)
132
+ unless options[:weeks].nil?
133
+ options[:weeks], partial_weeks = options[:weeks].divmod(1)
134
+ options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
135
+ end
136
+
137
+ unless options[:days].nil?
138
+ options[:days], partial_days = options[:days].divmod(1)
139
+ options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
140
+ end
141
+
142
+ d = to_date.advance(options)
143
+ d = d.gregorian if d.julian?
144
+ time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
145
+ seconds_to_advance = \
146
+ options.fetch(:seconds, 0) +
147
+ options.fetch(:minutes, 0) * 60 +
148
+ options.fetch(:hours, 0) * 3600
149
+
150
+ if seconds_to_advance.zero?
151
+ time_advanced_by_date
152
+ else
153
+ time_advanced_by_date.since(seconds_to_advance)
154
+ end
155
+ end
156
+
157
+ # Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension
158
+ def ago(seconds)
159
+ since(-seconds)
160
+ end
161
+
162
+ # Returns a new Time representing the time a number of seconds since the instance time
163
+ def since(seconds)
164
+ self + seconds
165
+ rescue
166
+ to_datetime.since(seconds)
167
+ end
168
+ alias :in :since
169
+
170
+ # Returns a new Time representing the start of the day (0:00)
171
+ def beginning_of_day
172
+ change(:hour => 0)
173
+ end
174
+ alias :midnight :beginning_of_day
175
+ alias :at_midnight :beginning_of_day
176
+ alias :at_beginning_of_day :beginning_of_day
177
+
178
+ # Returns a new Time representing the middle of the day (12:00)
179
+ def middle_of_day
180
+ change(:hour => 12)
181
+ end
182
+ alias :midday :middle_of_day
183
+ alias :noon :middle_of_day
184
+ alias :at_midday :middle_of_day
185
+ alias :at_noon :middle_of_day
186
+ alias :at_middle_of_day :middle_of_day
187
+
188
+ # Returns a new Time representing the end of the day, 23:59:59.999999
189
+ def end_of_day
190
+ change(
191
+ :hour => 23,
192
+ :min => 59,
193
+ :sec => 59,
194
+ :usec => Rational(999999999, 1000)
195
+ )
196
+ end
197
+ alias :at_end_of_day :end_of_day
198
+
199
+ # Returns a new Time representing the start of the hour (x:00)
200
+ def beginning_of_hour
201
+ change(:min => 0)
202
+ end
203
+ alias :at_beginning_of_hour :beginning_of_hour
204
+
205
+ # Returns a new Time representing the end of the hour, x:59:59.999999
206
+ def end_of_hour
207
+ change(
208
+ :min => 59,
209
+ :sec => 59,
210
+ :usec => Rational(999999999, 1000)
211
+ )
212
+ end
213
+ alias :at_end_of_hour :end_of_hour
214
+
215
+ # Returns a new Time representing the start of the minute (x:xx:00)
216
+ def beginning_of_minute
217
+ change(:sec => 0)
218
+ end
219
+ alias :at_beginning_of_minute :beginning_of_minute
220
+
221
+ # Returns a new Time representing the end of the minute, x:xx:59.999999
222
+ def end_of_minute
223
+ change(
224
+ :sec => 59,
225
+ :usec => Rational(999999999, 1000)
226
+ )
227
+ end
228
+ alias :at_end_of_minute :end_of_minute
229
+
230
+ # Returns a Range representing the whole day of the current time.
231
+ def all_day
232
+ beginning_of_day..end_of_day
233
+ end
234
+
235
+ def plus_with_duration(other) #:nodoc:
236
+ if ActiveSupport::Duration === other
237
+ other.since(self)
238
+ else
239
+ plus_without_duration(other)
240
+ end
241
+ end
242
+ alias_method :plus_without_duration, :+
243
+ alias_method :+, :plus_with_duration
244
+
245
+ def minus_with_duration(other) #:nodoc:
246
+ if ActiveSupport::Duration === other
247
+ other.until(self)
248
+ else
249
+ minus_without_duration(other)
250
+ end
251
+ end
252
+ alias_method :minus_without_duration, :-
253
+ alias_method :-, :minus_with_duration
254
+
255
+ # Time#- can also be used to determine the number of seconds between two Time instances.
256
+ # We're layering on additional behavior so that ActiveSupport::TimeWithZone instances
257
+ # are coerced into values that Time#- will recognize
258
+ def minus_with_coercion(other)
259
+ other = other.comparable_time if other.respond_to?(:comparable_time)
260
+ other.is_a?(DateTime) ? to_f - other.to_f : minus_without_coercion(other)
261
+ end
262
+ alias_method :minus_without_coercion, :-
263
+ alias_method :-, :minus_with_coercion
264
+
265
+ # Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances
266
+ # can be chronologically compared with a Time
267
+ def compare_with_coercion(other)
268
+ # we're avoiding Time#to_datetime and Time#to_time because they're expensive
269
+ if other.class == Time
270
+ compare_without_coercion(other)
271
+ elsif other.is_a?(Time)
272
+ compare_without_coercion(other.to_time)
273
+ else
274
+ to_datetime <=> other
275
+ end
276
+ end
277
+ alias_method :compare_without_coercion, :<=>
278
+ alias_method :<=>, :compare_with_coercion
279
+
280
+ # Layers additional behavior on Time#eql? so that ActiveSupport::TimeWithZone instances
281
+ # can be eql? to an equivalent Time
282
+ def eql_with_coercion(other)
283
+ # if other is an ActiveSupport::TimeWithZone, coerce a Time instance from it so we can do eql? comparison
284
+ other = other.comparable_time if other.respond_to?(:comparable_time)
285
+ eql_without_coercion(other)
286
+ end
287
+ alias_method :eql_without_coercion, :eql?
288
+ alias_method :eql?, :eql_with_coercion
289
+
290
+ end
@@ -0,0 +1,14 @@
1
+ require "active_support/core_ext/date_and_time/compatibility"
2
+ require "active_support/core_ext/module/remove_method"
3
+
4
+ class Time
5
+ include DateAndTime::Compatibility
6
+
7
+ remove_possible_method :to_time
8
+
9
+ # Either return +self+ or the time in the local system timezone depending
10
+ # on the setting of +ActiveSupport.to_time_preserves_timezone+.
11
+ def to_time
12
+ preserve_timezone ? self : getlocal
13
+ end
14
+ end
@@ -0,0 +1,67 @@
1
+ require 'active_support/inflector/methods'
2
+ require 'active_support/values/time_zone'
3
+
4
+ class Time
5
+ DATE_FORMATS = {
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
+ :usec => '%Y%m%d%H%M%S%6N',
10
+ :time => '%H:%M',
11
+ :short => '%d %b %H:%M',
12
+ :long => '%B %d, %Y %H:%M',
13
+ :long_ordinal => lambda { |time|
14
+ day_format = ActiveSupport::Inflector.ordinalize(time.day)
15
+ time.strftime("%B #{day_format}, %Y %H:%M")
16
+ },
17
+ :rfc822 => lambda { |time|
18
+ offset_format = time.formatted_offset(false)
19
+ time.strftime("%a, %d %b %Y %H:%M:%S #{offset_format}")
20
+ },
21
+ :iso8601 => lambda { |time| time.iso8601 }
22
+ }
23
+
24
+ # Converts to a formatted string. See DATE_FORMATS for built-in formats.
25
+ #
26
+ # This method is aliased to <tt>to_s</tt>.
27
+ #
28
+ # time = Time.now # => 2007-01-18 06:10:17 -06:00
29
+ #
30
+ # time.to_formatted_s(:time) # => "06:10"
31
+ # time.to_s(:time) # => "06:10"
32
+ #
33
+ # time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
34
+ # time.to_formatted_s(:number) # => "20070118061017"
35
+ # time.to_formatted_s(:short) # => "18 Jan 06:10"
36
+ # time.to_formatted_s(:long) # => "January 18, 2007 06:10"
37
+ # time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
38
+ # time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
39
+ # time.to_formatted_s(:iso8601) # => "2007-01-18T06:10:17-06:00"
40
+ #
41
+ # == Adding your own time formats to +to_formatted_s+
42
+ # You can add your own formats to the Time::DATE_FORMATS hash.
43
+ # Use the format name as the hash key and either a strftime string
44
+ # or Proc instance that takes a time argument as the value.
45
+ #
46
+ # # config/initializers/time_formats.rb
47
+ # Time::DATE_FORMATS[:month_and_year] = '%B %Y'
48
+ # Time::DATE_FORMATS[:short_ordinal] = ->(time) { time.strftime("%B #{time.day.ordinalize}") }
49
+ def to_formatted_s(format = :default)
50
+ if formatter = DATE_FORMATS[format]
51
+ formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
52
+ else
53
+ to_default_s
54
+ end
55
+ end
56
+ alias_method :to_default_s, :to_s
57
+ alias_method :to_s, :to_formatted_s
58
+
59
+ # Returns a formatted string of the offset from UTC, or an alternative
60
+ # string if the time zone is already UTC.
61
+ #
62
+ # Time.local(2000).formatted_offset # => "-06:00"
63
+ # Time.local(2000).formatted_offset(false) # => "-0600"
64
+ def formatted_offset(colon = true, alternate_utc_string = nil)
65
+ utc? && alternate_utc_string || ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset, colon)
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ require 'active_support/deprecation'
2
+
3
+ ActiveSupport::Deprecation.warn("This is deprecated and will be removed in Rails 5.1 with no replacement.")
@@ -0,0 +1,111 @@
1
+ require 'active_support/time_with_zone'
2
+ require 'active_support/core_ext/time/acts_like'
3
+ require 'active_support/core_ext/date_and_time/zones'
4
+
5
+ class Time
6
+ include DateAndTime::Zones
7
+ class << self
8
+ attr_accessor :zone_default
9
+
10
+ # Returns the TimeZone for the current request, if this has been set (via Time.zone=).
11
+ # If <tt>Time.zone</tt> has not been set for the current request, returns the TimeZone specified in <tt>config.time_zone</tt>.
12
+ def zone
13
+ Thread.current[:time_zone] || zone_default
14
+ end
15
+
16
+ # Sets <tt>Time.zone</tt> to a TimeZone object for the current request/thread.
17
+ #
18
+ # This method accepts any of the following:
19
+ #
20
+ # * A Rails TimeZone object.
21
+ # * An identifier for a Rails TimeZone object (e.g., "Eastern Time (US & Canada)", <tt>-5.hours</tt>).
22
+ # * A TZInfo::Timezone object.
23
+ # * An identifier for a TZInfo::Timezone object (e.g., "America/New_York").
24
+ #
25
+ # Here's an example of how you might set <tt>Time.zone</tt> on a per request basis and reset it when the request is done.
26
+ # <tt>current_user.time_zone</tt> just needs to return a string identifying the user's preferred time zone:
27
+ #
28
+ # class ApplicationController < ActionController::Base
29
+ # around_action :set_time_zone
30
+ #
31
+ # def set_time_zone
32
+ # if logged_in?
33
+ # Time.use_zone(current_user.time_zone) { yield }
34
+ # else
35
+ # yield
36
+ # end
37
+ # end
38
+ # end
39
+ def zone=(time_zone)
40
+ Thread.current[:time_zone] = find_zone!(time_zone)
41
+ end
42
+
43
+ # Allows override of <tt>Time.zone</tt> locally inside supplied block;
44
+ # resets <tt>Time.zone</tt> to existing value when done.
45
+ #
46
+ # class ApplicationController < ActionController::Base
47
+ # around_action :set_time_zone
48
+ #
49
+ # private
50
+ #
51
+ # def set_time_zone
52
+ # Time.use_zone(current_user.timezone) { yield }
53
+ # end
54
+ # end
55
+ #
56
+ # NOTE: This won't affect any <tt>ActiveSupport::TimeWithZone</tt>
57
+ # objects that have already been created, e.g. any model timestamp
58
+ # attributes that have been read before the block will remain in
59
+ # the application's default timezone.
60
+ def use_zone(time_zone)
61
+ new_zone = find_zone!(time_zone)
62
+ begin
63
+ old_zone, ::Time.zone = ::Time.zone, new_zone
64
+ yield
65
+ ensure
66
+ ::Time.zone = old_zone
67
+ end
68
+ end
69
+
70
+ # Returns a TimeZone instance matching the time zone provided.
71
+ # Accepts the time zone in any format supported by <tt>Time.zone=</tt>.
72
+ # Raises an +ArgumentError+ for invalid time zones.
73
+ #
74
+ # Time.find_zone! "America/New_York" # => #<ActiveSupport::TimeZone @name="America/New_York" ...>
75
+ # Time.find_zone! "EST" # => #<ActiveSupport::TimeZone @name="EST" ...>
76
+ # Time.find_zone! -5.hours # => #<ActiveSupport::TimeZone @name="Bogota" ...>
77
+ # Time.find_zone! nil # => nil
78
+ # Time.find_zone! false # => false
79
+ # Time.find_zone! "NOT-A-TIMEZONE" # => ArgumentError: Invalid Timezone: NOT-A-TIMEZONE
80
+ def find_zone!(time_zone)
81
+ if !time_zone || time_zone.is_a?(ActiveSupport::TimeZone)
82
+ time_zone
83
+ else
84
+ # Look up the timezone based on the identifier (unless we've been
85
+ # passed a TZInfo::Timezone)
86
+ unless time_zone.respond_to?(:period_for_local)
87
+ time_zone = ActiveSupport::TimeZone[time_zone] || TZInfo::Timezone.get(time_zone)
88
+ end
89
+
90
+ # Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone
91
+ if time_zone.is_a?(ActiveSupport::TimeZone)
92
+ time_zone
93
+ else
94
+ ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone)
95
+ end
96
+ end
97
+ rescue TZInfo::InvalidTimezoneIdentifier
98
+ raise ArgumentError, "Invalid Timezone: #{time_zone}"
99
+ end
100
+
101
+ # Returns a TimeZone instance matching the time zone provided.
102
+ # Accepts the time zone in any format supported by <tt>Time.zone=</tt>.
103
+ # Returns +nil+ for invalid time zones.
104
+ #
105
+ # Time.find_zone "America/New_York" # => #<ActiveSupport::TimeZone @name="America/New_York" ...>
106
+ # Time.find_zone "NOT-A-TIMEZONE" # => nil
107
+ def find_zone(time_zone)
108
+ find_zone!(time_zone) rescue nil
109
+ end
110
+ end
111
+ end