Skip to content

Commit

Permalink
bump minor
Browse files Browse the repository at this point in the history
  • Loading branch information
seamusabshere committed Apr 24, 2012
1 parent 9f093f3 commit 6747f1b
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 46 deletions.
5 changes: 0 additions & 5 deletions .document

This file was deleted.

11 changes: 11 additions & 0 deletions CHANGELOG
@@ -0,0 +1,11 @@
0.2.0 / 2012-04-24

* Breaking changes

* Use ISO8601 for JSON output. No sense in coming up with our own structure. Timeframe.parse will still understand the old startDate/endDate hash structure.

* Enhancements

* New MultiJson syntax
* Simplify and DRY.
* Tested on MRI 1.8, MRI 1.9, and JRuby 1.6.7+
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -4,5 +4,6 @@ gemspec

# development dependencies
gem 'yard'
gem 'minitest'
gem 'rake'
gem 'minitest'
gem 'minitest-reporters'
13 changes: 11 additions & 2 deletions README.markdown
Expand Up @@ -2,6 +2,17 @@

A Ruby class for describing and interacting with timeframes.

## Real-world usage

<p><a href="http://brighterplanet.com"><img src="https://s3.amazonaws.com/static.brighterplanet.com/assets/logos/flush-left/inline/green/rasterized/brighter_planet-160-transparent.png" alt="Brighter Planet logo"/></a></p>

We use `timeframe` for [data science at Brighter Planet](http://brighterplanet.com/research) and in production at

* [Brighter Planet's impact estimate web service](http://impact.brighterplanet.com)
* [Brighter Planet's reference data web service](http://data.brighterplanet.com)

Originally proposed to us by [the awesome programmers at fingertips](http:/fngtps.com)

## Based on ISO 8601

As [documented by wikipedia](http://en.wikipedia.org/wiki/ISO_8601#Time_intervals), time intervals are like:
Expand Down Expand Up @@ -33,8 +44,6 @@ http://rdoc.info/projects/rossmeissl/timeframe

## Acknowledgements

The good parts of Timeframe all came from the gentlemen at Fingertips[http://fngtps.com].

Thanks to @artemk for https://github.com/rossmeissl/timeframe/pull/5

## Copyright
Expand Down
15 changes: 7 additions & 8 deletions Rakefile
@@ -1,15 +1,14 @@
require 'bundler'
Bundler::GemHelper.install_tasks
#!/usr/bin/env rake
require "bundler/gem_tasks"

require 'rake'
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs.push 'lib'
t.test_files = FileList['spec/**/*spec.rb']
t.verbose = true
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end


task :default => :test

require 'yard'
Expand Down
4 changes: 2 additions & 2 deletions lib/timeframe.rb
Expand Up @@ -279,11 +279,11 @@ def last_year
end

def to_json(*)
%({"startDate":"#{start_date.iso8601}","endDate":"#{end_date.iso8601}"})
iso8601
end

def as_json(*)
{ :startDate => start_date.iso8601, :endDate => end_date.iso8601 }
iso8601
end

# An ISO 8601 "time interval" like YYYY-MM-DD/YYYY-MM-DD
Expand Down
29 changes: 15 additions & 14 deletions lib/timeframe/iso_8601.rb
Expand Up @@ -3,17 +3,17 @@ module Iso8601
# Internal use.
#
# Parses a duration like 'P1Y2M4DT3H4M2S'
class Duration < ::Struct.new(:date_part, :time_part)
def seconds
(y*31_556_926 + m*2_629_743.83 + d*86_400 + h*3_600 + minutes*60 + s).ceil
class Duration
attr_reader :seconds
def initialize(date_part, time_part)
y = parse date_part, :Y
m = parse date_part, :M
d = parse date_part, :D
h = parse time_part, :H
minutes = parse time_part, :M
s = parse time_part, :S
@seconds = (y*31_556_926 + m*2_629_743.83 + d*86_400 + h*3_600 + minutes*60 + s).ceil
end
private
def y; @y ||= parse(date_part, :Y); end
def m; @m ||= parse(date_part, :M); end
def d; @d ||= parse(date_part, :D); end
def h; @h ||= parse(time_part, :H); end
def minutes; @minutes ||= parse(time_part, :M); end
def s; @s ||= parse(time_part, :S); end
def parse(part, indicator)
if part =~ /(\d+)#{indicator.to_s}/
$1.to_f
Expand All @@ -27,10 +27,11 @@ def parse(part, indicator)
class Side
# We add one day because so that it can be excluded per timeframe's conventions.
EXCLUDED_LAST_DAY = 86_400
attr_reader :date_part, :time_part
attr_reader :date_part
attr_reader :time_part
def to_time(counterpart)
if date_part.start_with?('P')
counterpart.resolve_time(self) + resolve_offset + EXCLUDED_LAST_DAY
counterpart.resolve_time(self) + offset + EXCLUDED_LAST_DAY
else
resolve_time counterpart
end
Expand All @@ -50,7 +51,7 @@ def resolve_time(*)
Time.parse [date_part, time_part].join('T')
end
# When A is a period, it counts as a negative offset to B.
def resolve_offset
def offset
0.0 - Duration.new(date_part, time_part).seconds
end
end
Expand Down Expand Up @@ -85,7 +86,7 @@ def resolve_time(counterpart)
end
Time.parse [filled_in_date_part, filled_in_time_part].join('T')
end
def resolve_offset
def offset
Duration.new(date_part, time_part).seconds
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/timeframe/version.rb
@@ -1,3 +1,3 @@
class Timeframe
VERSION = '0.1.1'
VERSION = '0.2.0'
end
9 changes: 0 additions & 9 deletions spec/spec_helper.rb

This file was deleted.

11 changes: 11 additions & 0 deletions test/helper.rb
@@ -0,0 +1,11 @@
require 'rubygems'
require 'bundler/setup'

require 'minitest/spec'
require 'minitest/autorun'
require 'minitest/reporters'
MiniTest::Unit.runner = MiniTest::SuiteRunner.new
MiniTest::Unit.runner.reporters << MiniTest::Reporters::SpecReporter.new

require 'timeframe'
require 'timeframe/core_ext/array'
8 changes: 4 additions & 4 deletions spec/timeframe_spec.rb → test/test_timeframe.rb
@@ -1,4 +1,4 @@
require File.expand_path('../spec_helper', __FILE__)
require 'helper'

describe Timeframe do
describe 'initialization' do
Expand Down Expand Up @@ -275,11 +275,11 @@
end
it 'understands JSON' do
json =<<-EOS
{"startDate":"2009-05-01", "endDate":"2009-06-01"}
2009-05-01/2009-06-01
EOS
Timeframe.parse(json).must_equal Timeframe.new(:year => 2009, :month => 5)
end
it 'understands a Ruby hash' do
it 'understands a particular style of Ruby hash we used to emit (deprecated)' do
hsh = { :startDate => '2009-05-01', :endDate => '2009-06-01' }
Timeframe.parse(hsh).must_equal Timeframe.new(:year => 2009, :month => 5)
Timeframe.parse(hsh.stringify_keys).must_equal Timeframe.new(:year => 2009, :month => 5)
Expand All @@ -288,7 +288,7 @@

describe '#to_json' do
it 'should generate JSON (test fails on ruby 1.8)' do
Timeframe.new(:year => 2009).to_json.must_equal %({"startDate":"2009-01-01","endDate":"2010-01-01"})
Timeframe.new(:year => 2009).to_json.must_equal %{2009-01-01/2010-01-01}
end
it 'understands its own #to_json' do
t = Timeframe.new(:year => 2009, :month => 5)
Expand Down

0 comments on commit 6747f1b

Please sign in to comment.