mirror of
https://github.com/jimeh/time_ext.git
synced 2026-02-19 13:26:39 +00:00
initial import of time extensions
This commit is contained in:
1
lib/time/ext.rb
Normal file
1
lib/time/ext.rb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
require 'time_ext'
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
require 'rubygems'
|
||||||
|
require 'active_support'
|
||||||
|
require 'time_ext/time'
|
||||||
168
lib/time_ext/time.rb
Normal file
168
lib/time_ext/time.rb
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
class Time
|
||||||
|
|
||||||
|
# Returns a new Time representing the start of the unit specified (second by default)
|
||||||
|
def floor(unit = :sec)
|
||||||
|
self.send("beginning_of_#{unit}")
|
||||||
|
end
|
||||||
|
alias :beginning_of :floor
|
||||||
|
alias :at_beginning_of :floor
|
||||||
|
|
||||||
|
# Returns a new Time representing the start of the next unit specified (second by default)
|
||||||
|
def ceil(unit = :sec)
|
||||||
|
self.send("next_#{unit}").send("beginning_of_#{unit}")
|
||||||
|
end
|
||||||
|
alias :beginning_of_next :ceil
|
||||||
|
alias :at_beginning_of_next :ceil
|
||||||
|
|
||||||
|
# Short-hand for seconds_ago(1)
|
||||||
|
def prev_second
|
||||||
|
seconds_ago(1)
|
||||||
|
end
|
||||||
|
alias :prev_sec :prev_second
|
||||||
|
|
||||||
|
# Short-hand for seconds_since(1)
|
||||||
|
def next_second
|
||||||
|
seconds_since(1)
|
||||||
|
end
|
||||||
|
alias :next_sec :next_second
|
||||||
|
|
||||||
|
# Short-hand for minutes_ago(1)
|
||||||
|
def prev_minute
|
||||||
|
minutes_ago(1)
|
||||||
|
end
|
||||||
|
alias :prev_min :prev_minute
|
||||||
|
|
||||||
|
# Short-hand for minutes_since(1)
|
||||||
|
def next_minute
|
||||||
|
minutes_since(1)
|
||||||
|
end
|
||||||
|
alias :next_min :next_minute
|
||||||
|
|
||||||
|
# Short-hand for hours_ago(1)
|
||||||
|
def prev_hour
|
||||||
|
hours_ago(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Short-hand for hours_since(1)
|
||||||
|
def next_hour
|
||||||
|
hours_since(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Short-hand for days_ago(1)
|
||||||
|
def prev_day
|
||||||
|
days_ago(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Short-hand for days_since(1)
|
||||||
|
def next_day
|
||||||
|
days_since(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a new Time representing the time a number of specified seconds ago
|
||||||
|
def seconds_ago(seconds)
|
||||||
|
advance(:seconds => -seconds)
|
||||||
|
end
|
||||||
|
alias :secs_ago :seconds_ago
|
||||||
|
|
||||||
|
# Returns a new Time representing the time a number of specified seconds in the future
|
||||||
|
def seconds_since(seconds)
|
||||||
|
advance(:seconds => seconds)
|
||||||
|
end
|
||||||
|
alias :secs_since :seconds_since
|
||||||
|
|
||||||
|
# Returns a new Time representing the time a number of specified minutes ago
|
||||||
|
def minutes_ago(minutes)
|
||||||
|
advance(: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)
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a new Time representing the time a number of specified hours in the future
|
||||||
|
def hours_since(hours)
|
||||||
|
advance(:hours => hours)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a new Time representing the time a number of specified days ago
|
||||||
|
def days_ago(days)
|
||||||
|
advance(: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)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a new Time representing the start of the hour (XX:00:00)
|
||||||
|
def beginning_of_hour
|
||||||
|
change(:min => 0, :sec => 0, :usec => 0)
|
||||||
|
end
|
||||||
|
alias :at_beginning_of_hour :beginning_of_hour
|
||||||
|
|
||||||
|
# Returns a new Time representing the end of the hour, XX:59:59.999999 (.999999999 in ruby1.9)
|
||||||
|
def end_of_hour
|
||||||
|
change(:min => 59, :sec => 59, :usec => 999999.999)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a new Time representing the start of the minute (XX:XX:00)
|
||||||
|
def beginning_of_minute
|
||||||
|
change(:sec => 0, :usec => 0)
|
||||||
|
end
|
||||||
|
alias :beginning_of_min :beginning_of_minute
|
||||||
|
alias :at_beginning_of_min :beginning_of_minute
|
||||||
|
alias :at_beginning_of_minute :beginning_of_minute
|
||||||
|
|
||||||
|
# Returns a new Time representing the end of the hour, XX:XX:59.999999 (.999999999 in ruby1.9)
|
||||||
|
def end_of_minute
|
||||||
|
change(:sec => 59, :usec => 999999.999)
|
||||||
|
end
|
||||||
|
alias :end_of_min :end_of_minute
|
||||||
|
|
||||||
|
# Returns a new Time representing the start of the second, XX:XX:XX.000000 (.000000000 in ruby1.9)
|
||||||
|
def beginning_of_second
|
||||||
|
change(:usec => 0)
|
||||||
|
end
|
||||||
|
alias :beginning_of_sec :beginning_of_second
|
||||||
|
alias :at_beginning_of_sec :beginning_of_second
|
||||||
|
alias :at_beginning_of_second :beginning_of_second
|
||||||
|
|
||||||
|
# Returns a new Time representing the end of the hour, XX:XX:XX.999999 (.999999999 in ruby1.9)
|
||||||
|
def end_of_second
|
||||||
|
change(:usec => 999999.999)
|
||||||
|
end
|
||||||
|
alias :end_of_sec :end_of_second
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,7 +1,108 @@
|
|||||||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||||
|
|
||||||
describe "TimeExt" do
|
describe "TimeExt" do
|
||||||
it "fails" do
|
|
||||||
fail "hey buddy, you should probably rename this file and start specing for real"
|
before(:each) do
|
||||||
|
@now = Time.now
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should have extra Time calculation methods" do
|
||||||
|
@now.prev_second.should == @now - 1.second
|
||||||
|
@now.prev_sec.should == @now - 1.second
|
||||||
|
@now.next_second.should == @now + 1.second
|
||||||
|
@now.next_sec.should == @now + 1.second
|
||||||
|
|
||||||
|
@now.prev_minute.should == @now - 1.minute
|
||||||
|
@now.prev_min.should == @now - 1.minute
|
||||||
|
@now.next_minute.should == @now + 1.minute
|
||||||
|
@now.next_min.should == @now + 1.minute
|
||||||
|
|
||||||
|
@now.prev_hour.should == @now - 1.hour
|
||||||
|
@now.next_hour.should == @now + 1.hour
|
||||||
|
|
||||||
|
@now.prev_day.should == @now - 1.day
|
||||||
|
@now.next_day.should == @now + 1.day
|
||||||
|
@now.days_ago(1).should == @now - 1.day
|
||||||
|
@now.days_since(1).should == @now + 1.day
|
||||||
|
|
||||||
|
@now.respond_to?(:floor).should be_true
|
||||||
|
@now.respond_to?(:beginning_of).should be_true
|
||||||
|
@now.respond_to?(:ceil).should be_true
|
||||||
|
@now.respond_to?(:beginning_of_next).should be_true
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should floor to seconds" do
|
||||||
|
@now.floor(:sec).usec.should == 0
|
||||||
|
@now.floor(:sec).sec.should == @now.sec
|
||||||
|
@now.floor(:sec).min.should == @now.min
|
||||||
|
@now.floor(:sec).hour.should == @now.hour
|
||||||
|
@now.floor(:sec).day.should == @now.day
|
||||||
|
@now.floor(:sec).month.should == @now.month
|
||||||
|
@now.floor(:sec).year.should == @now.year
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should floor to minutes" do
|
||||||
|
@now.floor(:min).usec.should == 0
|
||||||
|
@now.floor(:min).sec.should == 0
|
||||||
|
@now.floor(:min).min.should == @now.min
|
||||||
|
@now.floor(:min).hour.should == @now.hour
|
||||||
|
@now.floor(:min).day.should == @now.day
|
||||||
|
@now.floor(:min).month.should == @now.month
|
||||||
|
@now.floor(:min).year.should == @now.year
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should floor to hours" do
|
||||||
|
@now.floor(:hour).usec.should == 0
|
||||||
|
@now.floor(:hour).sec.should == 0
|
||||||
|
@now.floor(:hour).min.should == 0
|
||||||
|
@now.floor(:hour).hour.should == @now.hour
|
||||||
|
@now.floor(:hour).day.should == @now.day
|
||||||
|
@now.floor(:hour).month.should == @now.month
|
||||||
|
@now.floor(:hour).year.should == @now.year
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should floor to days" do
|
||||||
|
@now.floor(:day).usec.should == 0
|
||||||
|
@now.floor(:day).sec.should == 0
|
||||||
|
@now.floor(:day).min.should == 0
|
||||||
|
@now.floor(:day).hour.should == 0
|
||||||
|
@now.floor(:day).day.should == @now.day
|
||||||
|
@now.floor(:day).month.should == @now.month
|
||||||
|
@now.floor(:day).year.should == @now.year
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should floor to months" do
|
||||||
|
@now.floor(:month).usec.should == 0
|
||||||
|
@now.floor(:month).sec.should == 0
|
||||||
|
@now.floor(:month).min.should == 0
|
||||||
|
@now.floor(:month).hour.should == 0
|
||||||
|
@now.floor(:month).day.should == 1
|
||||||
|
@now.floor(:month).month.should == @now.month
|
||||||
|
@now.floor(:month).year.should == @now.year
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should floor to years" do
|
||||||
|
@now.floor(:year).usec.should == 0
|
||||||
|
@now.floor(:year).sec.should == 0
|
||||||
|
@now.floor(:year).min.should == 0
|
||||||
|
@now.floor(:year).hour.should == 0
|
||||||
|
@now.floor(:year).day.should == 1
|
||||||
|
@now.floor(:year).month.should == 1
|
||||||
|
@now.floor(:year).year.should == @now.year
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user