calculations.rb 5.2 KB
Newer Older
1
# frozen_string_literal: true
2

3
require "date"
4 5 6 7 8
require "active_support/duration"
require "active_support/core_ext/object/acts_like"
require "active_support/core_ext/date/zones"
require "active_support/core_ext/time/zones"
require "active_support/core_ext/date_and_time/calculations"
9

10
class Date
11
  include DateAndTime::Calculations
12

13
  class << self
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    attr_accessor :beginning_of_week_default

    # Returns the week start (e.g. :monday) for the current request, if this has been set (via Date.beginning_of_week=).
    # If <tt>Date.beginning_of_week</tt> has not been set for the current request, returns the week start specified in <tt>config.beginning_of_week</tt>.
    # If no config.beginning_of_week was specified, returns :monday.
    def beginning_of_week
      Thread.current[:beginning_of_week] || beginning_of_week_default || :monday
    end

    # Sets <tt>Date.beginning_of_week</tt> to a week start (e.g. :monday) for current request/thread.
    #
    # This method accepts any of the following day symbols:
    # :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday
    def beginning_of_week=(week_start)
      Thread.current[:beginning_of_week] = find_beginning_of_week!(week_start)
    end

31
    # Returns week start day symbol (e.g. :monday), or raises an +ArgumentError+ for invalid day symbol.
32
    def find_beginning_of_week!(week_start)
X
Xavier Noria 已提交
33
      raise ArgumentError, "Invalid beginning of week: #{week_start}" unless ::Date::DAYS_INTO_WEEK.key?(week_start)
34 35 36
      week_start
    end

37 38
    # Returns a new Date representing the date 1 day ago (i.e. yesterday's date).
    def yesterday
39
      ::Date.current.yesterday
40
    end
41 42 43

    # Returns a new Date representing the date 1 day after today (i.e. tomorrow's date).
    def tomorrow
44
      ::Date.current.tomorrow
45 46
    end

47
    # Returns Time.zone.today when <tt>Time.zone</tt> or <tt>config.time_zone</tt> are set, otherwise just returns Date.today.
48
    def current
49
      ::Time.zone ? ::Time.zone.today : ::Date.today
50 51 52 53
    end
  end

  # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
54
  # and then subtracts the specified number of seconds.
55
  def ago(seconds)
56
    in_time_zone.since(-seconds)
57 58 59 60 61
  end

  # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
  # and then adds the specified number of seconds
  def since(seconds)
62
    in_time_zone.since(seconds)
63 64 65 66 67
  end
  alias :in :since

  # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
  def beginning_of_day
68
    in_time_zone
69 70 71 72 73
  end
  alias :midnight :beginning_of_day
  alias :at_midnight :beginning_of_day
  alias :at_beginning_of_day :beginning_of_day

A
Anatoli Makarevich 已提交
74 75 76 77 78 79 80 81 82 83
  # Converts Date to a Time (or DateTime if necessary) with the time portion set to the middle of the day (12:00)
  def middle_of_day
    in_time_zone.middle_of_day
  end
  alias :midday :middle_of_day
  alias :noon :middle_of_day
  alias :at_midday :middle_of_day
  alias :at_noon :middle_of_day
  alias :at_middle_of_day :middle_of_day

84 85
  # Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
  def end_of_day
86
    in_time_zone.end_of_day
87
  end
A
Andrew White 已提交
88
  alias :at_end_of_day :end_of_day
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  def plus_with_duration(other) #:nodoc:
    if ActiveSupport::Duration === other
      other.since(self)
    else
      plus_without_duration(other)
    end
  end
  alias_method :plus_without_duration, :+
  alias_method :+, :plus_with_duration

  def minus_with_duration(other) #:nodoc:
    if ActiveSupport::Duration === other
      plus_with_duration(-other)
    else
      minus_without_duration(other)
    end
  end
  alias_method :minus_without_duration, :-
  alias_method :-, :minus_with_duration

110
  # Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
111 112
  # any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
  def advance(options)
113
    options = options.dup
114 115 116 117 118 119 120 121 122
    d = self
    d = d >> options.delete(:years) * 12 if options[:years]
    d = d >> options.delete(:months)     if options[:months]
    d = d +  options.delete(:weeks) * 7  if options[:weeks]
    d = d +  options.delete(:days)       if options[:days]
    d
  end

  # Returns a new Date where one or more of the elements have been changed according to the +options+ parameter.
V
Vijay Dev 已提交
123
  # The +options+ parameter is a hash with a combination of these keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>.
124
  #
125 126
  #   Date.new(2007, 5, 12).change(day: 1)               # => Date.new(2007, 5, 1)
  #   Date.new(2007, 5, 12).change(year: 2005, month: 1) # => Date.new(2005, 1, 12)
127 128
  def change(options)
    ::Date.new(
A
Alexey Gaziev 已提交
129 130 131
      options.fetch(:year, year),
      options.fetch(:month, month),
      options.fetch(:day, day)
132 133
    )
  end
134

135 136 137
  # Allow Date to be compared with Time by converting to DateTime and relying on the <=> from there.
  def compare_with_coercion(other)
    if other.is_a?(Time)
138
      to_datetime <=> other
139 140 141 142 143 144
    else
      compare_without_coercion(other)
    end
  end
  alias_method :compare_without_coercion, :<=>
  alias_method :<=>, :compare_with_coercion
145 146
  alias_method :before?, :<
  alias_method :after?, :>
J
Jeremy Kemper 已提交
147
end