From d0a2534f34d8b2698e62aad00190f9a78eb55434 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Tue, 27 Jul 2010 19:18:18 +0300 Subject: [PATCH] lots of changes --- lib/time_ext/time.rb | 62 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/lib/time_ext/time.rb b/lib/time_ext/time.rb index 4e3fb71..629895b 100644 --- a/lib/time_ext/time.rb +++ b/lib/time_ext/time.rb @@ -1,5 +1,13 @@ class Time + def days_into_week + defined?(DAYS_INTO_WEEK) ? DAYS_INTO_WEEK : { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 } + end + + def common_year_days_in_month + defined?(COMMON_YEAR_DAYS_IN_MONTH) ? COMMON_YEAR_DAYS_IN_MONTH : [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + end + # Returns a new Time representing the start of the unit specified (second by default) def floor(unit = :sec) self.send("beginning_of_#{unit}") @@ -14,6 +22,13 @@ class Time alias :beginning_of_next :ceil alias :at_beginning_of_next :ceil + def round(unit = :sec) + next_unit = self.ceil(unit) + this_unit = self.floor(unit) + (self - this_unit) < (next_unit - self) ? this_unit : next_unit + end + alias :beginning_of_closest :round + # Short-hand for seconds_ago(1) def prev_second ago(1) @@ -58,6 +73,21 @@ class Time days_since(1) end + # Returns a new Time representing the start of the given day in the previous week (default is Monday). + def prev_week(day = :monday) + weeks_ago(1).beginning_of_week.since(days_into_week[day].day).change(:hour => 0) + end + + # Short-hand for quarters_since(1).beginning_of_quarter + def next_quarter + quarters_since(1).beginning_of_quarter + end + + # Short-hand for quarters_ago(1).beginning_of_quarter + def prev_quarter + quarters_ago(1).beginning_of_quarter + end + # Aliases to keep available method names to a standard pattern alias :secs_ago :ago alias :seconds_ago :ago @@ -66,34 +96,54 @@ class Time # Returns a new Time representing the time a number of specified minutes ago def minutes_ago(minutes) - advance(:minutes => -minutes) + ago(minutes.minutes) end alias :mins_ago :minutes_ago # Returns a new Time representing the time a number of specified minutes in the future def minutes_since(minutes) - advance(:minutes => minutes) + since(minutes.minutes) end alias :mins_since :minutes_since # Returns a new Time representing the time a number of specified hours ago def hours_ago(hours) - advance(:hours => -hours) + ago(hours.hours) end # Returns a new Time representing the time a number of specified hours in the future def hours_since(hours) - advance(:hours => hours) + since(hours.hours) end # Returns a new Time representing the time a number of specified days ago def days_ago(days) - advance(:days => -days) + ago(days.days) end # Returns a new Time representing the time a number of specified days in the future def days_since(days) - advance(:days => days) + since(days.days) + end + + # Returns a new Time representing the time a number of specified weeks ago + def weeks_ago(weeks) + ago(weeks.weeks) + end + + # Returns a new Time representing the time a number of specified weeks in the future + def weeks_since(weeks) + since(weeks.weeks) + end + + # Returns a new Time representing the time a number of specified quarters (3 months) ago + def quarters_ago(quarters) + ago((quarters * 3).months) + end + + # Returns a new Time representing the time a number of specified quarters (3 months) in the future + def quarters_since(quarters) + since((quarters * 3).months) end # Returns a new Time representing the start of the second, XX:XX:XX.000000 (.000000000 in ruby1.9)