graceful_rounding 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/README.md +18 -0
- data/Rakefile +2 -0
- data/graceful_rounding.gemspec +21 -0
- data/lib/graceful_rounding/version.rb +3 -0
- data/lib/graceful_rounding.rb +47 -0
- data/test/test_graceful_rounding.rb +28 -0
- metadata +64 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Graceful Rounding
|
2
|
+
|
3
|
+
A simple gem to round to the nearest 15 minutes inside a grace period.
|
4
|
+
|
5
|
+
Adds a method to Time class. Rounds Time object using (@time.min) minutes within a grace period (+/- 7 min of the quarter hour)
|
6
|
+
|
7
|
+
Tested under:
|
8
|
+
|
9
|
+
* Ruby 1.9.2p136 tested
|
10
|
+
|
11
|
+
## Example usage
|
12
|
+
|
13
|
+
|
14
|
+
@t = Time.new(2011,03,26,19,06,0, "-04:00")
|
15
|
+
@t.graceful_rounding #=> 2011,03,26,19,00,0, "-04:00"
|
16
|
+
|
17
|
+
|
18
|
+
Chase Southard 2011 released under the same terms as Ruby itself
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "graceful_rounding/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "graceful_rounding"
|
7
|
+
s.version = GracefulRounding::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Chase Southard"]
|
10
|
+
s.email = ["chase.southard@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/chaserx/graceful-rounding"
|
12
|
+
s.summary = %q{Rounds a Time object back based on a +/- 7 minute grace period. Ruby 1.9.2p136 tested}
|
13
|
+
s.description = %q{Time clocks sometimes round to the nearest quarter hour with a grace period. This gem adds a method to the Time class to gracefully round the time object}
|
14
|
+
|
15
|
+
s.rubyforge_project = "graceful_rounding"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module GracefulRounding
|
2
|
+
|
3
|
+
# takes a Time object and rounds the minutes within a grace period (+/- 7 min of the quarter hour)
|
4
|
+
# returns a new time object
|
5
|
+
# @t = Time.new(2011,03,26,19,06,0, "-04:00")
|
6
|
+
# @t.graceful_rounding #=> 2011,03,26,19,00,0, "-04:00"
|
7
|
+
def graceful_rounding
|
8
|
+
raise ArgumentError, 'Argument is not Time class' unless self.is_a? Time
|
9
|
+
|
10
|
+
@time = self
|
11
|
+
mins = @time.min
|
12
|
+
|
13
|
+
if mins == 00
|
14
|
+
return @time
|
15
|
+
elsif (1..7).cover?(mins)
|
16
|
+
return @time -= (mins*60)
|
17
|
+
elsif (8..14).cover?(mins)
|
18
|
+
return @time += (15 - mins)*60
|
19
|
+
elsif mins == 15
|
20
|
+
return @time
|
21
|
+
elsif (16..22).cover?(mins)
|
22
|
+
return @time -= (mins - 15)*60
|
23
|
+
elsif (23..29).cover?(mins)
|
24
|
+
return @time += (30 - mins)*60
|
25
|
+
elsif mins == 30
|
26
|
+
return @time
|
27
|
+
elsif (31..37).cover?(mins)
|
28
|
+
return @time -= (mins - 30)*60
|
29
|
+
elsif (38..44).cover?(mins)
|
30
|
+
return @time += (45 - mins)*60
|
31
|
+
elsif mins == 45
|
32
|
+
return @time
|
33
|
+
elsif (46..52).cover?(mins)
|
34
|
+
return @time -= (mins - 45)*60
|
35
|
+
elsif (53..59).cover?(mins)
|
36
|
+
return @time += (60 - mins)*60
|
37
|
+
else
|
38
|
+
raise 'value of Time.min is not between 00..59'
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
class Time
|
46
|
+
include GracefulRounding
|
47
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative '../lib/graceful_rounding.rb'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
class TestGracefulRounding < MiniTest::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
@t = Time.new(2011,03,25,21,05,0, "-04:00")
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_graceful_rounding
|
13
|
+
assert_equal Time.new(2011, 03,25,21,00,0, "-04:00"), @t.graceful_rounding
|
14
|
+
|
15
|
+
assert_equal Time.new(2011, 03,26,19,00,0, "-04:00"), Time.new(2011, 03,26,19,00,0, "-04:00").graceful_rounding
|
16
|
+
assert_equal Time.new(2011, 03,26,19,30,0, "-04:00"), Time.new(2011, 03,26,19,30,0, "-04:00").graceful_rounding
|
17
|
+
assert_equal Time.new(2011, 03,26,19,15,0, "-04:00"), Time.new(2011, 03,26,19,15,0, "-04:00").graceful_rounding
|
18
|
+
assert_equal Time.new(2011, 03,26,19,45,0, "-04:00"), Time.new(2011, 03,26,19,45,0, "-04:00").graceful_rounding
|
19
|
+
|
20
|
+
assert_equal Time.new(2011, 03,26,19,00,0, "-04:00"), Time.new(2011, 03,26,19,06,0, "-04:00").graceful_rounding
|
21
|
+
assert_equal Time.new(2011, 03,26,19,15,0, "-04:00"), Time.new(2011, 03,26,19,18,0, "-04:00").graceful_rounding
|
22
|
+
assert_equal Time.new(2011, 03,26,19,45,0, "-04:00"), Time.new(2011, 03,26,19,38,0, "-04:00").graceful_rounding
|
23
|
+
assert_equal Time.new(2011, 03,26,20,00,0, "-04:00"), Time.new(2011, 03,26,19,55,0, "-04:00").graceful_rounding
|
24
|
+
|
25
|
+
refute_equal Time.new(2011, 03,26,19,30,0, "-04:00"), Time.new(2011, 03,26,19,18,0, "-04:00").graceful_rounding
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: graceful_rounding
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chase Southard
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-26 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Time clocks sometimes round to the nearest quarter hour with a grace period. This gem adds a method to the Time class to gracefully round the time object
|
18
|
+
email:
|
19
|
+
- chase.southard@gmail.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- Gemfile
|
29
|
+
- README.md
|
30
|
+
- Rakefile
|
31
|
+
- graceful_rounding.gemspec
|
32
|
+
- lib/graceful_rounding.rb
|
33
|
+
- lib/graceful_rounding/version.rb
|
34
|
+
- test/test_graceful_rounding.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: https://github.com/chaserx/graceful-rounding
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project: graceful_rounding
|
59
|
+
rubygems_version: 1.5.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Rounds a Time object back based on a +/- 7 minute grace period. Ruby 1.9.2p136 tested
|
63
|
+
test_files:
|
64
|
+
- test/test_graceful_rounding.rb
|