Class: TimeCrisis::Date
- Extended by:
- TimeCrisis::DateRange::Date::ClassMethods, MeteorologicalSeasons::ClassMethods, NamedMonths, NthWeekday::ClassMethods
- Includes:
- Comparable
- Defined in:
- lib/time_crisis/date.rb,
lib/time_crisis/to_json.rb,
lib/time_crisis/support/ext/date/acts_like.rb,
lib/time_crisis/support/ext/date/conversions.rb,
lib/time_crisis/support/ext/date/calculations.rb
Overview
TimeCrisis’s date class, a simple class which, unlike the standard Date class, does not include any time information. Taken from ThirdBase by Jeremy Evans.
This class is significantly faster than the standard Date class for two reasons. First, it does not depend on the Rational class (which is slow). Second, it doesn’t convert all dates to julian dates unless it is necessary.
Direct Known Subclasses
Constant Summary collapse
- MONTHNAMES =
[nil] + %w(January February March April May June July August September October November December)
- ABBR_MONTHNAMES =
[nil] + %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
- MONTH_NUM_MAP =
{}
- DAYNAMES =
%w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
- ABBR_DAYNAMES =
%w(Sun Mon Tue Wed Thu Fri Sat)
- DAY_NUM_MAP =
{}
- CUMMULATIVE_MONTH_DAYS =
{1=>0, 2=>31, 3=>59, 4=>90, 5=>120, 6=>151, 7=>181, 8=>212, 9=>243, 10=>273, 11=>304, 12=>334}
- LEAP_CUMMULATIVE_MONTH_DAYS =
{1=>0, 2=>31, 3=>60, 4=>91, 5=>121, 6=>152, 7=>182, 8=>213, 9=>244, 10=>274, 11=>305, 12=>335}
- DAYS_IN_MONTH =
{1=>31, 2=>28, 3=>31, 4=>30, 5=>31, 6=>30, 7=>31, 8=>31, 9=>30, 10=>31, 11=>30, 12=>31}
- LEAP_DAYS_IN_MONTH =
{1=>31, 2=>29, 3=>31, 4=>30, 5=>31, 6=>30, 7=>31, 8=>31, 9=>30, 10=>31, 11=>30, 12=>31}
- MONTHNAME_RE_PATTERN =
"(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|january|february|march|april|may|june|july|august|september|october|november|december)"
- FULL_MONTHNAME_RE_PATTERN =
"(january|february|march|april|may|june|july|august|september|october|november|december)"
- ABBR_MONTHNAME_RE_PATTERN =
"(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)"
- FULL_DAYNAME_RE_PATTERN =
"(sunday|monday|tuesday|wednesday|thursday|friday|saturday)"
- ABBR_DAYNAME_RE_PATTERN =
"(sun|mon|tue|wed|thu|fri|sat)"
- PARSER_LIST =
[]
- DEFAULT_PARSER_LIST =
[:iso, :us, :num]
- PARSERS =
{}
- DEFAULT_PARSERS =
{}
- STRFTIME_RE =
/%./o
- STRPTIME_PROC_A =
proc {|h,x| h[:cwday] = DAY_NUM_MAP[x.downcase]}
- STRPTIME_PROC_B =
proc {|h,x| h[:month] = MONTH_NUM_MAP[x.downcase]}
- STRPTIME_PROC_C =
proc {|h,x| h[:year] ||= x.to_i*100}
- STRPTIME_PROC_d =
proc {|h,x| h[:day] = x.to_i}
- STRPTIME_PROC_G =
proc {|h,x| h[:cwyear] = x.to_i}
- STRPTIME_PROC_g =
proc {|h,x| h[:cwyear] = two_digit_year(x)}
- STRPTIME_PROC_j =
proc {|h,x| h[:yday] = x.to_i}
- STRPTIME_PROC_m =
proc {|h,x| h[:month] = x.to_i}
- STRPTIME_PROC_u =
proc {|h,x| h[:cwday] = x.to_i}
- STRPTIME_PROC_V =
proc {|h,x| h[:cweek] = x.to_i}
- STRPTIME_PROC_y =
proc {|h,x| h[:year] = two_digit_year(x)}
- STRPTIME_PROC_Y =
proc {|h,x| h[:year] = x.to_i}
- UNIXEPOCH =
2440588
- DATE_FORMATS =
{ :short => "%e %b", :long => "%B %e, %Y", :db => "%Y-%m-%d", :number => "%Y%m%d", :rfc822 => "%e %b %Y" }
Class Method Summary collapse
-
.add_parser(type, pattern, &block) ⇒ Object
Add a parser to the parser type.
-
.add_parser_type(type) ⇒ Object
Add a parser type to the list of parser types.
-
.civil(year, mon, day) ⇒ Object
Returns a new Date with the given year, month, and day.
-
.commercial(cwyear, cweek, cwday = 5) ⇒ Object
Returns a new Date with the given commercial week year, commercial week, and commercial week day.
- .current ⇒ Object
-
.jd(j) ⇒ Object
Returns a new Date with the given julian date.
-
.new(*args) ⇒ Object
Calls civil with the given arguments.
- .new! ⇒ Object
-
.ordinal(year, yday) ⇒ Object
Returns a new Date with the given year and day of year.
-
.parse(str, opts = {}) ⇒ Object
Parses the given string and returns a Date.
-
.reset_parsers! ⇒ Object
Reset the parsers, parser types, and order of parsers used to the default.
-
.strptime(str, fmt = strptime_default) ⇒ Object
Parse the string using the provided format (or the default format).
-
.today ⇒ Object
Returns a date with the current year, month, and date.
- .tomorrow ⇒ Object
-
.use_parsers(*parsers) ⇒ Object
Set the order of parser types to use to the given parser types.
- .yesterday ⇒ Object
Instance Method Summary collapse
-
#<<(m) ⇒ Object
Returns a new date with m number of months subtracted from this date.
-
#<=>(date) ⇒ Object
Compare two dates.
-
#==(date) ⇒ Object
(also: #eql?)
Dates are equel only if their year, month, and day match.
-
#===(d) ⇒ Object
If d is a date, only true if it is equal to this date.
-
#>>(m) ⇒ Object
Returns a new date with m number of months added to this date.
- #acts_like_date? ⇒ Boolean
- #advance(options) ⇒ Object
- #ago(seconds) ⇒ Object
- #beginning_of_day ⇒ Object (also: #midnight, #at_midnight, #at_beginning_of_day)
- #beginning_of_month ⇒ Object (also: #at_beginning_of_month)
- #beginning_of_quarter ⇒ Object (also: #at_beginning_of_quarter)
- #beginning_of_week ⇒ Object (also: #monday, #at_beginning_of_week)
- #beginning_of_year ⇒ Object (also: #at_beginning_of_year)
- #change(options) ⇒ Object
-
#cwday ⇒ Object
The commercial week day for this date.
-
#cweek ⇒ Object
The commercial week for this date.
-
#cwyear ⇒ Object
The commercial week year for this date.
-
#day ⇒ Object
(also: #mday)
The day of the month for this date.
- #days_in_month(m = nil, y = nil) ⇒ Object
-
#downto(d, &block) ⇒ Object
Yield every date between this date and given date to the block.
- #end_of_day ⇒ Object
- #end_of_month ⇒ Object (also: #at_end_of_month)
- #end_of_quarter ⇒ Object (also: #at_end_of_quarter)
- #end_of_week ⇒ Object (also: #at_end_of_week)
- #end_of_year ⇒ Object (also: #at_end_of_year)
- #future? ⇒ Boolean
-
#hash ⇒ Object
Unique value for this date, based on it’s year, month, and day of month.
-
#initialize(opts) ⇒ Date
constructor
Called by Date.new!, Takes a hash with one of the following keys:.
-
#jd ⇒ Object
This date’s julian date.
- #last_month ⇒ Object
- #last_year ⇒ Object
-
#leap? ⇒ Boolean
Whether this date is in a leap year.
-
#minus_with_duration(other) ⇒ Object
(also: #-)
:nodoc:.
-
#mon ⇒ Object
(also: #month)
The month number for this date (January is 1, December is 12).
- #months_ago(months) ⇒ Object
- #months_since(months) ⇒ Object
- #next_month ⇒ Object
- #next_week(day = :monday) ⇒ Object
- #next_year ⇒ Object
- #past? ⇒ Boolean
-
#plus_with_duration(other) ⇒ Object
(also: #+)
:nodoc:.
- #readable_inspect ⇒ Object (also: #inspect)
- #since(seconds) ⇒ Object (also: #in)
-
#step(limit, step = 1) ⇒ Object
Yield each date between this date and limit, adding step number of days in each iteration.
-
#strftime(fmt = strftime_default) ⇒ Object
Format the time using a format string, or the default format string.
-
#succ ⇒ Object
(also: #next)
Return the day after this date.
- #to_date ⇒ Object
- #to_datetime ⇒ Object
- #to_formatted_s(format = :default) ⇒ Object (also: #to_s)
- #to_json(options = nil) ⇒ Object
- #to_tc_date ⇒ Object
- #to_tc_datetime(form = :local) ⇒ Object
- #to_tc_time(form = :local) ⇒ Object
- #to_time(form = :local) ⇒ Object
- #today? ⇒ Boolean
- #tomorrow ⇒ Object
-
#upto(d, &block) ⇒ Object
Yield every date between this date and the given date to the block.
-
#wday ⇒ Object
Return the day of the week for this date.
- #xmlschema ⇒ Object
-
#yday ⇒ Object
Return the day of the year for this date.
-
#year ⇒ Object
Return the year for this date.
- #years_ago(years) ⇒ Object
- #years_since(years) ⇒ Object
- #yesterday ⇒ Object
Methods included from TimeCrisis::DateRange::Date::ClassMethods
Methods included from NthWeekday::ClassMethods
Methods included from NamedMonths
april, august, december, february, january, july, june, march, may, month_range, november, october, september
Methods included from MeteorologicalSeasons::ClassMethods
beginning_of_meteorological_autumn, beginning_of_meteorological_spring, beginning_of_meteorological_summer, beginning_of_meteorological_winter
Constructor Details
#initialize(opts) ⇒ Date
Called by Date.new!, Takes a hash with one of the following keys:
-
:civil : should be an array with 3 elements, a year, month, and day
-
:commercial : should be an array with 3 elements, a commercial week year, commercial week, and commercial week day
-
:jd : should be an integer specifying the julian date
-
:ordinal : should be an array with 2 elements, a year and day of year.
An ArgumentError is raised if the date is invalid. All Date objects are immutable once created.
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/time_crisis/date.rb', line 333 def initialize(opts) if opts[:civil] @year, @mon, @day = opts[:civil] raise(ArgumentError, "invalid date") unless @year.is_a?(Integer) && @mon.is_a?(Integer) && @day.is_a?(Integer) && valid_civil? elsif opts[:ordinal] @year, @yday = opts[:ordinal] raise(ArgumentError, "invalid date") unless @year.is_a?(Integer) && @yday.is_a?(Integer) && valid_ordinal? elsif opts[:jd] @jd = opts[:jd] raise(ArgumentError, "invalid date") unless @jd.is_a?(Integer) elsif opts[:commercial] @cwyear, @cweek, @cwday = opts[:commercial] raise(ArgumentError, "invalid date") unless @cwyear.is_a?(Integer) && @cweek.is_a?(Integer) && @cwday.is_a?(Integer) && valid_commercial? else raise(ArgumentError, "invalid date format") end end |
Class Method Details
.add_parser(type, pattern, &block) ⇒ Object
Add a parser to the parser type. Arguments:
-
type - The parser type to which to add the parser, should be a Symbol.
-
pattern - Can be either a Regexp or String:
-
String - A strptime parser regular expression is created using pattern as the format string. If a block is given, it is used. If no block is given, the parser will operate identically to strptime.
-
Regexp - The regular expression is used directly. In this case, a block must be provided, or an error is raised.
-
The block, if provided, should take a single MatchData argument. It should return nil if it cannot successfully parse the string, an instance of this class, or a hash of values to be passed to new!.
109 110 111 112 113 114 115 116 117 |
# File 'lib/time_crisis/date.rb', line 109 def self.add_parser(type, pattern, &block) if pattern.is_a?(String) pattern, blk = strptime_pattern_and_block(pattern) block ||= blk else raise(ArgumentError, 'must provide block for Regexp parser') unless block_given? end parser_hash[type].unshift([pattern, block]) end |
.add_parser_type(type) ⇒ Object
Add a parser type to the list of parser types. Should be used if you want to add your own parser types.
122 123 124 |
# File 'lib/time_crisis/date.rb', line 122 def self.add_parser_type(type) parser_hash[type] ||= [] end |
.civil(year, mon, day) ⇒ Object
Returns a new Date with the given year, month, and day.
127 128 129 |
# File 'lib/time_crisis/date.rb', line 127 def self.civil(year, mon, day) new!(:civil=>[year, mon, day]) end |
.commercial(cwyear, cweek, cwday = 5) ⇒ Object
Returns a new Date with the given commercial week year, commercial week, and commercial week day.
133 134 135 |
# File 'lib/time_crisis/date.rb', line 133 def self.commercial(cwyear, cweek, cwday=5) new!(:commercial=>[cwyear, cweek, cwday]) end |
.current ⇒ Object
15 16 17 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 15 def current ::TimeCrisis::Time.zone_default ? ::TimeCrisis::Time.zone.today : ::TimeCrisis::Date.today end |
.jd(j) ⇒ Object
Returns a new Date with the given julian date.
138 139 140 |
# File 'lib/time_crisis/date.rb', line 138 def self.jd(j) new!(:jd=>j) end |
.new(*args) ⇒ Object
Calls civil with the given arguments.
143 144 145 |
# File 'lib/time_crisis/date.rb', line 143 def self.new(*args) civil(*args) end |
.new! ⇒ Object
94 |
# File 'lib/time_crisis/date.rb', line 94 alias new! new |
.ordinal(year, yday) ⇒ Object
Returns a new Date with the given year and day of year.
148 149 150 |
# File 'lib/time_crisis/date.rb', line 148 def self.ordinal(year, yday) new!(:ordinal=>[year, yday]) end |
.parse(str, opts = {}) ⇒ Object
Parses the given string and returns a Date. Raises an ArgumentError if no parser can correctly parse the date. Takes the following options:
-
:parser_types : an array of parser types to use, overriding the default or the ones specified by use_parsers.
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/time_crisis/date.rb', line 158 def self.parse(str, opts={}) s = str.strip parsers(opts[:parser_types]) do |pattern, block| if m = pattern.match(s) if res = block.call(m) return res.is_a?(Hash) ? new!(res) : res end end end raise ArgumentError, 'invalid date' end |
.reset_parsers! ⇒ Object
Reset the parsers, parser types, and order of parsers used to the default.
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/time_crisis/date.rb', line 171 def self.reset_parsers! parser_hash.clear default_parser_hash.each do |type, parsers| add_parser_type(type) parsers.reverse.each do |re, parser| add_parser(type, re, &parser) end end use_parsers(*default_parser_list) end |
.strptime(str, fmt = strptime_default) ⇒ Object
Parse the string using the provided format (or the default format). Raises an ArgumentError if the format does not match the string.
184 185 186 187 188 189 190 191 192 |
# File 'lib/time_crisis/date.rb', line 184 def self.strptime(str, fmt=strptime_default) pattern, block = strptime_pattern_and_block(fmt) s = str.strip if m = pattern.match(s) block.call(m) else raise ArgumentError, 'invalid date' end end |
.today ⇒ Object
Returns a date with the current year, month, and date.
195 196 197 198 |
# File 'lib/time_crisis/date.rb', line 195 def self.today t = Time.now civil(t.year, t.mon, t.day) end |
.tomorrow ⇒ Object
11 12 13 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 11 def tomorrow ::TimeCrisis::Date.today.tomorrow end |
.use_parsers(*parsers) ⇒ Object
Set the order of parser types to use to the given parser types.
201 202 203 |
# File 'lib/time_crisis/date.rb', line 201 def self.use_parsers(*parsers) parser_list.replace(parsers) end |
.yesterday ⇒ Object
7 8 9 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 7 def yesterday ::TimeCrisis::Date.today.yesterday end |
Instance Method Details
#<<(m) ⇒ Object
Returns a new date with m number of months subtracted from this date.
391 392 393 |
# File 'lib/time_crisis/date.rb', line 391 def <<(m) self >> -m end |
#<=>(date) ⇒ Object
Compare two dates. If the given date is greater than self, return -1, if it is less, return 1, and if it is equal, return 0. If given date is a number, compare this date’s julian date to it.
398 399 400 401 402 403 404 405 |
# File 'lib/time_crisis/date.rb', line 398 def <=>(date) if date.is_a?(Numeric) jd <=> date else ((d = (year <=> date.year)) == 0) && ((d = (mon <=> date.mon)) == 0) && ((d = (day <=> date.day)) == 0) d end end |
#==(date) ⇒ Object Also known as: eql?
Dates are equel only if their year, month, and day match.
408 409 410 411 |
# File 'lib/time_crisis/date.rb', line 408 def ==(date) return false unless Date === date year == date.year and mon == date.mon and day == date.day end |
#===(d) ⇒ Object
If d is a date, only true if it is equal to this date. If d is Numeric, only true if it equals this date’s julian date.
415 416 417 418 419 420 421 |
# File 'lib/time_crisis/date.rb', line 415 def ===(d) case d when Numeric then jd == d when Date then self == d else false end end |
#>>(m) ⇒ Object
Returns a new date with m number of months added to this date. If the day of self does not exist in the new month, set the new day to be the last day of the new month.
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/time_crisis/date.rb', line 372 def >>(m) raise(TypeError, "m must be an integer") unless m.is_a?(Integer) y = year n = mon + m if n > 12 or n <= 0 a, n = n.divmod(12) if n == 0 n = 12 y += a - 1 else y += a end end ndays = days_in_month(n, y) d = day > ndays ? ndays : day new_civil(y, n, d) end |
#acts_like_date? ⇒ Boolean
5 6 7 |
# File 'lib/time_crisis/support/ext/date/acts_like.rb', line 5 def acts_like_date? true end |
#advance(options) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 72 def advance() = .dup 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
32 33 34 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 32 def ago(seconds) to_tc_time.since(-seconds) end |
#beginning_of_day ⇒ Object Also known as: midnight, at_midnight, at_beginning_of_day
41 42 43 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 41 def beginning_of_day to_tc_time end |
#beginning_of_month ⇒ Object Also known as: at_beginning_of_month
143 144 145 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 143 def beginning_of_month self.acts_like?(:time) ? change(:day => 1,:hour => 0, :min => 0, :sec => 0) : change(:day => 1) end |
#beginning_of_quarter ⇒ Object Also known as: at_beginning_of_quarter
154 155 156 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 154 def beginning_of_quarter beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month }) end |
#beginning_of_week ⇒ Object Also known as: monday, at_beginning_of_week
122 123 124 125 126 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 122 def beginning_of_week days_to_monday = self.wday!=0 ? self.wday-1 : 6 result = self - days_to_monday self.acts_like?(:time) ? result.midnight : result end |
#beginning_of_year ⇒ Object Also known as: at_beginning_of_year
164 165 166 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 164 def beginning_of_year self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0) : change(:month => 1, :day => 1) end |
#change(options) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 82 def change() ::TimeCrisis::Date.civil( [:year] || self.year, [:month] || self.month, [:day] || self.day ) end |
#cwday ⇒ Object
The commercial week day for this date.
424 425 426 |
# File 'lib/time_crisis/date.rb', line 424 def cwday @cwday || commercial[2] end |
#cweek ⇒ Object
The commercial week for this date.
429 430 431 |
# File 'lib/time_crisis/date.rb', line 429 def cweek @cweek || commercial[1] end |
#cwyear ⇒ Object
The commercial week year for this date.
434 435 436 |
# File 'lib/time_crisis/date.rb', line 434 def cwyear @cwyear || commercial[0] end |
#day ⇒ Object Also known as: mday
The day of the month for this date.
439 440 441 |
# File 'lib/time_crisis/date.rb', line 439 def day @day || civil[2] end |
#days_in_month(m = nil, y = nil) ⇒ Object
539 540 541 |
# File 'lib/time_crisis/date.rb', line 539 def days_in_month(m=nil, y=nil) (_leap?(y||year) ? LEAP_DAYS_IN_MONTH : DAYS_IN_MONTH)[m||mon] end |
#downto(d, &block) ⇒ Object
Yield every date between this date and given date to the block. The given date should be less than this date.
446 447 448 |
# File 'lib/time_crisis/date.rb', line 446 def downto(d, &block) step(d, -1, &block) end |
#end_of_day ⇒ Object
48 49 50 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 48 def end_of_day to_tc_time.end_of_day end |
#end_of_month ⇒ Object Also known as: at_end_of_month
148 149 150 151 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 148 def end_of_month last_day = ::TimeCrisis::Time.days_in_month( self.month, self.year ) self.acts_like?(:time) ? change(:day => last_day, :hour => 23, :min => 59, :sec => 59) : change(:day => last_day) end |
#end_of_quarter ⇒ Object Also known as: at_end_of_quarter
159 160 161 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 159 def end_of_quarter beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month end |
#end_of_week ⇒ Object Also known as: at_end_of_week
130 131 132 133 134 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 130 def end_of_week days_to_sunday = self.wday!=0 ? 7-self.wday : 0 result = self + days_to_sunday.days self.acts_like?(:time) ? result.end_of_day : result end |
#end_of_year ⇒ Object Also known as: at_end_of_year
169 170 171 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 169 def end_of_year self.acts_like?(:time) ? change(:month => 12,:day => 31,:hour => 23, :min => 59, :sec => 59) : change(:month => 12, :day => 31) end |
#future? ⇒ Boolean
28 29 30 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 28 def future? self > ::TimeCrisis::Date.current end |
#hash ⇒ Object
Unique value for this date, based on it’s year, month, and day of month.
451 452 453 |
# File 'lib/time_crisis/date.rb', line 451 def hash civil.hash end |
#jd ⇒ Object
This date’s julian date.
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/time_crisis/date.rb', line 462 def jd @jd ||= ( y = year m = mon d = day if m <= 2 y -= 1 m += 12 end a = (y / 100.0).floor jd = (365.25 * (y + 4716)).floor + (30.6001 * (m + 1)).floor + d - 1524 + (2 - a + (a / 4.0).floor) ) end |
#last_month ⇒ Object
114 115 116 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 114 def last_month months_ago(1) end |
#last_year ⇒ Object
106 107 108 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 106 def last_year years_ago(1) end |
#leap? ⇒ Boolean
Whether this date is in a leap year.
479 480 481 |
# File 'lib/time_crisis/date.rb', line 479 def leap? _leap?(year) end |
#minus_with_duration(other) ⇒ Object Also known as: -
:nodoc:
62 63 64 65 66 67 68 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 62 def minus_with_duration(other) #:nodoc: if TimeCrisis::Duration === other plus_with_duration(-other) else minus_without_duration(other) end end |
#mon ⇒ Object Also known as: month
The month number for this date (January is 1, December is 12).
484 485 486 |
# File 'lib/time_crisis/date.rb', line 484 def mon @mon || civil[1] end |
#months_ago(months) ⇒ Object
90 91 92 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 90 def months_ago(months) advance(:months => -months) end |
#months_since(months) ⇒ Object
94 95 96 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 94 def months_since(months) advance(:months => months) end |
#next_month ⇒ Object
118 119 120 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 118 def next_month months_since(1) end |
#next_week(day = :monday) ⇒ Object
137 138 139 140 141 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 137 def next_week(day = :monday) days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} result = (self + 7).beginning_of_week + days_into_week[day] self.acts_like?(:time) ? result.change(:hour => 0) : result end |
#next_year ⇒ Object
110 111 112 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 110 def next_year years_since(1) end |
#past? ⇒ Boolean
20 21 22 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 20 def past? self < ::TimeCrisis::Date.current end |
#plus_with_duration(other) ⇒ Object Also known as: +
:nodoc:
52 53 54 55 56 57 58 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 52 def plus_with_duration(other) #:nodoc: if TimeCrisis::Duration === other other.since(self) else plus_without_duration(other) end end |
#readable_inspect ⇒ Object Also known as: inspect
25 26 27 |
# File 'lib/time_crisis/support/ext/date/conversions.rb', line 25 def readable_inspect "#<TimeCrisis::Date #{strftime('%a, %d %b %Y')}>" end |
#since(seconds) ⇒ Object Also known as: in
36 37 38 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 36 def since(seconds) to_tc_time.since(seconds) end |
#step(limit, step = 1) ⇒ Object
Yield each date between this date and limit, adding step number of days in each iteration. Returns current date.
491 492 493 494 495 496 497 498 499 |
# File 'lib/time_crisis/date.rb', line 491 def step(limit, step=1) da = self op = %w(== <= >=)[step <=> 0] while da.__send__(op, limit) yield da da += step end self end |
#strftime(fmt = strftime_default) ⇒ Object
Format the time using a format string, or the default format string.
502 503 504 |
# File 'lib/time_crisis/date.rb', line 502 def strftime(fmt=strftime_default) fmt.gsub(STRFTIME_RE){|x| _strftime(x[1..1])} end |
#succ ⇒ Object Also known as: next
Return the day after this date.
507 508 509 |
# File 'lib/time_crisis/date.rb', line 507 def succ self + 1 end |
#to_date ⇒ Object
44 45 46 |
# File 'lib/time_crisis/support/ext/date/conversions.rb', line 44 def to_date ::Date.civil(year, month, day) end |
#to_datetime ⇒ Object
48 49 50 |
# File 'lib/time_crisis/support/ext/date/conversions.rb', line 48 def to_datetime ::DateTime.civil(year, month, day, 0, 0, 0, 0) end |
#to_formatted_s(format = :default) ⇒ Object Also known as: to_s
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/time_crisis/support/ext/date/conversions.rb', line 11 def to_formatted_s(format = :default) if formatter = DATE_FORMATS[format] if formatter.respond_to?(:call) formatter.call(self).to_s else strftime(formatter) end else to_default_s end end |
#to_json(options = nil) ⇒ Object
3 4 5 |
# File 'lib/time_crisis/to_json.rb', line 3 def to_json( = nil) %("#{strftime("%Y-%m-%d")}") end |
#to_tc_date ⇒ Object
31 32 33 |
# File 'lib/time_crisis/support/ext/date/conversions.rb', line 31 def to_tc_date self end |
#to_tc_datetime(form = :local) ⇒ Object
35 36 37 38 |
# File 'lib/time_crisis/support/ext/date/conversions.rb', line 35 def to_tc_datetime(form = :local) offset = form == :local ? (Time.zone.now.utc_offset rescue Time.now.utc_offset) : 0 ::TimeCrisis::DateTime.civil(year, month, day, 0, 0, 0, 0, offset) end |
#to_tc_time(form = :local) ⇒ Object
40 41 42 |
# File 'lib/time_crisis/support/ext/date/conversions.rb', line 40 def to_tc_time(form = :local) ::TimeCrisis::Time.send(form, year, month, day) end |
#to_time(form = :local) ⇒ Object
52 53 54 |
# File 'lib/time_crisis/support/ext/date/conversions.rb', line 52 def to_time(form = :local) ::Time.send(form, year, month, day) end |
#today? ⇒ Boolean
24 25 26 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 24 def today? self.to_tc_date == ::TimeCrisis::Date.current end |
#tomorrow ⇒ Object
178 179 180 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 178 def tomorrow self + 1 end |
#upto(d, &block) ⇒ Object
Yield every date between this date and the given date to the block. The given date should be greater than this date.
519 520 521 |
# File 'lib/time_crisis/date.rb', line 519 def upto(d, &block) step(d, &block) end |
#wday ⇒ Object
Return the day of the week for this date. Sunday is 0, Saturday is 6.
524 525 526 |
# File 'lib/time_crisis/date.rb', line 524 def wday (jd + 1) % 7 end |
#xmlschema ⇒ Object
56 57 58 |
# File 'lib/time_crisis/support/ext/date/conversions.rb', line 56 def xmlschema to_tc_datetime.xmlschema end |
#yday ⇒ Object
Return the day of the year for this date. January 1 is 1.
529 530 531 532 |
# File 'lib/time_crisis/date.rb', line 529 def yday h = leap? ? LEAP_CUMMULATIVE_MONTH_DAYS : CUMMULATIVE_MONTH_DAYS @yday ||= h[mon] + day end |
#year ⇒ Object
Return the year for this date.
535 536 537 |
# File 'lib/time_crisis/date.rb', line 535 def year @year || civil[0] end |
#years_ago(years) ⇒ Object
98 99 100 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 98 def years_ago(years) advance(:years => -years) end |
#years_since(years) ⇒ Object
102 103 104 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 102 def years_since(years) advance(:years => years) end |
#yesterday ⇒ Object
174 175 176 |
# File 'lib/time_crisis/support/ext/date/calculations.rb', line 174 def yesterday self - 1 end |