time_date_helpers 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/time_date_helpers/time_helpers.rb +37 -2
- data/lib/time_date_helpers/version.rb +1 -1
- data/tests/date_tests.rb +4 -2
- data/tests/time_tests.rb +3 -3
- data/time_date_helpers.gemspec +1 -0
- metadata +24 -20
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d7f2a9d9ffd2107fe83c23cabe46eefa9067acaa15884b2cfe103f92c85ad44b
|
4
|
+
data.tar.gz: 902901cc08f369a542bb07ccfb9a6a26d7aec64fce7724546d7853d45358f697
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c212c758d9a911efe814a5cf947cd4c5947c8d756aa559e973730f733d50c4ccf649fa2f485c7defd8d0643750821ab36a3dfba06afab70efaba9d3397d0ff43
|
7
|
+
data.tar.gz: 6540be1a5af14f4285e627a0027b2ea05a998cd17969dd1d86353a89509a520c53df16f46ee3088133d07f2caff12b8a954d05a648b8fc2ef3517fca7a747c00
|
@@ -6,8 +6,8 @@ module TimeDateHelpers
|
|
6
6
|
# Merge whatever options the user has selected with the defaults
|
7
7
|
options.merge!(opt)
|
8
8
|
|
9
|
-
# make sure that increment is a
|
10
|
-
return nil unless options[:increment].class ==
|
9
|
+
# make sure that increment is a Integer between 1 and 59
|
10
|
+
return nil unless options[:increment].class == Integer
|
11
11
|
return nil if (options[:increment] > 59 || options[:increment] < 1)
|
12
12
|
|
13
13
|
# Set up some local variables used in calculations
|
@@ -35,6 +35,41 @@ module TimeDateHelpers
|
|
35
35
|
Time.new(time.year, time.month, time.day, (time.hour+hr_adj), new_min, 0)
|
36
36
|
end
|
37
37
|
|
38
|
+
def round_hours(start_time, end_time, opt={})
|
39
|
+
# Set the default options
|
40
|
+
options = {:direction => :up, :increment => 15}
|
41
|
+
# Merge whatever options the user has selected with the defaults
|
42
|
+
options.merge!(opt)
|
43
|
+
|
44
|
+
# make sure that increment is a Integer between 1 and 59
|
45
|
+
return nil unless options[:increment].class == Integer
|
46
|
+
return nil if (options[:increment] > 59 || options[:increment] < 1)
|
47
|
+
|
48
|
+
# Set up some local variables used in calculations
|
49
|
+
new_start_min, new_end_min, hr_adj = start_time.min, end_time.min, 0
|
50
|
+
total_segments = (60.0/options[:increment]).ceil # Need to round up
|
51
|
+
closest_segment_down = (new_min/options[:increment])
|
52
|
+
closest_segment_up = (new_min/options[:increment]) + 1
|
53
|
+
|
54
|
+
# Now it is time to actually adjust the minutes (and perhaps hour)
|
55
|
+
if (new_min - options[:increment]*closest_segment_down) == 0
|
56
|
+
new_min # we are on an increment, so just return the minutes
|
57
|
+
elsif options[:direction] == :up
|
58
|
+
# if rounding up, need to check if up against the end-of-the-hour limit
|
59
|
+
if closest_segment_up == total_segments
|
60
|
+
new_min = 00; hr_adj=1 # near end of hour, so move up to next hour
|
61
|
+
else
|
62
|
+
new_min = options[:increment]*(closest_segment_up)
|
63
|
+
end
|
64
|
+
else
|
65
|
+
# we are rounding down, which is very easy
|
66
|
+
new_min = options[:increment]*(closest_segment_down)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Finally, return the adjusted time
|
70
|
+
Time.new(time.year, time.month, time.day, (time.hour+hr_adj), new_min, 0)
|
71
|
+
end
|
72
|
+
|
38
73
|
def humanize_time(time, opt={})
|
39
74
|
# Set the default options
|
40
75
|
options = {:ampm => true, :with_seconds => false}
|
data/tests/date_tests.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require
|
1
|
+
require "minitest/autorun"
|
2
2
|
require 'chronic'
|
3
3
|
require 'time_date_helpers'
|
4
4
|
|
5
|
-
class DateHelperTests < Test
|
5
|
+
class DateHelperTests < Minitest::Test
|
6
6
|
|
7
7
|
# Test humanize_date method
|
8
8
|
def test_humanize_date_works
|
@@ -45,6 +45,7 @@ class DateHelperTests < Test::Unit::TestCase
|
|
45
45
|
date5 = "April 1, 1999"
|
46
46
|
assert_equal 10, convert_to_date(date1).month
|
47
47
|
assert_equal Date.today + 1, convert_to_date(date2)
|
48
|
+
# note that the assertion below will fail right after the switch to/from DST
|
48
49
|
assert_equal Date.today - 1, convert_to_date(date3)
|
49
50
|
assert_equal 25, convert_to_date(date4).day
|
50
51
|
assert_equal 1999, convert_to_date(date5).year
|
@@ -64,6 +65,7 @@ class DateHelperTests < Test::Unit::TestCase
|
|
64
65
|
today = Date.today
|
65
66
|
assert_equal 10, convert_to_datetime(datetime1).month
|
66
67
|
assert_equal Time.new((today).year, (today).month, (today+1).day, 12, 0, 0), convert_to_datetime(datetime2)
|
68
|
+
# note that the assertion below will fail right after the switch to/from DST
|
67
69
|
assert_equal Time.new((today).year, (today).month, (today-1).day, 15, 0, 0), convert_to_datetime(datetime3)
|
68
70
|
assert_equal 8, convert_to_datetime(datetime4).hour
|
69
71
|
assert_equal 22, convert_to_datetime(datetime5).hour
|
data/tests/time_tests.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require "minitest/autorun"
|
2
2
|
require 'time_date_helpers'
|
3
3
|
|
4
|
-
class TimeHelperTests < Test
|
4
|
+
class TimeHelperTests < Minitest::Test
|
5
5
|
|
6
6
|
def test_default_settings
|
7
7
|
quarter1 = Time.new(2012, 04, 30, 9, 6, 0)
|
@@ -109,7 +109,7 @@ class TimeHelperTests < Test::Unit::TestCase
|
|
109
109
|
assert_equal '03:14:15 pm', humanize_time(time4, :with_seconds => true)
|
110
110
|
end
|
111
111
|
|
112
|
-
def
|
112
|
+
def test_humanize_time_fails_for_nontimes
|
113
113
|
time1 = nil
|
114
114
|
time2 = "10/04/1986"
|
115
115
|
time3 = 3.14159
|
data/time_date_helpers.gemspec
CHANGED
@@ -6,6 +6,7 @@ Gem::Specification.new do |s|
|
|
6
6
|
s.name = "time_date_helpers"
|
7
7
|
s.version = TimeDateHelpers::VERSION
|
8
8
|
s.authors = ["Klingon Code Warrior"]
|
9
|
+
s.licenses = ['MIT']
|
9
10
|
s.email = ["profh@cmu.edu"]
|
10
11
|
s.homepage = "https://github.com/profh/time_date_helpers"
|
11
12
|
s.summary = %q{A series of helpers to deal with time and date issues in Rails.}
|
metadata
CHANGED
@@ -1,38 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_date_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.4
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Klingon Code Warrior
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2019-11-19 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: chronic
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
25
27
|
- !ruby/object:Gem::Dependency
|
26
28
|
name: chronic
|
27
|
-
requirement:
|
28
|
-
none: false
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
29
30
|
requirements:
|
30
|
-
- -
|
31
|
+
- - ">="
|
31
32
|
- !ruby/object:Gem::Version
|
32
33
|
version: '0'
|
33
34
|
type: :runtime
|
34
35
|
prerelease: false
|
35
|
-
version_requirements:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
36
41
|
description: The initial version has only a few date and time helpers and was created
|
37
42
|
primarily for teaching purposes (how to create gems), but does have some value.
|
38
43
|
I will try to get back to this gem and add more methods as time allows.
|
@@ -42,7 +47,7 @@ executables: []
|
|
42
47
|
extensions: []
|
43
48
|
extra_rdoc_files: []
|
44
49
|
files:
|
45
|
-
- .gitignore
|
50
|
+
- ".gitignore"
|
46
51
|
- Gemfile
|
47
52
|
- README.mkd
|
48
53
|
- Rakefile
|
@@ -54,28 +59,27 @@ files:
|
|
54
59
|
- tests/time_tests.rb
|
55
60
|
- time_date_helpers.gemspec
|
56
61
|
homepage: https://github.com/profh/time_date_helpers
|
57
|
-
licenses:
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata: {}
|
58
65
|
post_install_message:
|
59
66
|
rdoc_options: []
|
60
67
|
require_paths:
|
61
68
|
- lib
|
62
69
|
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
-
none: false
|
64
70
|
requirements:
|
65
|
-
- -
|
71
|
+
- - ">="
|
66
72
|
- !ruby/object:Gem::Version
|
67
73
|
version: '0'
|
68
74
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
-
none: false
|
70
75
|
requirements:
|
71
|
-
- -
|
76
|
+
- - ">="
|
72
77
|
- !ruby/object:Gem::Version
|
73
78
|
version: '0'
|
74
79
|
requirements: []
|
75
80
|
rubyforge_project: time_date_helpers
|
76
|
-
rubygems_version:
|
81
|
+
rubygems_version: 2.7.6.2
|
77
82
|
signing_key:
|
78
|
-
specification_version:
|
83
|
+
specification_version: 4
|
79
84
|
summary: A series of helpers to deal with time and date issues in Rails.
|
80
85
|
test_files: []
|
81
|
-
has_rdoc:
|