mirror of
https://github.com/jimeh/time_ext.git
synced 2026-02-19 13:26:39 +00:00
lots of changes
This commit is contained in:
@@ -1,5 +1,13 @@
|
|||||||
class Time
|
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)
|
# Returns a new Time representing the start of the unit specified (second by default)
|
||||||
def floor(unit = :sec)
|
def floor(unit = :sec)
|
||||||
self.send("beginning_of_#{unit}")
|
self.send("beginning_of_#{unit}")
|
||||||
@@ -14,6 +22,13 @@ class Time
|
|||||||
alias :beginning_of_next :ceil
|
alias :beginning_of_next :ceil
|
||||||
alias :at_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)
|
# Short-hand for seconds_ago(1)
|
||||||
def prev_second
|
def prev_second
|
||||||
ago(1)
|
ago(1)
|
||||||
@@ -58,6 +73,21 @@ class Time
|
|||||||
days_since(1)
|
days_since(1)
|
||||||
end
|
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
|
# Aliases to keep available method names to a standard pattern
|
||||||
alias :secs_ago :ago
|
alias :secs_ago :ago
|
||||||
alias :seconds_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
|
# Returns a new Time representing the time a number of specified minutes ago
|
||||||
def minutes_ago(minutes)
|
def minutes_ago(minutes)
|
||||||
advance(:minutes => -minutes)
|
ago(minutes.minutes)
|
||||||
end
|
end
|
||||||
alias :mins_ago :minutes_ago
|
alias :mins_ago :minutes_ago
|
||||||
|
|
||||||
# Returns a new Time representing the time a number of specified minutes in the future
|
# Returns a new Time representing the time a number of specified minutes in the future
|
||||||
def minutes_since(minutes)
|
def minutes_since(minutes)
|
||||||
advance(:minutes => minutes)
|
since(minutes.minutes)
|
||||||
end
|
end
|
||||||
alias :mins_since :minutes_since
|
alias :mins_since :minutes_since
|
||||||
|
|
||||||
# Returns a new Time representing the time a number of specified hours ago
|
# Returns a new Time representing the time a number of specified hours ago
|
||||||
def hours_ago(hours)
|
def hours_ago(hours)
|
||||||
advance(:hours => -hours)
|
ago(hours.hours)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a new Time representing the time a number of specified hours in the future
|
# Returns a new Time representing the time a number of specified hours in the future
|
||||||
def hours_since(hours)
|
def hours_since(hours)
|
||||||
advance(:hours => hours)
|
since(hours.hours)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a new Time representing the time a number of specified days ago
|
# Returns a new Time representing the time a number of specified days ago
|
||||||
def days_ago(days)
|
def days_ago(days)
|
||||||
advance(:days => -days)
|
ago(days.days)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a new Time representing the time a number of specified days in the future
|
# Returns a new Time representing the time a number of specified days in the future
|
||||||
def days_since(days)
|
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
|
end
|
||||||
|
|
||||||
# Returns a new Time representing the start of the second, XX:XX:XX.000000 (.000000000 in ruby1.9)
|
# Returns a new Time representing the start of the second, XX:XX:XX.000000 (.000000000 in ruby1.9)
|
||||||
|
|||||||
Reference in New Issue
Block a user