Skip to content
This repository
Browse code

DateTime calculations analogous to the Date and Time extensions. Closes

#7693.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6303 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 543622959a85ae464dfce4e0658e96a2e7dd3dbb 1 parent bf6d4ce
Jeremy Kemper authored March 04, 2007
2  activesupport/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* DateTime calculations analogous to the Date and Time extensions.  #7693 [Geoff Buesing]
  4
+
3 5
 * Give DateTime correct .to_s implementations, lets it play nice with ActiveRecord quoting. [gbuesing]
4 6
 
5 7
 * Add File.atomic_write,  allows you to write large files in an atomic manner, preventing users from seeing half written files.  [Koz]
4  activesupport/lib/active_support/core_ext/date_time.rb
... ...
@@ -1,6 +1,8 @@
1 1
 require 'date'
2 2
 require "#{File.dirname(__FILE__)}/time/behavior"
  3
+require "#{File.dirname(__FILE__)}/date_time/calculations"
3 4
 require "#{File.dirname(__FILE__)}/date_time/conversions"
4 5
 
5 6
 DateTime.send(:include, ActiveSupport::CoreExtensions::Time::Behavior)
6  
-DateTime.send(:include, ActiveSupport::CoreExtensions::DateTime::Conversions)
  7
+DateTime.send(:include, ActiveSupport::CoreExtensions::DateTime::Calculations)
  8
+DateTime.send(:include, ActiveSupport::CoreExtensions::DateTime::Conversions)
173  activesupport/lib/active_support/core_ext/date_time/calculations.rb
... ...
@@ -0,0 +1,173 @@
  1
+require 'rational'
  2
+
  3
+module ActiveSupport #:nodoc:
  4
+  module CoreExtensions #:nodoc:
  5
+    module DateTime #:nodoc:
  6
+      # Enables the use of time calculations within DateTime itself
  7
+      module Calculations
  8
+
  9
+        # Seconds since midnight: DateTime.now.seconds_since_midnight
  10
+        def seconds_since_midnight
  11
+          self.sec + (self.min * 60) + (self.hour * 3600)
  12
+        end
  13
+
  14
+        # Returns a new DateTime where one or more of the elements have been changed according to the +options+ parameter. The time options
  15
+        # (hour, minute, sec) reset cascadingly, so if only the hour is passed, then minute and sec is set to 0. If the hour and
  16
+        # minute is passed, then sec is set to 0.
  17
+        def change(options)
  18
+          ::DateTime.civil(
  19
+            options[:year]  || self.year,
  20
+            options[:month] || self.month,
  21
+            options[:mday]  || self.mday,
  22
+            options[:hour]  || self.hour,
  23
+            options[:min]   || (options[:hour] ? 0 : self.min),
  24
+            options[:sec]   || ((options[:hour] || options[:min]) ? 0 : self.sec),
  25
+            options[:offset]  || self.offset,
  26
+            options[:start]  || self.start
  27
+          )
  28
+        end
  29
+
  30
+        # Uses Date to provide precise Time calculations for years, months, and days.  The +options+ parameter takes a hash with
  31
+        # any of these keys: :months, :days, :years.
  32
+        def advance(options)
  33
+          d = ::Date.new(year + (options.delete(:years) || 0), month, day)
  34
+          d = d >> options.delete(:months) if options[:months]
  35
+          d = d +  options.delete(:days)   if options[:days]
  36
+          change(options.merge(:year => d.year, :month => d.month, :mday => d.day))
  37
+        end
  38
+
  39
+        # Returns a new DateTime representing the time a number of seconds ago
  40
+        # Do not use this method in combination with x.months, use months_ago instead!
  41
+        def ago(seconds)
  42
+          self.since(-seconds)
  43
+        end
  44
+
  45
+        # Returns a new DateTime representing the time a number of seconds since the instance time
  46
+        # Do not use this method in combination with x.months, use months_since instead!
  47
+        def since(seconds)
  48
+          self + Rational(seconds, 86400)
  49
+        end
  50
+        alias :in :since
  51
+
  52
+        # Returns a new DateTime representing the time a number of specified months ago
  53
+         def months_ago(months)
  54
+           months_since(-months)
  55
+         end
  56
+
  57
+         def months_since(months)
  58
+           year, month, mday = self.year, self.month, self.mday
  59
+
  60
+           month += months
  61
+
  62
+           # in case months is negative
  63
+           while month < 1
  64
+             month += 12
  65
+             year -= 1
  66
+           end
  67
+
  68
+           # in case months is positive
  69
+           while month > 12
  70
+             month -= 12
  71
+             year += 1
  72
+           end
  73
+
  74
+           max = ::Time.days_in_month(month, year)
  75
+           mday = max if mday > max
  76
+
  77
+           change(:year => year, :month => month, :mday => mday)
  78
+         end
  79
+
  80
+         # Returns a new DateTime representing the time a number of specified years ago
  81
+         def years_ago(years)
  82
+           change(:year => self.year - years)
  83
+         end
  84
+
  85
+         def years_since(years)
  86
+           change(:year => self.year + years)
  87
+         end
  88
+
  89
+         # Short-hand for years_ago(1)
  90
+         def last_year
  91
+           years_ago(1)
  92
+         end
  93
+
  94
+         # Short-hand for years_since(1)
  95
+         def next_year
  96
+           years_since(1)
  97
+         end
  98
+
  99
+         # Short-hand for months_ago(1)
  100
+         def last_month
  101
+           months_ago(1)
  102
+         end
  103
+
  104
+         # Short-hand for months_since(1)
  105
+         def next_month
  106
+           months_since(1)
  107
+         end
  108
+
  109
+         # Returns a new DateTime representing the "start" of this week (Monday, 0:00)
  110
+         def beginning_of_week
  111
+           days_to_monday = self.wday!=0 ? self.wday-1 : 6
  112
+           (self - days_to_monday).midnight
  113
+         end
  114
+         alias :monday :beginning_of_week
  115
+         alias :at_beginning_of_week :beginning_of_week
  116
+
  117
+         # Returns a new DateTime representing the start of the given day in next week (default is Monday).
  118
+         def next_week(day = :monday)
  119
+           days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
  120
+           ((self + 7).beginning_of_week + days_into_week[day]).change(:hour => 0)
  121
+         end
  122
+
  123
+         # Returns a new DateTime representing the start of the day (0:00)
  124
+         def beginning_of_day
  125
+           change(:hour => 0)
  126
+         end
  127
+         alias :midnight :beginning_of_day
  128
+         alias :at_midnight :beginning_of_day
  129
+         alias :at_beginning_of_day :beginning_of_day
  130
+
  131
+         # Returns a new DateTime representing the end of the day (23:59:59)
  132
+         def end_of_day
  133
+           change(:hour => 23, :min => 59, :sec => 59)
  134
+         end
  135
+
  136
+         # Returns a new DateTime representing the start of the month (1st of the month, 0:00)
  137
+         def beginning_of_month
  138
+           change(:mday => 1,:hour => 0, :min => 0, :sec => 0)
  139
+         end
  140
+         alias :at_beginning_of_month :beginning_of_month
  141
+
  142
+         # Returns a new DateTime representing the end of the month (last day of the month, 0:00)
  143
+         def end_of_month
  144
+           last_day = ::Time.days_in_month( self.month, self.year )
  145
+           change(:mday => last_day,:hour => 0, :min => 0, :sec => 0)
  146
+         end
  147
+         alias :at_end_of_month :end_of_month
  148
+
  149
+         # Returns  a new DateTime representing the start of the quarter (1st of january, april, july, october, 0:00)
  150
+         def beginning_of_quarter
  151
+           beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
  152
+         end
  153
+         alias :at_beginning_of_quarter :beginning_of_quarter
  154
+
  155
+         # Returns  a new DateTime representing the start of the year (1st of january, 0:00)
  156
+         def beginning_of_year
  157
+           change(:month => 1,:mday => 1,:hour => 0, :min => 0, :sec => 0)
  158
+         end
  159
+         alias :at_beginning_of_year :beginning_of_year
  160
+
  161
+         # Convenience method which returns a new DateTime representing the time 1 day ago
  162
+         def yesterday
  163
+           self - 1
  164
+         end
  165
+
  166
+         # Convenience method which returns a new DateTime representing the time 1 day since the instance time
  167
+         def tomorrow
  168
+           self + 1
  169
+         end
  170
+      end
  171
+    end
  172
+  end
  173
+end
139  activesupport/test/core_ext/date_time_ext_test.rb
@@ -12,4 +12,141 @@ def test_to_s
12 12
   def test_to_date
13 13
     assert_equal Date.new(2005, 2, 21), DateTime.new(2005, 2, 21).to_date
14 14
   end
15  
-end
  15
+
  16
+  def test_seconds_since_midnight
  17
+    assert_equal 1,DateTime.civil(2005,1,1,0,0,1).seconds_since_midnight
  18
+    assert_equal 60,DateTime.civil(2005,1,1,0,1,0).seconds_since_midnight
  19
+    assert_equal 3660,DateTime.civil(2005,1,1,1,1,0).seconds_since_midnight
  20
+    assert_equal 86399,DateTime.civil(2005,1,1,23,59,59).seconds_since_midnight
  21
+  end
  22
+
  23
+  def test_begining_of_week
  24
+    assert_equal DateTime.civil(2005,1,31), DateTime.civil(2005,2,4,10,10,10).beginning_of_week
  25
+    assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,28,0,0,0).beginning_of_week #monday
  26
+    assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,29,0,0,0).beginning_of_week #tuesday
  27
+    assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,30,0,0,0).beginning_of_week #wednesday
  28
+    assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,01,0,0,0).beginning_of_week #thursday
  29
+    assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,02,0,0,0).beginning_of_week #friday
  30
+    assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,03,0,0,0).beginning_of_week #saturday
  31
+    assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,04,0,0,0).beginning_of_week #sunday
  32
+  end
  33
+
  34
+  def test_beginning_of_day
  35
+    assert_equal DateTime.civil(2005,2,4,0,0,0), DateTime.civil(2005,2,4,10,10,10).beginning_of_day
  36
+  end
  37
+
  38
+  def test_beginning_of_month
  39
+    assert_equal DateTime.civil(2005,2,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_month
  40
+  end
  41
+
  42
+  def test_beginning_of_quarter
  43
+    assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,2,15,10,10,10).beginning_of_quarter
  44
+    assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,1,1,0,0,0).beginning_of_quarter
  45
+    assert_equal DateTime.civil(2005,10,1,0,0,0), DateTime.civil(2005,12,31,10,10,10).beginning_of_quarter
  46
+    assert_equal DateTime.civil(2005,4,1,0,0,0), DateTime.civil(2005,6,30,23,59,59).beginning_of_quarter
  47
+  end
  48
+
  49
+  def test_end_of_month
  50
+    assert_equal DateTime.civil(2005,3,31,0,0,0), DateTime.civil(2005,3,20,10,10,10).end_of_month
  51
+    assert_equal DateTime.civil(2005,2,28,0,0,0), DateTime.civil(2005,2,20,10,10,10).end_of_month
  52
+    assert_equal DateTime.civil(2005,4,30,0,0,0), DateTime.civil(2005,4,20,10,10,10).end_of_month
  53
+
  54
+  end
  55
+
  56
+  def test_beginning_of_year
  57
+    assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_year
  58
+  end
  59
+
  60
+  def test_months_ago
  61
+    assert_equal DateTime.civil(2005,5,5,10),  DateTime.civil(2005,6,5,10,0,0).months_ago(1)
  62
+    assert_equal DateTime.civil(2004,11,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(7)
  63
+    assert_equal DateTime.civil(2004,12,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(6)
  64
+    assert_equal DateTime.civil(2004,6,5,10),  DateTime.civil(2005,6,5,10,0,0).months_ago(12)
  65
+    assert_equal DateTime.civil(2003,6,5,10),  DateTime.civil(2005,6,5,10,0,0).months_ago(24)
  66
+  end
  67
+
  68
+  def test_months_since
  69
+    assert_equal DateTime.civil(2005,7,5,10),  DateTime.civil(2005,6,5,10,0,0).months_since(1)
  70
+    assert_equal DateTime.civil(2006,1,5,10),  DateTime.civil(2005,12,5,10,0,0).months_since(1)
  71
+    assert_equal DateTime.civil(2005,12,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(6)
  72
+    assert_equal DateTime.civil(2006,6,5,10),  DateTime.civil(2005,12,5,10,0,0).months_since(6)
  73
+    assert_equal DateTime.civil(2006,1,5,10),  DateTime.civil(2005,6,5,10,0,0).months_since(7)
  74
+    assert_equal DateTime.civil(2006,6,5,10),  DateTime.civil(2005,6,5,10,0,0).months_since(12)
  75
+    assert_equal DateTime.civil(2007,6,5,10),  DateTime.civil(2005,6,5,10,0,0).months_since(24)
  76
+  end
  77
+
  78
+  def test_years_ago
  79
+    assert_equal DateTime.civil(2004,6,5,10),  DateTime.civil(2005,6,5,10,0,0).years_ago(1)
  80
+    assert_equal DateTime.civil(1998,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(7)
  81
+  end
  82
+
  83
+  def test_years_since
  84
+    assert_equal DateTime.civil(2006,6,5,10),  DateTime.civil(2005,6,5,10,0,0).years_since(1)
  85
+    assert_equal DateTime.civil(2012,6,5,10),  DateTime.civil(2005,6,5,10,0,0).years_since(7)
  86
+    assert_equal DateTime.civil(2182,6,5,10),  DateTime.civil(2005,6,5,10,0,0).years_since(177)
  87
+  end
  88
+
  89
+  def test_last_year
  90
+    assert_equal DateTime.civil(2004,6,5,10),  DateTime.civil(2005,6,5,10,0,0).last_year
  91
+  end
  92
+
  93
+
  94
+  def test_ago
  95
+    assert_equal DateTime.civil(2005,2,22,10,10,9),  DateTime.civil(2005,2,22,10,10,10).ago(1)
  96
+    assert_equal DateTime.civil(2005,2,22,9,10,10),  DateTime.civil(2005,2,22,10,10,10).ago(3600)
  97
+    assert_equal DateTime.civil(2005,2,20,10,10,10), DateTime.civil(2005,2,22,10,10,10).ago(86400*2)
  98
+    assert_equal DateTime.civil(2005,2,20,9,9,45),   DateTime.civil(2005,2,22,10,10,10).ago(86400*2 + 3600 + 25)
  99
+  end
  100
+
  101
+  def test_since
  102
+    assert_equal DateTime.civil(2005,2,22,10,10,11), DateTime.civil(2005,2,22,10,10,10).since(1)
  103
+    assert_equal DateTime.civil(2005,2,22,11,10,10), DateTime.civil(2005,2,22,10,10,10).since(3600)
  104
+    assert_equal DateTime.civil(2005,2,24,10,10,10), DateTime.civil(2005,2,22,10,10,10).since(86400*2)
  105
+    assert_equal DateTime.civil(2005,2,24,11,10,35), DateTime.civil(2005,2,22,10,10,10).since(86400*2 + 3600 + 25)
  106
+  end
  107
+
  108
+  def test_yesterday
  109
+    assert_equal DateTime.civil(2005,2,21,10,10,10), DateTime.civil(2005,2,22,10,10,10).yesterday
  110
+    assert_equal DateTime.civil(2005,2,28,10,10,10), DateTime.civil(2005,3,2,10,10,10).yesterday.yesterday
  111
+  end
  112
+
  113
+  def test_tomorrow
  114
+    assert_equal DateTime.civil(2005,2,23,10,10,10), DateTime.civil(2005,2,22,10,10,10).tomorrow
  115
+    assert_equal DateTime.civil(2005,3,2,10,10,10),  DateTime.civil(2005,2,28,10,10,10).tomorrow.tomorrow
  116
+  end
  117
+
  118
+  def test_change
  119
+    assert_equal DateTime.civil(2006,2,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:year => 2006)
  120
+    assert_equal DateTime.civil(2005,6,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:month => 6)
  121
+    assert_equal DateTime.civil(2012,9,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:year => 2012, :month => 9)
  122
+    assert_equal DateTime.civil(2005,2,22,16),       DateTime.civil(2005,2,22,15,15,10).change(:hour => 16)
  123
+    assert_equal DateTime.civil(2005,2,22,16,45),    DateTime.civil(2005,2,22,15,15,10).change(:hour => 16, :min => 45)
  124
+    assert_equal DateTime.civil(2005,2,22,15,45),    DateTime.civil(2005,2,22,15,15,10).change(:min => 45)
  125
+  end
  126
+
  127
+  def test_plus
  128
+    assert_equal DateTime.civil(2006,2,28,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:years => 1)
  129
+    assert_equal DateTime.civil(2005,6,28,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:months => 4)
  130
+    assert_equal DateTime.civil(2012,9,28,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:years => 7, :months => 7)
  131
+    assert_equal DateTime.civil(2013,10,3,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :days => 5)
  132
+  end
  133
+
  134
+  def test_next_week
  135
+    assert_equal DateTime.civil(2005,2,28), DateTime.civil(2005,2,22,15,15,10).next_week
  136
+    assert_equal DateTime.civil(2005,3,4), DateTime.civil(2005,2,22,15,15,10).next_week(:friday)
  137
+    assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,10,23,0,0,0).next_week
  138
+    assert_equal DateTime.civil(2006,11,1), DateTime.civil(2006,10,23,0,0,0).next_week(:wednesday)
  139
+  end
  140
+
  141
+  def test_next_month_on_31st
  142
+    assert_equal DateTime.civil(2005, 9, 30), DateTime.civil(2005, 8, 31).next_month
  143
+  end
  144
+
  145
+  def test_last_month_on_31st
  146
+    assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month
  147
+  end
  148
+
  149
+  def test_xmlschema_is_available
  150
+    assert_nothing_raised { DateTime.now.xmlschema }
  151
+  end
  152
+end

0 notes on commit 5436229

Please sign in to comment.
Something went wrong with that request. Please try again.