Class: Date
Overview
Date
This new version of Date extension has been largely improved by porting some of the methods used by ActiveSupport. The old version already had much in common with the Active Support library, so it was decided to take it a step further in that direction for the sake of interoparability.
Hopefully most of these methods will find there way into Ruby’s own standard library eventually.
The biggest difference with ActiveSupport is the lack of many of the “English-esque” methods, and that we use #stamp with Date::FORMAT, instead of #to_formmated_s with Date::DATE_FORMATS. We do not override the standard #to_s method like ActiveSupport does.
Constant Summary collapse
- FORMAT =
{ :short => "%e %b", :long => "%B %e, %Y", :db => "%Y-%m-%d", :number => "%Y%m%d", :rfc822 => "%e %b %Y", :default => "%Y-%m-%d", nil => "%Y-%m-%d" }
Class Method Summary collapse
-
.current ⇒ Object
Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today.
-
.tomorrow ⇒ Object
Returns a new Date representing the date 1 day after today (i.e. tomorrow’s date).
-
.yesterday ⇒ Object
Returns a new Date representing the date 1 day ago (i.e. yesterday’s date).
Instance Method Summary collapse
-
#advance(options) ⇒ Object
Provides precise Date calculations for years, months, and days.
-
#ago(seconds) ⇒ Object
Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) and then subtracts the specified number of seconds.
-
#beginning_of_day ⇒ Object
(also: #midnight)
Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00).
-
#change(options) ⇒ Object
Returns a new Date where one or more of the elements have been changed according to the
options
parameter. -
#days_in_month ⇒ Object
Returns the number of days in the date’s month.
- #days_of_month ⇒ Object
-
#month_name ⇒ Object
Get the month name for this date object.
-
#since(seconds) ⇒ Object
(also: #in)
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.
-
#stamp(format = :default) ⇒ Object
Convert to a formatted string.
-
#to_date ⇒ Object
A method to keep Time, Date and DateTime instances interchangeable on conversions.
-
#to_datetime ⇒ Object
Converts a Date instance to a DateTime, where the time is set to the beginning of the day and UTC offset is set to 0.
-
#to_time(form = :local) ⇒ Object
Converts a Date instance to a Time, where the time is set to the beginning of the day.
-
#tomorrow ⇒ Object
Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time.
- #xmlschema ⇒ Object
-
#yesterday ⇒ Object
Convenience method which returns a new Date/DateTime representing the time 1 day ago.
Class Method Details
.current ⇒ Object
Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today.
43 44 45 |
# File 'lib/standard/facets/date.rb', line 43 def self.current ::Time.zone_default ? ::Time.zone.today : ::Date.today end |
Instance Method Details
#advance(options) ⇒ Object
Provides precise Date calculations for years, months, and days. The options
parameter takes a hash with any of these keys: :years
, :months
, :weeks
, :days
.
137 138 139 140 141 142 143 144 |
# File 'lib/standard/facets/date.rb', line 137 def advance() d = self d = d >> .delete(:years) * 12 if [:years] d = d >> .delete(:months) if [:months] d = d + .delete(:weeks) * 7 if [:weeks] d = d + .delete(:days) if [:days] d end |
#ago(seconds) ⇒ Object
Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) and then subtracts the specified number of seconds
163 164 165 |
# File 'lib/standard/facets/date.rb', line 163 def ago(seconds) to_time.since(-seconds) end |
#beginning_of_day ⇒ Object Also known as: midnight
Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00).
177 178 179 |
# File 'lib/standard/facets/date.rb', line 177 def beginning_of_day to_time end |
#change(options) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/standard/facets/date.rb', line 152 def change() ::Date.new( [:year] || self.year, [:month] || self.month, [:day] || self.day ) end |
#days_in_month ⇒ Object
Returns the number of days in the date’s month.
Date.new(2004,2).days_in_month #=> 29
CREDIT: Ken Kunz.
88 89 90 |
# File 'lib/standard/facets/date.rb', line 88 def days_in_month Date.civil(year, month, -1).day end |
#days_of_month ⇒ Object
92 93 94 |
# File 'lib/standard/facets/date.rb', line 92 def days_of_month (1..days_in_month).to_a end |
#month_name ⇒ Object
Get the month name for this date object
CREDIT: Benjamin Oakes
99 100 101 |
# File 'lib/standard/facets/date.rb', line 99 def month_name MONTHNAMES[self.month] end |
#since(seconds) ⇒ Object Also known as: in
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.
170 171 172 |
# File 'lib/standard/facets/date.rb', line 170 def since(seconds) to_time.since(seconds) end |
#stamp(format = :default) ⇒ Object
Convert to a formatted string. See DATE_FORMATS for predefined formats.
This method is aliased to to_s
.
date = Date.new(2007, 11, 10) # Sat, 10 Nov 2007
date.stamp(:db) # => "2007-11-10"
date.stamp(:short) # => "10 Nov"
date.stamp(:long) # => "November 10, 2007"
date.stamp(:rfc822) # => "10 Nov 2007"
Adding your own formats to stamp
You can add your own formats to the Date::FORMAT hash. Use the format name as the hash key and a strftime string as the value. Eg.
Date::FORMAT[:month_and_year] = "%B %Y"
121 122 123 124 125 126 127 |
# File 'lib/standard/facets/date.rb', line 121 def stamp(format=:default) if formatter = FORMAT[format] strftime(formatter) else to_s end end |
#to_date ⇒ Object
A method to keep Time, Date and DateTime instances interchangeable on conversions. In this case, it simply returns self
.
49 50 51 |
# File 'lib/standard/facets/date.rb', line 49 def to_date self end |
#to_datetime ⇒ Object
Converts a Date instance to a DateTime, where the time is set to the beginning of the day and UTC offset is set to 0.
date = Date.new(2007, 11, 10) # Sat, 10 Nov 2007
date.to_datetime # Sat, 10 Nov 2007 00:00:00 0000
59 60 61 |
# File 'lib/standard/facets/date.rb', line 59 def to_datetime ::DateTime.civil(year, month, day, 0, 0, 0, 0) end |
#to_time(form = :local) ⇒ Object
Converts a Date instance to a Time, where the time is set to the beginning of the day. The timezone can be either :local or :utc (default :local).
date = Date.new(2007, 11, 10) # Sat, 10 Nov 2007
date.to_time # Sat Nov 10 00:00:00 0800 2007
date.to_time(:local) # Sat Nov 10 00:00:00 0800 2007
date.to_time(:utc) # Sat Nov 10 00:00:00 UTC 2007
73 74 75 76 |
# File 'lib/standard/facets/date.rb', line 73 def to_time(form=:local) ::Time.send(form, year, month, day) ##::Time.send("#{form}_time", year, month, day) end |
#tomorrow ⇒ Object
Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time.
190 191 192 |
# File 'lib/standard/facets/date.rb', line 190 def tomorrow self + 1 end |
#xmlschema ⇒ Object
79 80 81 |
# File 'lib/standard/facets/date.rb', line 79 def xmlschema to_time.xmlschema end |
#yesterday ⇒ Object
Convenience method which returns a new Date/DateTime representing the time 1 day ago.
184 185 186 |
# File 'lib/standard/facets/date.rb', line 184 def yesterday self - 1 end |