Permalink
Browse files

Version 0.0.1.alpha

  • Loading branch information...
0 parents commit 4eb2c489878109dbf6a381c3ae235ff4e0b96c89 @paulmaki committed Feb 14, 2012
Showing with 239 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +4 −0 Gemfile
  3. +19 −0 LICENSE
  4. +26 −0 README.md
  5. +6 −0 Rakefile
  6. +31 −0 fuzzytime.gemspec
  7. +36 −0 lib/fuzzytime.rb
  8. +3 −0 lib/fuzzytime/version.rb
  9. +109 −0 spec/fuzzytime_spec.rb
  10. +1 −0 spec/spec_helper.rb
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in fuzzytime.gemspec
+gemspec
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Paul Maki
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
@@ -0,0 +1,26 @@
+This simple gem outputs time in a "fuzzy" manner.
+
+Note: this is my first gem so please do tell if you find anything wrong with it!
+
+It takes the Time class as input or defaults to the current local time if no time is provided.
+
+Example usage:
+
+```ruby
+require 'fuzzytime'
+
+t = Time.local(2012,1,1,0,0)
+Fuzzytime.fuzzy_time(t) => "midnight"
+
+t = Time.local(2012,3,17,4,20)
+Fuzzytime.fuzzy_time(t) => "20 past 4"
+
+t = Time.local(2012,3,17,9,45)
+Fuzzytime.fuzzy_time(t) => "quarter til 10"
+
+t = Time.local(2012,1,1,12,0)
+Fuzzytime.fuzzy_time(t) => "noon"
+
+t = Time.local(2012,3,17,17,15)
+Fuzzytime.fuzzy_time(t) => "quarter past 5"
+```
@@ -0,0 +1,6 @@
+require "bundler/gem_tasks"
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec)
+
+task default: :spec
@@ -0,0 +1,31 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "fuzzytime/version"
+
+Gem::Specification.new do |s|
+ s.name = "fuzzytime"
+ s.version = Fuzzytime::VERSION
+ s.authors = ["Paul Maki"]
+ s.email = ["paul.maki@gmail.com"]
+ s.homepage = "https://github.com/paulmaki/fuzzytime"
+ s.summary = %q{Converts times into "fuzzy" output}
+ s.description = %q{
+ Sample output:
+ 2:30AM => half past 2
+ 7:50AM => 10 til 8
+ 12:00PM => noon
+ 5:15PM => quarter past 5
+ 9:00PM => 9 o'clock
+ 10:17PM => 17 past 10
+ 12:00AM => midnight
+ }
+
+ s.rubyforge_project = "fuzzytime"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_development_dependency "rspec"
+end
@@ -0,0 +1,36 @@
+#require "fuzzytime/version"
+
+module Fuzzytime
+
+ def self.fuzzy_time(t = Time.now)
+
+ min = t.min
+ hour = t.hour
+
+ case min
+ when 0
+ when 1..30
+ rel = "past"
+ min = min
+ when 31..59
+ rel = "til"
+ min = 60 - min
+ hour = (hour + 1) % 24
+ end
+
+ min = "quarter" if min == 15
+ min = "half" if min == 30
+
+ case hour
+ when 0
+ hour = "midnight"
+ when 12
+ hour = "noon"
+ else
+ hour = hour % 12
+ suffix = t.min.zero? && hour.is_a?(Integer) ? "o'clock" : ""
+ end
+
+ "#{min} #{rel} #{hour} #{suffix}".strip
+ end
+end
@@ -0,0 +1,3 @@
+module Fuzzytime
+ VERSION = "0.0.1.alpha"
+end
@@ -0,0 +1,109 @@
+# encoding: utf-8
+require 'spec_helper'
+
+describe Fuzzytime do
+
+ it "should display midnight correctly" do
+ t = Time.local(2012,1,1,0,0)
+ Fuzzytime.fuzzy_time(t).should include "midnight"
+ end
+
+ it "should display noon correctly" do
+ t = Time.local(2012,1,1,12,0)
+ Fuzzytime.fuzzy_time(t).should include "noon"
+ end
+
+ it "should display 5 past correctly" do
+ t = Time.local(2012,1,1,2,5)
+ Fuzzytime.fuzzy_time(t).should include "5 past"
+ end
+
+ it "should display 10 past correctly" do
+ t = Time.local(2012,1,1,12,10)
+ Fuzzytime.fuzzy_time(t).should include "10 past"
+ end
+
+ it "should display 15 past correctly" do
+ t = Time.local(2012,1,1,12,15)
+ Fuzzytime.fuzzy_time(t).should include "quarter"
+ Fuzzytime.fuzzy_time(t).should include "past"
+ end
+
+ it "should display 20 past correctly" do
+ t = Time.local(2012,1,1,2,20)
+ Fuzzytime.fuzzy_time(t).should include "20 past"
+ end
+
+ it "should display 25 past correctly" do
+ t = Time.local(2012,1,1,2,25)
+ Fuzzytime.fuzzy_time(t).should include "25 past"
+ end
+
+ it "should display 30 past correctly" do
+ t = Time.local(2012,1,1,2,30)
+ Fuzzytime.fuzzy_time(t).should include("half past")
+ end
+
+ it "should display 35 past correctly" do
+ t = Time.local(2012,1,1,2,35)
+ Fuzzytime.fuzzy_time(t).should include("25")
+ Fuzzytime.fuzzy_time(t).should include("til")
+ end
+
+ it "should display 40 past correctly" do
+ t = Time.local(2012,1,1,1,40)
+ Fuzzytime.fuzzy_time(t).should include("20")
+ Fuzzytime.fuzzy_time(t).should include("til")
+ end
+
+ it "should display 45 past correctly" do
+ t = Time.local(2012,1,1,1,45)
+ Fuzzytime.fuzzy_time(t).should include("quarter")
+ Fuzzytime.fuzzy_time(t).should include("til")
+ end
+
+ it "should display 50 past correctly" do
+ t = Time.local(2012,1,1,1,50)
+ Fuzzytime.fuzzy_time(t).should include("10")
+ Fuzzytime.fuzzy_time(t).should include("til")
+ end
+
+ it "should display 55 past correctly" do
+ t = Time.local(2012,1,1,1,55)
+ Fuzzytime.fuzzy_time(t).should include("5")
+ Fuzzytime.fuzzy_time(t).should include("til")
+ end
+
+ it "should display 11:45am correctly" do
+ t = Time.local(2012,1,1,11,45)
+ Fuzzytime.fuzzy_time(t).should == "quarter til noon"
+ end
+
+ it "should display 11:45pm correctly" do
+ t = Time.local(2012,1,1,23,45)
+ Fuzzytime.fuzzy_time(t).should == "quarter til midnight"
+ end
+
+ it "should display 12:15am correctly" do
+ t1 = Time.local(2012,1,1,0,15)
+ Fuzzytime.fuzzy_time(t1).should == "quarter past midnight"
+ end
+
+ it "should display 12:15pm correctly" do
+ t1 = Time.local(2012,1,1,12,15)
+ Fuzzytime.fuzzy_time(t1).should == "quarter past noon"
+ end
+
+ it "should display 5:15am and 5:15pm correctly" do
+ t1 = Time.local(2012,1,1,5,15)
+ t2 = Time.local(2012,1,1,17,15)
+ Fuzzytime.fuzzy_time(t1).should == "quarter past 5"
+ Fuzzytime.fuzzy_time(t2).should == "quarter past 5"
+ end
+
+ it "should display 4:17pm correctly" do
+ t1 = Time.local(2012,1,1,16,17)
+ Fuzzytime.fuzzy_time(t1).should == "17 past 4"
+ end
+
+end
@@ -0,0 +1 @@
+require 'fuzzytime'

0 comments on commit 4eb2c48

Please sign in to comment.