├── spec ├── spec_helper.rb └── fuzzytime_spec.rb ├── .travis.yml ├── .gitignore ├── lib ├── fuzzytime │ └── version.rb └── fuzzytime.rb ├── Gemfile ├── Rakefile ├── README.md ├── fuzzytime.gemspec └── LICENSE /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'fuzzytime' 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | rvm: 2 | - 1.8.7 3 | - 1.9.2 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .bundle 3 | Gemfile.lock 4 | pkg/* 5 | -------------------------------------------------------------------------------- /lib/fuzzytime/version.rb: -------------------------------------------------------------------------------- 1 | module Fuzzytime 2 | VERSION = "0.0.2" 3 | end 4 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | # Specify your gem's dependencies in fuzzytime.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require 'rspec/core/rake_task' 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This simple gem outputs time in a "fuzzy" manner. 2 | 3 | Note: this is my first gem so please do tell if you find anything wrong with it! 4 | 5 | It takes the Time class as input or defaults to the current local time if no time is provided. 6 | 7 | Example usage: 8 | 9 | ```ruby 10 | require 'fuzzytime' 11 | 12 | t = Time.local(2012,1,1,0,0) 13 | Fuzzytime.fuzzy_time(t) => "midnight" 14 | 15 | t = Time.local(2012,3,17,4,20) 16 | Fuzzytime.fuzzy_time(t) => "20 past 4" 17 | 18 | t = Time.local(2012,3,17,9,45) 19 | Fuzzytime.fuzzy_time(t) => "quarter til 10" 20 | 21 | t = Time.local(2012,1,1,12,0) 22 | Fuzzytime.fuzzy_time(t) => "noon" 23 | 24 | t = Time.local(2012,3,17,17,15) 25 | Fuzzytime.fuzzy_time(t) => "quarter past 5" 26 | ``` 27 | -------------------------------------------------------------------------------- /lib/fuzzytime.rb: -------------------------------------------------------------------------------- 1 | #require "fuzzytime/version" 2 | 3 | module Fuzzytime 4 | 5 | def self.fuzzy_time(t = Time.now) 6 | 7 | min = t.min 8 | hour = t.hour 9 | 10 | case min 11 | when 0 12 | when 1..30 13 | rel = "past" 14 | min = min 15 | when 31..59 16 | rel = "til" 17 | min = 60 - min 18 | hour = (hour + 1) % 24 19 | end 20 | 21 | min = "quarter" if min == 15 22 | min = "half" if min == 30 23 | 24 | case hour 25 | when 0 26 | hour = "midnight" 27 | when 12 28 | hour = "noon" 29 | else 30 | hour = hour % 12 31 | suffix = t.min.zero? && hour.is_a?(Integer) ? "o'clock" : "" 32 | min = nil if t.min.zero? 33 | end 34 | 35 | "#{min} #{rel} #{hour} #{suffix}".strip 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /fuzzytime.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "fuzzytime/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "fuzzytime" 7 | s.version = Fuzzytime::VERSION 8 | s.authors = ["Paul Maki"] 9 | s.email = ["paul.maki@gmail.com"] 10 | s.homepage = "https://github.com/paulmaki/fuzzytime" 11 | s.summary = %q{Converts times into "fuzzy" output} 12 | s.description = %q{ 13 | Sample output: 14 | 2:30AM => half past 2 15 | 7:50AM => 10 til 8 16 | 12:00PM => noon 17 | 5:15PM => quarter past 5 18 | 9:00PM => 9 o'clock 19 | 10:17PM => 17 past 10 20 | 12:00AM => midnight 21 | } 22 | 23 | s.rubyforge_project = "fuzzytime" 24 | 25 | s.files = `git ls-files`.split("\n") 26 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 27 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 28 | s.require_paths = ["lib"] 29 | 30 | s.add_development_dependency "rake" 31 | s.add_development_dependency "rspec" 32 | end 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Paul Maki 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do 8 | so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /spec/fuzzytime_spec.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | require 'spec_helper' 3 | 4 | describe Fuzzytime do 5 | 6 | it "should display midnight correctly" do 7 | t = Time.local(2012,1,1,0,0) 8 | Fuzzytime.fuzzy_time(t).should include "midnight" 9 | end 10 | 11 | it "should display noon correctly" do 12 | t = Time.local(2012,1,1,12,0) 13 | Fuzzytime.fuzzy_time(t).should include "noon" 14 | end 15 | 16 | it "should display 5 past correctly" do 17 | t = Time.local(2012,1,1,2,5) 18 | Fuzzytime.fuzzy_time(t).should include "5 past" 19 | end 20 | 21 | it "should display 10 past correctly" do 22 | t = Time.local(2012,1,1,12,10) 23 | Fuzzytime.fuzzy_time(t).should include "10 past" 24 | end 25 | 26 | it "should display 15 past correctly" do 27 | t = Time.local(2012,1,1,12,15) 28 | Fuzzytime.fuzzy_time(t).should include "quarter" 29 | Fuzzytime.fuzzy_time(t).should include "past" 30 | end 31 | 32 | it "should display 20 past correctly" do 33 | t = Time.local(2012,1,1,2,20) 34 | Fuzzytime.fuzzy_time(t).should include "20 past" 35 | end 36 | 37 | it "should display 25 past correctly" do 38 | t = Time.local(2012,1,1,2,25) 39 | Fuzzytime.fuzzy_time(t).should include "25 past" 40 | end 41 | 42 | it "should display 30 past correctly" do 43 | t = Time.local(2012,1,1,2,30) 44 | Fuzzytime.fuzzy_time(t).should include("half past") 45 | end 46 | 47 | it "should display 35 past correctly" do 48 | t = Time.local(2012,1,1,2,35) 49 | Fuzzytime.fuzzy_time(t).should include("25") 50 | Fuzzytime.fuzzy_time(t).should include("til") 51 | end 52 | 53 | it "should display 40 past correctly" do 54 | t = Time.local(2012,1,1,1,40) 55 | Fuzzytime.fuzzy_time(t).should include("20") 56 | Fuzzytime.fuzzy_time(t).should include("til") 57 | end 58 | 59 | it "should display 45 past correctly" do 60 | t = Time.local(2012,1,1,1,45) 61 | Fuzzytime.fuzzy_time(t).should include("quarter") 62 | Fuzzytime.fuzzy_time(t).should include("til") 63 | end 64 | 65 | it "should display 50 past correctly" do 66 | t = Time.local(2012,1,1,1,50) 67 | Fuzzytime.fuzzy_time(t).should include("10") 68 | Fuzzytime.fuzzy_time(t).should include("til") 69 | end 70 | 71 | it "should display 55 past correctly" do 72 | t = Time.local(2012,1,1,1,55) 73 | Fuzzytime.fuzzy_time(t).should include("5") 74 | Fuzzytime.fuzzy_time(t).should include("til") 75 | end 76 | 77 | it "should read 10PM correctly" do 78 | t = Time.local(2012,1,1,22,00) 79 | Fuzzytime.fuzzy_time(t).should == "10 o'clock" 80 | end 81 | 82 | it "should display 11:45am correctly" do 83 | t = Time.local(2012,1,1,11,45) 84 | Fuzzytime.fuzzy_time(t).should == "quarter til noon" 85 | end 86 | 87 | it "should display 11:45pm correctly" do 88 | t = Time.local(2012,1,1,23,45) 89 | Fuzzytime.fuzzy_time(t).should == "quarter til midnight" 90 | end 91 | 92 | it "should display 12:15am correctly" do 93 | t1 = Time.local(2012,1,1,0,15) 94 | Fuzzytime.fuzzy_time(t1).should == "quarter past midnight" 95 | end 96 | 97 | it "should display 12:15pm correctly" do 98 | t1 = Time.local(2012,1,1,12,15) 99 | Fuzzytime.fuzzy_time(t1).should == "quarter past noon" 100 | end 101 | 102 | it "should display 5:15am and 5:15pm correctly" do 103 | t1 = Time.local(2012,1,1,5,15) 104 | t2 = Time.local(2012,1,1,17,15) 105 | Fuzzytime.fuzzy_time(t1).should == "quarter past 5" 106 | Fuzzytime.fuzzy_time(t2).should == "quarter past 5" 107 | end 108 | 109 | it "should display 4:17pm correctly" do 110 | t1 = Time.local(2012,1,1,16,17) 111 | Fuzzytime.fuzzy_time(t1).should == "17 past 4" 112 | end 113 | 114 | end 115 | --------------------------------------------------------------------------------