calculations.rb 5.1 KB
Newer Older
1
require 'date'
2
require 'active_support/duration'
3
require 'active_support/core_ext/object/acts_like'
4 5
require 'active_support/core_ext/date/zones'
require 'active_support/core_ext/time/zones'
6
require 'active_support/core_ext/date_and_time/calculations'
7

8
class Date
9
  include DateAndTime::Calculations
10

11
  class << self
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    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

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

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

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

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

  # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
52
  # and then subtracts the specified number of seconds.
53
  def ago(seconds)
54
    in_time_zone.since(-seconds)
55 56 57 58 59
  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)
60
    in_time_zone.since(seconds)
61 62 63 64 65
  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
66
    in_time_zone
67 68 69 70 71
  end
  alias :midnight :beginning_of_day
  alias :at_midnight :beginning_of_day
  alias :at_beginning_of_day :beginning_of_day

A
Anatoli Makarevich 已提交
72 73 74 75 76 77 78 79 80 81
  # 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

82 83
  # 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
84
    in_time_zone.end_of_day
85
  end
A
Andrew White 已提交
86
  alias :at_end_of_day :end_of_day
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  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

108
  # Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
109 110
  # any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
  def advance(options)
111
    options = options.dup
112 113 114 115 116 117 118 119 120
    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 已提交
121
  # The +options+ parameter is a hash with a combination of these keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>.
122
  #
123 124
  #   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)
125 126
  def change(options)
    ::Date.new(
A
Alexey Gaziev 已提交
127 128 129
      options.fetch(:year, year),
      options.fetch(:month, month),
      options.fetch(:day, day)
130 131
    )
  end
132 133 134 135 136 137 138 139 140 141 142
  
  # 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)
      self.to_datetime <=> other
    else
      compare_without_coercion(other)
    end
  end
  alias_method :compare_without_coercion, :<=>
  alias_method :<=>, :compare_with_coercion
J
Jeremy Kemper 已提交
143
end