Skip to content

Instantly share code, notes, and snippets.

@dsci
Created February 28, 2012 15:59
Show Gist options
  • Select an option

  • Save dsci/1933323 to your computer and use it in GitHub Desktop.

Select an option

Save dsci/1933323 to your computer and use it in GitHub Desktop.
Clockwork ... tiny Time extensions
Gem::Specification.new do |s|
s.name = 'clockwork'
s.version = '0.1'
s.platform = Gem::Platform::RUBY
s.author = 'Daniel Schmidt'
s.email = '[email protected]'
s.summary = 'Clockwork Orange'
s.description = 'Collection of Time extensions.'
s.files = ['clockwork.rb',]
s.test_file = 'clockwork_spec.rb'
s.require_path = '.'
s.add_development_dependency('chronic', ["~> 0.6.7"])
s.add_development_dependency('rspec', ["~> 2.0"])
s.add_development_dependency("delorean", "~> 1.2.0")
end
Time.class_eval do
def is_tomorrow?
do_absolute_date_difference(Time.now)
end
def is_yesterday?
do_absolute_date_difference(Time.now)
end
def time_only
return self.strftime("%H:%M:%S")
end
private
def do_absolute_date_difference(time)
return ((time.day-self.day).abs == 1) if self.month == time.month
return [28,29,30].include?((time.day-self.day).abs) if (self.month - time.month).abs == 1
if((self.year - time.year).abs==1)
return [28,29,30].include?((time.day-self.day).abs) if (self.month - time.month).abs == 11
end
return false
end
end
require File.expand_path('clockwork')
require 'chronic'
require 'delorean'
describe "Clockwork Time extensions" do
describe "methods" do
subject{Time.now}
it{should respond_to(:is_tomorrow?)}
it{should respond_to(:is_yesterday?)}
describe '#is_tomorrow?' do
context "with tomorrow date" do
let(:is_tomorrow){Chronic.parse("tomorrow").is_tomorrow?}
it "should be true" do
is_tomorrow.should be true
end
end
context "with todays date" do
let(:is_tomorrow){Chronic.parse("today").is_tomorrow?}
it "should be false" do
is_tomorrow.should be false
end
end
context "and month border" do
before do
Delorean.time_travel_to "2012-02-29"
end
after do
Delorean.back_to_the_present
end
let(:is_tomorrow){Chronic.parse("tomorrow").is_tomorrow?}
it "should be true" do
is_tomorrow.should be true
end
end
context "and year border" do
before do
Delorean.time_travel_to "dec 31 2012"
end
after do
Delorean.back_to_the_present
end
let(:is_tomorrow){Chronic.parse("jan 1 2013").is_tomorrow?}
it "should be true" do
is_tomorrow.should be true
end
end
context "any date" do
let(:is_tomorrow){Chronic.parse("dec 31 2014").is_tomorrow?}
it "should be false" do
is_tomorrow.should be false
end
end
end
describe "#is_yesterday?" do
context "with yesterday date" do
let(:is_yesterday){Chronic.parse("yesterday").is_yesterday?}
it "should be true" do
is_yesterday.should be true
end
end
context "with todays date" do
let(:is_yesterday){Chronic.parse("today").is_yesterday?}
it "should be false" do
is_yesterday.should be false
end
end
context "and month border" do
before do
Delorean.time_travel_to "2012-02-29"
end
after do
Delorean.back_to_the_present
end
let(:is_yesterday){Chronic.parse("yesterday").is_yesterday?}
it "should be true" do
is_yesterday.should be true
end
end
context "and year border" do
before do
Delorean.time_travel_to "jan 01 2013"
end
after do
Delorean.back_to_the_present
end
let(:is_yesterday){Chronic.parse("dec 31 2012").is_yesterday?}
it "should be true" do
is_yesterday.should be true
end
end
context "any date" do
let(:is_yesterday){Chronic.parse("dec 31 2012").is_yesterday?}
it "should be false" do
is_yesterday.should be false
end
end
end
describe "#time_only" do
let(:time_only){Chronic.parse("today at noon")}
it "extracts time and should be 12:00:00" do
time_only.should eq "12:00:00"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment