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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1013 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +39 -0
- data/lib/active_support.rb +99 -0
- data/lib/active_support/all.rb +3 -0
- data/lib/active_support/array_inquirer.rb +44 -0
- data/lib/active_support/backtrace_cleaner.rb +103 -0
- data/lib/active_support/benchmarkable.rb +49 -0
- data/lib/active_support/builder.rb +6 -0
- data/lib/active_support/cache.rb +701 -0
- data/lib/active_support/cache/file_store.rb +204 -0
- data/lib/active_support/cache/mem_cache_store.rb +207 -0
- data/lib/active_support/cache/memory_store.rb +167 -0
- data/lib/active_support/cache/null_store.rb +41 -0
- data/lib/active_support/cache/strategy/local_cache.rb +172 -0
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
- data/lib/active_support/callbacks.rb +791 -0
- data/lib/active_support/concern.rb +142 -0
- data/lib/active_support/concurrency/latch.rb +26 -0
- data/lib/active_support/concurrency/share_lock.rb +226 -0
- data/lib/active_support/configurable.rb +148 -0
- data/lib/active_support/core_ext.rb +4 -0
- data/lib/active_support/core_ext/array.rb +7 -0
- data/lib/active_support/core_ext/array/access.rb +90 -0
- data/lib/active_support/core_ext/array/conversions.rb +211 -0
- data/lib/active_support/core_ext/array/extract_options.rb +29 -0
- data/lib/active_support/core_ext/array/grouping.rb +107 -0
- data/lib/active_support/core_ext/array/inquiry.rb +17 -0
- data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
- data/lib/active_support/core_ext/array/wrap.rb +46 -0
- data/lib/active_support/core_ext/benchmark.rb +14 -0
- data/lib/active_support/core_ext/big_decimal.rb +1 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/active_support/core_ext/class.rb +2 -0
- data/lib/active_support/core_ext/class/attribute.rb +128 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -0
- data/lib/active_support/core_ext/class/subclasses.rb +41 -0
- data/lib/active_support/core_ext/date.rb +5 -0
- data/lib/active_support/core_ext/date/acts_like.rb +8 -0
- data/lib/active_support/core_ext/date/blank.rb +12 -0
- data/lib/active_support/core_ext/date/calculations.rb +143 -0
- data/lib/active_support/core_ext/date/conversions.rb +95 -0
- data/lib/active_support/core_ext/date/zones.rb +6 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- data/lib/active_support/core_ext/date_time.rb +5 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +14 -0
- data/lib/active_support/core_ext/date_time/blank.rb +12 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +199 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +105 -0
- data/lib/active_support/core_ext/digest/uuid.rb +51 -0
- data/lib/active_support/core_ext/enumerable.rb +146 -0
- data/lib/active_support/core_ext/file.rb +1 -0
- data/lib/active_support/core_ext/file/atomic.rb +68 -0
- data/lib/active_support/core_ext/hash.rb +9 -0
- data/lib/active_support/core_ext/hash/compact.rb +24 -0
- data/lib/active_support/core_ext/hash/conversions.rb +262 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +38 -0
- data/lib/active_support/core_ext/hash/except.rb +22 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +23 -0
- data/lib/active_support/core_ext/hash/keys.rb +170 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -0
- data/lib/active_support/core_ext/hash/slice.rb +48 -0
- data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
- data/lib/active_support/core_ext/integer.rb +3 -0
- data/lib/active_support/core_ext/integer/inflections.rb +29 -0
- data/lib/active_support/core_ext/integer/multiple.rb +10 -0
- data/lib/active_support/core_ext/integer/time.rb +29 -0
- data/lib/active_support/core_ext/kernel.rb +4 -0
- data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/active_support/core_ext/kernel/concern.rb +12 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +3 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +43 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +6 -0
- data/lib/active_support/core_ext/load_error.rb +31 -0
- data/lib/active_support/core_ext/marshal.rb +22 -0
- data/lib/active_support/core_ext/module.rb +12 -0
- data/lib/active_support/core_ext/module/aliasing.rb +74 -0
- data/lib/active_support/core_ext/module/anonymous.rb +28 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +36 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
- data/lib/active_support/core_ext/module/concerning.rb +135 -0
- data/lib/active_support/core_ext/module/delegation.rb +216 -0
- data/lib/active_support/core_ext/module/deprecation.rb +23 -0
- data/lib/active_support/core_ext/module/introspection.rb +68 -0
- data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
- data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
- data/lib/active_support/core_ext/module/reachable.rb +8 -0
- data/lib/active_support/core_ext/module/remove_method.rb +35 -0
- data/lib/active_support/core_ext/name_error.rb +31 -0
- data/lib/active_support/core_ext/numeric.rb +4 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +64 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +144 -0
- data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
- data/lib/active_support/core_ext/numeric/time.rb +74 -0
- data/lib/active_support/core_ext/object.rb +14 -0
- data/lib/active_support/core_ext/object/acts_like.rb +10 -0
- data/lib/active_support/core_ext/object/blank.rb +143 -0
- data/lib/active_support/core_ext/object/conversions.rb +4 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
- data/lib/active_support/core_ext/object/duplicable.rb +124 -0
- data/lib/active_support/core_ext/object/inclusion.rb +27 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +28 -0
- data/lib/active_support/core_ext/object/json.rb +205 -0
- data/lib/active_support/core_ext/object/to_param.rb +1 -0
- data/lib/active_support/core_ext/object/to_query.rb +84 -0
- data/lib/active_support/core_ext/object/try.rb +146 -0
- data/lib/active_support/core_ext/object/with_options.rb +69 -0
- data/lib/active_support/core_ext/range.rb +4 -0
- data/lib/active_support/core_ext/range/conversions.rb +31 -0
- data/lib/active_support/core_ext/range/each.rb +21 -0
- data/lib/active_support/core_ext/range/include_range.rb +23 -0
- data/lib/active_support/core_ext/range/overlaps.rb +8 -0
- data/lib/active_support/core_ext/regexp.rb +5 -0
- data/lib/active_support/core_ext/securerandom.rb +23 -0
- data/lib/active_support/core_ext/string.rb +13 -0
- data/lib/active_support/core_ext/string/access.rb +104 -0
- data/lib/active_support/core_ext/string/behavior.rb +6 -0
- data/lib/active_support/core_ext/string/conversions.rb +57 -0
- data/lib/active_support/core_ext/string/exclude.rb +11 -0
- data/lib/active_support/core_ext/string/filters.rb +102 -0
- data/lib/active_support/core_ext/string/indent.rb +43 -0
- data/lib/active_support/core_ext/string/inflections.rb +244 -0
- data/lib/active_support/core_ext/string/inquiry.rb +13 -0
- data/lib/active_support/core_ext/string/multibyte.rb +53 -0
- data/lib/active_support/core_ext/string/output_safety.rb +260 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
- data/lib/active_support/core_ext/string/strip.rb +23 -0
- data/lib/active_support/core_ext/string/zones.rb +14 -0
- data/lib/active_support/core_ext/struct.rb +3 -0
- data/lib/active_support/core_ext/time.rb +5 -0
- data/lib/active_support/core_ext/time/acts_like.rb +8 -0
- data/lib/active_support/core_ext/time/calculations.rb +290 -0
- data/lib/active_support/core_ext/time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/time/conversions.rb +67 -0
- data/lib/active_support/core_ext/time/marshal.rb +3 -0
- data/lib/active_support/core_ext/time/zones.rb +111 -0
- data/lib/active_support/core_ext/uri.rb +24 -0
- data/lib/active_support/dependencies.rb +755 -0
- data/lib/active_support/dependencies/autoload.rb +77 -0
- data/lib/active_support/dependencies/interlock.rb +55 -0
- data/lib/active_support/deprecation.rb +43 -0
- data/lib/active_support/deprecation/behaviors.rb +90 -0
- data/lib/active_support/deprecation/instance_delegator.rb +37 -0
- data/lib/active_support/deprecation/method_wrappers.rb +70 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +149 -0
- data/lib/active_support/deprecation/reporting.rb +112 -0
- data/lib/active_support/descendants_tracker.rb +60 -0
- data/lib/active_support/duration.rb +235 -0
- data/lib/active_support/duration/iso8601_parser.rb +122 -0
- data/lib/active_support/duration/iso8601_serializer.rb +51 -0
- data/lib/active_support/evented_file_update_checker.rb +199 -0
- data/lib/active_support/execution_wrapper.rb +126 -0
- data/lib/active_support/executor.rb +6 -0
- data/lib/active_support/file_update_checker.rb +157 -0
- data/lib/active_support/gem_version.rb +15 -0
- data/lib/active_support/gzip.rb +36 -0
- data/lib/active_support/hash_with_indifferent_access.rb +329 -0
- data/lib/active_support/i18n.rb +13 -0
- data/lib/active_support/i18n_railtie.rb +115 -0
- data/lib/active_support/inflections.rb +70 -0
- data/lib/active_support/inflector.rb +7 -0
- data/lib/active_support/inflector/inflections.rb +242 -0
- data/lib/active_support/inflector/methods.rb +390 -0
- data/lib/active_support/inflector/transliterate.rb +112 -0
- data/lib/active_support/json.rb +2 -0
- data/lib/active_support/json/decoding.rb +74 -0
- data/lib/active_support/json/encoding.rb +127 -0
- data/lib/active_support/key_generator.rb +71 -0
- data/lib/active_support/lazy_load_hooks.rb +76 -0
- data/lib/active_support/locale/en.yml +135 -0
- data/lib/active_support/log_subscriber.rb +109 -0
- data/lib/active_support/log_subscriber/test_helper.rb +104 -0
- data/lib/active_support/logger.rb +106 -0
- data/lib/active_support/logger_silence.rb +28 -0
- data/lib/active_support/logger_thread_safe_level.rb +31 -0
- data/lib/active_support/message_encryptor.rb +114 -0
- data/lib/active_support/message_verifier.rb +134 -0
- data/lib/active_support/multibyte.rb +21 -0
- data/lib/active_support/multibyte/chars.rb +231 -0
- data/lib/active_support/multibyte/unicode.rb +413 -0
- data/lib/active_support/notifications.rb +212 -0
- data/lib/active_support/notifications/fanout.rb +157 -0
- data/lib/active_support/notifications/instrumenter.rb +91 -0
- data/lib/active_support/number_helper.rb +368 -0
- data/lib/active_support/number_helper/number_converter.rb +182 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
- data/lib/active_support/option_merger.rb +25 -0
- data/lib/active_support/ordered_hash.rb +48 -0
- data/lib/active_support/ordered_options.rb +81 -0
- data/lib/active_support/per_thread_registry.rb +58 -0
- data/lib/active_support/proxy_object.rb +13 -0
- data/lib/active_support/rails.rb +27 -0
- data/lib/active_support/railtie.rb +51 -0
- data/lib/active_support/reloader.rb +129 -0
- data/lib/active_support/rescuable.rb +173 -0
- data/lib/active_support/security_utils.rb +27 -0
- data/lib/active_support/string_inquirer.rb +26 -0
- data/lib/active_support/subscriber.rb +120 -0
- data/lib/active_support/tagged_logging.rb +77 -0
- data/lib/active_support/test_case.rb +88 -0
- data/lib/active_support/testing/assertions.rb +99 -0
- data/lib/active_support/testing/autorun.rb +5 -0
- data/lib/active_support/testing/constant_lookup.rb +50 -0
- data/lib/active_support/testing/declarative.rb +26 -0
- data/lib/active_support/testing/deprecation.rb +36 -0
- data/lib/active_support/testing/file_fixtures.rb +34 -0
- data/lib/active_support/testing/isolation.rb +115 -0
- data/lib/active_support/testing/method_call_assertions.rb +41 -0
- data/lib/active_support/testing/setup_and_teardown.rb +50 -0
- data/lib/active_support/testing/stream.rb +42 -0
- data/lib/active_support/testing/tagged_logging.rb +25 -0
- data/lib/active_support/testing/time_helpers.rb +136 -0
- data/lib/active_support/time.rb +18 -0
- data/lib/active_support/time_with_zone.rb +511 -0
- data/lib/active_support/values/time_zone.rb +484 -0
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +8 -0
- data/lib/active_support/xml_mini.rb +209 -0
- data/lib/active_support/xml_mini/jdom.rb +181 -0
- data/lib/active_support/xml_mini/libxml.rb +77 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +82 -0
- data/lib/active_support/xml_mini/nokogiri.rb +81 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +85 -0
- data/lib/active_support/xml_mini/rexml.rb +128 -0
- metadata +350 -0
@@ -0,0 +1,40 @@
|
|
1
|
+
module DateAndTime
|
2
|
+
module Zones
|
3
|
+
# Returns the simultaneous time in <tt>Time.zone</tt> if a zone is given or
|
4
|
+
# if Time.zone_default is set. Otherwise, it returns the current time.
|
5
|
+
#
|
6
|
+
# Time.zone = 'Hawaii' # => 'Hawaii'
|
7
|
+
# Time.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00
|
8
|
+
# Date.new(2000).in_time_zone # => Sat, 01 Jan 2000 00:00:00 HST -10:00
|
9
|
+
#
|
10
|
+
# This method is similar to Time#localtime, except that it uses <tt>Time.zone</tt> as the local zone
|
11
|
+
# instead of the operating system's time zone.
|
12
|
+
#
|
13
|
+
# You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,
|
14
|
+
# and the conversion will be based on that zone instead of <tt>Time.zone</tt>.
|
15
|
+
#
|
16
|
+
# Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
|
17
|
+
# Date.new(2000).in_time_zone('Alaska') # => Sat, 01 Jan 2000 00:00:00 AKST -09:00
|
18
|
+
def in_time_zone(zone = ::Time.zone)
|
19
|
+
time_zone = ::Time.find_zone! zone
|
20
|
+
time = acts_like?(:time) ? self : nil
|
21
|
+
|
22
|
+
if time_zone
|
23
|
+
time_with_zone(time, time_zone)
|
24
|
+
else
|
25
|
+
time || self.to_time
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def time_with_zone(time, zone)
|
32
|
+
if time
|
33
|
+
ActiveSupport::TimeWithZone.new(time.utc? ? time : time.getutc, zone)
|
34
|
+
else
|
35
|
+
ActiveSupport::TimeWithZone.new(nil, zone, to_time(:utc))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
@@ -0,0 +1,5 @@
|
|
1
|
+
require 'active_support/core_ext/date_time/acts_like'
|
2
|
+
require 'active_support/core_ext/date_time/blank'
|
3
|
+
require 'active_support/core_ext/date_time/calculations'
|
4
|
+
require 'active_support/core_ext/date_time/compatibility'
|
5
|
+
require 'active_support/core_ext/date_time/conversions'
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'active_support/core_ext/object/acts_like'
|
3
|
+
|
4
|
+
class DateTime
|
5
|
+
# Duck-types as a Date-like class. See Object#acts_like?.
|
6
|
+
def acts_like_date?
|
7
|
+
true
|
8
|
+
end
|
9
|
+
|
10
|
+
# Duck-types as a Time-like class. See Object#acts_like?.
|
11
|
+
def acts_like_time?
|
12
|
+
true
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,199 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
class DateTime
|
4
|
+
class << self
|
5
|
+
# Returns <tt>Time.zone.now.to_datetime</tt> when <tt>Time.zone</tt> or
|
6
|
+
# <tt>config.time_zone</tt> are set, otherwise returns
|
7
|
+
# <tt>Time.now.to_datetime</tt>.
|
8
|
+
def current
|
9
|
+
::Time.zone ? ::Time.zone.now.to_datetime : ::Time.now.to_datetime
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns the number of seconds since 00:00:00.
|
14
|
+
#
|
15
|
+
# DateTime.new(2012, 8, 29, 0, 0, 0).seconds_since_midnight # => 0
|
16
|
+
# DateTime.new(2012, 8, 29, 12, 34, 56).seconds_since_midnight # => 45296
|
17
|
+
# DateTime.new(2012, 8, 29, 23, 59, 59).seconds_since_midnight # => 86399
|
18
|
+
def seconds_since_midnight
|
19
|
+
sec + (min * 60) + (hour * 3600)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns the number of seconds until 23:59:59.
|
23
|
+
#
|
24
|
+
# DateTime.new(2012, 8, 29, 0, 0, 0).seconds_until_end_of_day # => 86399
|
25
|
+
# DateTime.new(2012, 8, 29, 12, 34, 56).seconds_until_end_of_day # => 41103
|
26
|
+
# DateTime.new(2012, 8, 29, 23, 59, 59).seconds_until_end_of_day # => 0
|
27
|
+
def seconds_until_end_of_day
|
28
|
+
end_of_day.to_i - to_i
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns the fraction of a second as a +Rational+
|
32
|
+
#
|
33
|
+
# DateTime.new(2012, 8, 29, 0, 0, 0.5).subsec # => (1/2)
|
34
|
+
def subsec
|
35
|
+
sec_fraction
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns a new DateTime where one or more of the elements have been changed
|
39
|
+
# according to the +options+ parameter. The time options (<tt>:hour</tt>,
|
40
|
+
# <tt>:min</tt>, <tt>:sec</tt>) reset cascadingly, so if only the hour is
|
41
|
+
# passed, then minute and sec is set to 0. If the hour and minute is passed,
|
42
|
+
# then sec is set to 0. The +options+ parameter takes a hash with any of these
|
43
|
+
# keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>,
|
44
|
+
# <tt>:min</tt>, <tt>:sec</tt>, <tt>:offset</tt>, <tt>:start</tt>.
|
45
|
+
#
|
46
|
+
# DateTime.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => DateTime.new(2012, 8, 1, 22, 35, 0)
|
47
|
+
# DateTime.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => DateTime.new(1981, 8, 1, 22, 35, 0)
|
48
|
+
# DateTime.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => DateTime.new(1981, 8, 29, 0, 0, 0)
|
49
|
+
def change(options)
|
50
|
+
::DateTime.civil(
|
51
|
+
options.fetch(:year, year),
|
52
|
+
options.fetch(:month, month),
|
53
|
+
options.fetch(:day, day),
|
54
|
+
options.fetch(:hour, hour),
|
55
|
+
options.fetch(:min, options[:hour] ? 0 : min),
|
56
|
+
options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec + sec_fraction),
|
57
|
+
options.fetch(:offset, offset),
|
58
|
+
options.fetch(:start, start)
|
59
|
+
)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Uses Date to provide precise Time calculations for years, months, and days.
|
63
|
+
# The +options+ parameter takes a hash with any of these keys: <tt>:years</tt>,
|
64
|
+
# <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>,
|
65
|
+
# <tt>:minutes</tt>, <tt>:seconds</tt>.
|
66
|
+
def advance(options)
|
67
|
+
unless options[:weeks].nil?
|
68
|
+
options[:weeks], partial_weeks = options[:weeks].divmod(1)
|
69
|
+
options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
|
70
|
+
end
|
71
|
+
|
72
|
+
unless options[:days].nil?
|
73
|
+
options[:days], partial_days = options[:days].divmod(1)
|
74
|
+
options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
|
75
|
+
end
|
76
|
+
|
77
|
+
d = to_date.advance(options)
|
78
|
+
datetime_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
|
79
|
+
seconds_to_advance = \
|
80
|
+
options.fetch(:seconds, 0) +
|
81
|
+
options.fetch(:minutes, 0) * 60 +
|
82
|
+
options.fetch(:hours, 0) * 3600
|
83
|
+
|
84
|
+
if seconds_to_advance.zero?
|
85
|
+
datetime_advanced_by_date
|
86
|
+
else
|
87
|
+
datetime_advanced_by_date.since(seconds_to_advance)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# Returns a new DateTime representing the time a number of seconds ago.
|
92
|
+
# Do not use this method in combination with x.months, use months_ago instead!
|
93
|
+
def ago(seconds)
|
94
|
+
since(-seconds)
|
95
|
+
end
|
96
|
+
|
97
|
+
# Returns a new DateTime representing the time a number of seconds since the
|
98
|
+
# instance time. Do not use this method in combination with x.months, use
|
99
|
+
# months_since instead!
|
100
|
+
def since(seconds)
|
101
|
+
self + Rational(seconds.round, 86400)
|
102
|
+
end
|
103
|
+
alias :in :since
|
104
|
+
|
105
|
+
# Returns a new DateTime representing the start of the day (0:00).
|
106
|
+
def beginning_of_day
|
107
|
+
change(:hour => 0)
|
108
|
+
end
|
109
|
+
alias :midnight :beginning_of_day
|
110
|
+
alias :at_midnight :beginning_of_day
|
111
|
+
alias :at_beginning_of_day :beginning_of_day
|
112
|
+
|
113
|
+
# Returns a new DateTime representing the middle of the day (12:00)
|
114
|
+
def middle_of_day
|
115
|
+
change(:hour => 12)
|
116
|
+
end
|
117
|
+
alias :midday :middle_of_day
|
118
|
+
alias :noon :middle_of_day
|
119
|
+
alias :at_midday :middle_of_day
|
120
|
+
alias :at_noon :middle_of_day
|
121
|
+
alias :at_middle_of_day :middle_of_day
|
122
|
+
|
123
|
+
# Returns a new DateTime representing the end of the day (23:59:59).
|
124
|
+
def end_of_day
|
125
|
+
change(:hour => 23, :min => 59, :sec => 59)
|
126
|
+
end
|
127
|
+
alias :at_end_of_day :end_of_day
|
128
|
+
|
129
|
+
# Returns a new DateTime representing the start of the hour (hh:00:00).
|
130
|
+
def beginning_of_hour
|
131
|
+
change(:min => 0)
|
132
|
+
end
|
133
|
+
alias :at_beginning_of_hour :beginning_of_hour
|
134
|
+
|
135
|
+
# Returns a new DateTime representing the end of the hour (hh:59:59).
|
136
|
+
def end_of_hour
|
137
|
+
change(:min => 59, :sec => 59)
|
138
|
+
end
|
139
|
+
alias :at_end_of_hour :end_of_hour
|
140
|
+
|
141
|
+
# Returns a new DateTime representing the start of the minute (hh:mm:00).
|
142
|
+
def beginning_of_minute
|
143
|
+
change(:sec => 0)
|
144
|
+
end
|
145
|
+
alias :at_beginning_of_minute :beginning_of_minute
|
146
|
+
|
147
|
+
# Returns a new DateTime representing the end of the minute (hh:mm:59).
|
148
|
+
def end_of_minute
|
149
|
+
change(:sec => 59)
|
150
|
+
end
|
151
|
+
alias :at_end_of_minute :end_of_minute
|
152
|
+
|
153
|
+
# Returns a <tt>Time</tt> instance of the simultaneous time in the system timezone.
|
154
|
+
def localtime(utc_offset = nil)
|
155
|
+
utc = new_offset(0)
|
156
|
+
|
157
|
+
Time.utc(
|
158
|
+
utc.year, utc.month, utc.day,
|
159
|
+
utc.hour, utc.min, utc.sec + utc.sec_fraction
|
160
|
+
).getlocal(utc_offset)
|
161
|
+
end
|
162
|
+
alias_method :getlocal, :localtime
|
163
|
+
|
164
|
+
# Returns a <tt>Time</tt> instance of the simultaneous time in the UTC timezone.
|
165
|
+
#
|
166
|
+
# DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)) # => Mon, 21 Feb 2005 10:11:12 -0600
|
167
|
+
# DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc # => Mon, 21 Feb 2005 16:11:12 UTC
|
168
|
+
def utc
|
169
|
+
utc = new_offset(0)
|
170
|
+
|
171
|
+
Time.utc(
|
172
|
+
utc.year, utc.month, utc.day,
|
173
|
+
utc.hour, utc.min, utc.sec + utc.sec_fraction
|
174
|
+
)
|
175
|
+
end
|
176
|
+
alias_method :getgm, :utc
|
177
|
+
alias_method :getutc, :utc
|
178
|
+
alias_method :gmtime, :utc
|
179
|
+
|
180
|
+
# Returns +true+ if <tt>offset == 0</tt>.
|
181
|
+
def utc?
|
182
|
+
offset == 0
|
183
|
+
end
|
184
|
+
|
185
|
+
# Returns the offset value in seconds.
|
186
|
+
def utc_offset
|
187
|
+
(offset * 86400).to_i
|
188
|
+
end
|
189
|
+
|
190
|
+
# Layers additional behavior on DateTime#<=> so that Time and
|
191
|
+
# ActiveSupport::TimeWithZone instances can be compared with a DateTime.
|
192
|
+
def <=>(other)
|
193
|
+
if other.respond_to? :to_datetime
|
194
|
+
super other.to_datetime rescue nil
|
195
|
+
else
|
196
|
+
super
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'active_support/core_ext/date_and_time/compatibility'
|
2
|
+
require 'active_support/core_ext/module/remove_method'
|
3
|
+
|
4
|
+
class DateTime
|
5
|
+
include DateAndTime::Compatibility
|
6
|
+
|
7
|
+
remove_possible_method :to_time
|
8
|
+
|
9
|
+
# Either return an instance of `Time` with the same UTC offset
|
10
|
+
# as +self+ or an instance of `Time` representing the same time
|
11
|
+
# in the the local system timezone depending on the setting of
|
12
|
+
# on the setting of +ActiveSupport.to_time_preserves_timezone+.
|
13
|
+
def to_time
|
14
|
+
preserve_timezone ? getlocal(utc_offset) : getlocal
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'active_support/inflector/methods'
|
3
|
+
require 'active_support/core_ext/time/conversions'
|
4
|
+
require 'active_support/core_ext/date_time/calculations'
|
5
|
+
require 'active_support/values/time_zone'
|
6
|
+
|
7
|
+
class DateTime
|
8
|
+
# Convert to a formatted string. See Time::DATE_FORMATS for predefined formats.
|
9
|
+
#
|
10
|
+
# This method is aliased to <tt>to_s</tt>.
|
11
|
+
#
|
12
|
+
# === Examples
|
13
|
+
# datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0) # => Tue, 04 Dec 2007 00:00:00 +0000
|
14
|
+
#
|
15
|
+
# datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00"
|
16
|
+
# datetime.to_s(:db) # => "2007-12-04 00:00:00"
|
17
|
+
# datetime.to_s(:number) # => "20071204000000"
|
18
|
+
# datetime.to_formatted_s(:short) # => "04 Dec 00:00"
|
19
|
+
# datetime.to_formatted_s(:long) # => "December 04, 2007 00:00"
|
20
|
+
# datetime.to_formatted_s(:long_ordinal) # => "December 4th, 2007 00:00"
|
21
|
+
# datetime.to_formatted_s(:rfc822) # => "Tue, 04 Dec 2007 00:00:00 +0000"
|
22
|
+
# datetime.to_formatted_s(:iso8601) # => "2007-12-04T00:00:00+00:00"
|
23
|
+
#
|
24
|
+
# == Adding your own datetime formats to to_formatted_s
|
25
|
+
# DateTime formats are shared with Time. You can add your own to the
|
26
|
+
# Time::DATE_FORMATS hash. Use the format name as the hash key and
|
27
|
+
# either a strftime string or Proc instance that takes a time or
|
28
|
+
# datetime argument as the value.
|
29
|
+
#
|
30
|
+
# # config/initializers/time_formats.rb
|
31
|
+
# Time::DATE_FORMATS[:month_and_year] = '%B %Y'
|
32
|
+
# Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") }
|
33
|
+
def to_formatted_s(format = :default)
|
34
|
+
if formatter = ::Time::DATE_FORMATS[format]
|
35
|
+
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
|
36
|
+
else
|
37
|
+
to_default_s
|
38
|
+
end
|
39
|
+
end
|
40
|
+
alias_method :to_default_s, :to_s if instance_methods(false).include?(:to_s)
|
41
|
+
alias_method :to_s, :to_formatted_s
|
42
|
+
|
43
|
+
# Returns a formatted string of the offset from UTC, or an alternative
|
44
|
+
# string if the time zone is already UTC.
|
45
|
+
#
|
46
|
+
# datetime = DateTime.civil(2000, 1, 1, 0, 0, 0, Rational(-6, 24))
|
47
|
+
# datetime.formatted_offset # => "-06:00"
|
48
|
+
# datetime.formatted_offset(false) # => "-0600"
|
49
|
+
def formatted_offset(colon = true, alternate_utc_string = nil)
|
50
|
+
utc? && alternate_utc_string || ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset, colon)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000".
|
54
|
+
def readable_inspect
|
55
|
+
to_s(:rfc822)
|
56
|
+
end
|
57
|
+
alias_method :default_inspect, :inspect
|
58
|
+
alias_method :inspect, :readable_inspect
|
59
|
+
|
60
|
+
# Returns DateTime with local offset for given year if format is local else
|
61
|
+
# offset is zero.
|
62
|
+
#
|
63
|
+
# DateTime.civil_from_format :local, 2012
|
64
|
+
# # => Sun, 01 Jan 2012 00:00:00 +0300
|
65
|
+
# DateTime.civil_from_format :local, 2012, 12, 17
|
66
|
+
# # => Mon, 17 Dec 2012 00:00:00 +0000
|
67
|
+
def self.civil_from_format(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0)
|
68
|
+
if utc_or_local.to_sym == :local
|
69
|
+
offset = ::Time.local(year, month, day).utc_offset.to_r / 86400
|
70
|
+
else
|
71
|
+
offset = 0
|
72
|
+
end
|
73
|
+
civil(year, month, day, hour, min, sec, offset)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Converts +self+ to a floating-point number of seconds, including fractional microseconds, since the Unix epoch.
|
77
|
+
def to_f
|
78
|
+
seconds_since_unix_epoch.to_f + sec_fraction
|
79
|
+
end
|
80
|
+
|
81
|
+
# Converts +self+ to an integer number of seconds since the Unix epoch.
|
82
|
+
def to_i
|
83
|
+
seconds_since_unix_epoch.to_i
|
84
|
+
end
|
85
|
+
|
86
|
+
# Returns the fraction of a second as microseconds
|
87
|
+
def usec
|
88
|
+
(sec_fraction * 1_000_000).to_i
|
89
|
+
end
|
90
|
+
|
91
|
+
# Returns the fraction of a second as nanoseconds
|
92
|
+
def nsec
|
93
|
+
(sec_fraction * 1_000_000_000).to_i
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def offset_in_seconds
|
99
|
+
(offset * 86400).to_i
|
100
|
+
end
|
101
|
+
|
102
|
+
def seconds_since_unix_epoch
|
103
|
+
(jd - 2440588) * 86400 - offset_in_seconds + seconds_since_midnight
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module Digest
|
4
|
+
module UUID
|
5
|
+
DNS_NAMESPACE = "k\xA7\xB8\x10\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" #:nodoc:
|
6
|
+
URL_NAMESPACE = "k\xA7\xB8\x11\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" #:nodoc:
|
7
|
+
OID_NAMESPACE = "k\xA7\xB8\x12\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" #:nodoc:
|
8
|
+
X500_NAMESPACE = "k\xA7\xB8\x14\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" #:nodoc:
|
9
|
+
|
10
|
+
# Generates a v5 non-random UUID (Universally Unique IDentifier).
|
11
|
+
#
|
12
|
+
# Using Digest::MD5 generates version 3 UUIDs; Digest::SHA1 generates version 5 UUIDs.
|
13
|
+
# uuid_from_hash always generates the same UUID for a given name and namespace combination.
|
14
|
+
#
|
15
|
+
# See RFC 4122 for details of UUID at: http://www.ietf.org/rfc/rfc4122.txt
|
16
|
+
def self.uuid_from_hash(hash_class, uuid_namespace, name)
|
17
|
+
if hash_class == Digest::MD5
|
18
|
+
version = 3
|
19
|
+
elsif hash_class == Digest::SHA1
|
20
|
+
version = 5
|
21
|
+
else
|
22
|
+
raise ArgumentError, "Expected Digest::SHA1 or Digest::MD5, got #{hash_class.name}."
|
23
|
+
end
|
24
|
+
|
25
|
+
hash = hash_class.new
|
26
|
+
hash.update(uuid_namespace)
|
27
|
+
hash.update(name)
|
28
|
+
|
29
|
+
ary = hash.digest.unpack('NnnnnN')
|
30
|
+
ary[2] = (ary[2] & 0x0FFF) | (version << 12)
|
31
|
+
ary[3] = (ary[3] & 0x3FFF) | 0x8000
|
32
|
+
|
33
|
+
"%08x-%04x-%04x-%04x-%04x%08x" % ary
|
34
|
+
end
|
35
|
+
|
36
|
+
# Convenience method for uuid_from_hash using Digest::MD5.
|
37
|
+
def self.uuid_v3(uuid_namespace, name)
|
38
|
+
self.uuid_from_hash(Digest::MD5, uuid_namespace, name)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Convenience method for uuid_from_hash using Digest::SHA1.
|
42
|
+
def self.uuid_v5(uuid_namespace, name)
|
43
|
+
self.uuid_from_hash(Digest::SHA1, uuid_namespace, name)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Convenience method for SecureRandom.uuid.
|
47
|
+
def self.uuid_v4
|
48
|
+
SecureRandom.uuid
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|