time_date_helpers 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ time_date_helpers
2
+ ==========
3
+ This is a series of methods written to handle time and date issues in Ruby and/or Rails. To be honest, it is more of a teaching example than anything else at this point as there are Ruby equivalents (like strftime) that users may prefer. The original application was handling time and date issues of a ice cream shop application (tracking shift start and end times, dates for assignments and the like). In the class we created some modules in lib/ to add custom methods and in the next step we moved those methods into a gem (i.e., time_date_helpers).
4
+
5
+ Installation
6
+ ------------
7
+ Installing this gem is pretty simple -- just type on the command line:
8
+
9
+ ```
10
+ $ gem install time_date_helpers
11
+ ```
12
+
13
+ And add this gem into any other code with:
14
+
15
+ ```
16
+ require 'rubygems'
17
+ require 'chronic'
18
+ require 'time_date_helpers'
19
+ ```
20
+ Note that in irb the chronic gem is required for some of the date helpers. There is a dependency specified in the gem for chronic.
21
+
22
+
23
+ Usage
24
+ ------------
25
+ There are basically two sections to this gem:
26
+
27
+ * Date helpers
28
+ * Time helpers
29
+
30
+ (Note: SpaceTime helpers will be added _relatively_ soon...)
31
+
32
+
33
+ ### Date helpers ###
34
+
35
+ Most are self-explanatory and some may have other Ruby equivalents already.
36
+ Right now all I have in here are three formatting/conversion methods, but might expand this over time to add some other useful methods. The methods in this module were included in Object, so you can access them directly within main, if you wish.
37
+
38
+ **humanize_date** -- This simply takes a date or datetime and converts it to the standard mm/dd/yyyy display using strftime. If you pass in the optional argument :style => :full, then you will get the output formatted as full month name, day and year (e.g., "January 15, 1929"). (This latter option also controls for the weird issue with strftime adding a space to the day if it is less than 10; see tests for examples.)
39
+
40
+ **convert_to_date** -- This takes a string and attempts to parse it using the Chronic gem. If the date_string can be parsed, then it will return a date; else it will return nil. Because it uses chronic, it can take strings like "tomorrow" or "yesterday at 3pm" or "1 hour ago" and process them into datetime stamps. In this case, however, we are returning a date object, not a datetime object.
41
+
42
+ **convert_to_datetime** -- Just like the previous method, this takes a string and attempts to parse it using the Chronic gem. If the date_string can be parsed, then it will return a datetime; else it will return nil. The returning value is a datetime object.
43
+
44
+
45
+ ### Time helpers ###
46
+
47
+ Most are self-explanatory and some may have other Ruby equivalents already.
48
+ (Actually, there is only two methods right now...) The methods in this module were included in Object, so you can access them directly within main, if you wish.
49
+
50
+ **round_minutes** -- Round the minutes in an hour up or down given a particular interval. The default setting is to round up based on the quarter-hour (15 minute intervals). For example, by default using this method would result in _9:04 # => 9:15; 9:42 # => 9:45; 9:55 # => 10:00_. You can pass along two options: direction (:up or :down) and increment (1 to 59). For example, round_minutes(time1, :increment => 10, :direction => :down) would round the minutes of time1 down to the closest 10 minute interval (e.g., 9:42 # => 9:40).
51
+
52
+ Since as noted earlier this was used to manage start and end times of shifts, the method will stop any rounding up at the top of the hour. Hence, if you had 8 minute increments and the minutes passed in was 59, it would round up to 00 and the next hour, not to 04 and the next hour.
53
+
54
+ **humanize_time** -- Similar to humanize_date, except that the default display is a 12-hour clock with am/pm and no seconds. If the option :ampm => false is included, then military (24-hour clock) is used. If the option :with_seconds => true is included, then seconds will be added to the display.
55
+
56
+
57
+ ### Note on usage ###
58
+
59
+ This gem has a set of basic unit tests associated with it. The tests provide other examples of what is possible using this methods and may help the user further understand how this gem can be applied.
@@ -34,6 +34,21 @@ module TimeDateHelpers
34
34
  # Finally, return the adjusted time
35
35
  Time.new(time.year, time.month, time.day, (time.hour+hr_adj), new_min, 0)
36
36
  end
37
+
38
+ def humanize_time(time, opt={})
39
+ # Set the default options
40
+ options = {:ampm => true, :with_seconds => false}
41
+ # Merge whatever options the user has selected with the defaults
42
+ options.merge!(opt)
43
+ # Make sure what is passed is legit
44
+ return nil if time.nil?
45
+ return nil unless time.class == Time
46
+ if options[:ampm]
47
+ options[:with_seconds] ? time.strftime("%I:%M:%S %P") : time.strftime("%I:%M %P")
48
+ else
49
+ options[:with_seconds] ? time.strftime("%H:%M:%S") : time.strftime("%H:%M")
50
+ end
51
+ end
37
52
  end
38
53
  end
39
54
 
@@ -1,3 +1,3 @@
1
1
  module TimeDateHelpers
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,75 @@
1
+ require 'test/unit'
2
+ require 'chronic'
3
+ require 'time_date_helpers'
4
+
5
+ class DateHelperTests < Test::Unit::TestCase
6
+
7
+ # Test humanize_date method
8
+ def test_humanize_date_works
9
+ date1 = Time.new(1776, 07, 04, 0, 0, 0)
10
+ date2 = Date.new(1929, 01, 15)
11
+ date3 = DateTime.new(1986, 10, 04, 0, 0, 0)
12
+ date4 = Time.new(2012, 12, 25, 0, 0, 0)
13
+ assert_equal '07/04/1776', humanize_date(date1)
14
+ assert_equal '01/15/1929', humanize_date(date2)
15
+ assert_equal '10/04/1986', humanize_date(date3)
16
+ assert_equal '12/25/2012', humanize_date(date4)
17
+ end
18
+
19
+ def test_humanize_date_works_with_option
20
+ date1 = Time.new(1776, 07, 04, 0, 0, 0)
21
+ date2 = Date.new(1929, 01, 15)
22
+ date3 = DateTime.new(1986, 10, 04, 0, 0, 0)
23
+ date4 = Time.new(2012, 12, 25, 0, 0, 0)
24
+ assert_equal 'July 4, 1776', humanize_date(date1, :style => :full)
25
+ assert_equal 'January 15, 1929', humanize_date(date2, :style => :full)
26
+ assert_equal 'October 4, 1986', humanize_date(date3, :style => :full)
27
+ assert_equal 'December 25, 2012', humanize_date(date4, :style => :full)
28
+ end
29
+
30
+ def test_humanize_date_fails_for_nondates
31
+ date1 = nil
32
+ date2 = "10/04/1986"
33
+ date3 = 3.14159
34
+ assert_nil humanize_date(date1)
35
+ assert_nil humanize_date(date2)
36
+ assert_nil humanize_date(date3)
37
+ end
38
+
39
+ # Test convert_to_date
40
+ def test_convert_to_date_works
41
+ date1 = "10/04/1986"
42
+ date2 = "tomorrow"
43
+ date3 = "yesterday at 3pm"
44
+ date4 = "2012-12-25"
45
+ date5 = "April 1, 1999"
46
+ assert_equal 10, convert_to_date(date1).month
47
+ assert_equal Date.today + 1, convert_to_date(date2)
48
+ assert_equal Date.today - 1, convert_to_date(date3)
49
+ assert_equal 25, convert_to_date(date4).day
50
+ assert_equal 1999, convert_to_date(date5).year
51
+ assert_nil convert_to_date(nil)
52
+ assert_nil convert_to_date("FRED!")
53
+ assert_nil convert_to_date(3.14159)
54
+ end
55
+
56
+ # Test test_convert_to_datetime
57
+ def test_convert_to_datetime_works
58
+ datetime1 = "10/04/1986"
59
+ datetime2 = "tomorrow"
60
+ datetime3 = "yesterday at 3pm"
61
+ datetime4 = "2012-12-25 08:46:01"
62
+ datetime5 = "April 1, 1999 at 10pm"
63
+ datetime6 = "1 hour ago"
64
+ today = Date.today
65
+ assert_equal 10, convert_to_datetime(datetime1).month
66
+ assert_equal Time.new((today).year, (today).month, (today+1).day, 12, 0, 0), convert_to_datetime(datetime2)
67
+ assert_equal Time.new((today).year, (today).month, (today-1).day, 15, 0, 0), convert_to_datetime(datetime3)
68
+ assert_equal 8, convert_to_datetime(datetime4).hour
69
+ assert_equal 22, convert_to_datetime(datetime5).hour
70
+ assert_equal (Time.now - 3600).hour, convert_to_datetime(datetime6).hour
71
+ assert_nil convert_to_datetime(nil)
72
+ assert_nil convert_to_datetime("FRED!")
73
+ assert_nil convert_to_datetime(3.14159)
74
+ end
75
+ end
@@ -0,0 +1,120 @@
1
+ require 'test/unit'
2
+ require 'time_date_helpers'
3
+
4
+ class TimeHelperTests < Test::Unit::TestCase
5
+
6
+ def test_default_settings
7
+ quarter1 = Time.new(2012, 04, 30, 9, 6, 0)
8
+ quarter2 = Time.new(2012, 04, 30, 9, 16, 0)
9
+ quarter3 = Time.new(2012, 04, 30, 9, 36, 0)
10
+ quarter4 = Time.new(2012, 04, 30, 9, 46, 0)
11
+ assert_equal('09:15', round_minutes(quarter1).strftime('%H:%M'))
12
+ assert_equal('09:30', round_minutes(quarter2).strftime('%H:%M'))
13
+ assert_equal('09:45', round_minutes(quarter3).strftime('%H:%M'))
14
+ assert_equal('10:00', round_minutes(quarter4).strftime('%H:%M'))
15
+ end
16
+
17
+ def test_even_increments
18
+ # Test cases where total segments is an integer (i.e., 60%increment==0)
19
+ inc = 10
20
+ time1 = Time.new(2012, 04, 30, 9, 6, 0)
21
+ time2 = Time.new(2012, 04, 30, 9, 16, 0)
22
+ time3 = Time.new(2012, 04, 30, 9, 36, 0)
23
+ time4 = Time.new(2012, 04, 30, 9, 46, 0)
24
+ time5 = Time.new(2012, 04, 30, 9, 50, 0)
25
+ time6 = Time.new(2012, 04, 30, 9, 56, 0)
26
+ assert_equal('09:10', round_minutes(time1, :increment => inc).strftime('%H:%M'))
27
+ assert_equal('09:20', round_minutes(time2, :increment => inc).strftime('%H:%M'))
28
+ assert_equal('09:40', round_minutes(time3, :increment => inc).strftime('%H:%M'))
29
+ assert_equal('09:50', round_minutes(time4, :increment => inc).strftime('%H:%M'))
30
+ assert_equal('09:50', round_minutes(time5, :increment => inc).strftime('%H:%M'))
31
+ assert_equal('10:00', round_minutes(time6, :increment => inc).strftime('%H:%M'))
32
+ end
33
+
34
+ def test_uneven_increments
35
+ # Test cases where total segments is not an integer (i.e., 60%increment!=0)
36
+ inc = 8
37
+ time1 = Time.new(2012, 04, 30, 9, 6, 0)
38
+ time2 = Time.new(2012, 04, 30, 9, 16, 0)
39
+ time3 = Time.new(2012, 04, 30, 9, 34, 0)
40
+ time4 = Time.new(2012, 04, 30, 9, 46, 0)
41
+ time5 = Time.new(2012, 04, 30, 9, 56, 0)
42
+ time6 = Time.new(2012, 04, 30, 9, 59, 0)
43
+ assert_equal('09:08', round_minutes(time1, :increment => inc).strftime('%H:%M'))
44
+ assert_equal('09:16', round_minutes(time2, :increment => inc).strftime('%H:%M'))
45
+ assert_equal('09:40', round_minutes(time3, :increment => inc).strftime('%H:%M'))
46
+ assert_equal('09:48', round_minutes(time4, :increment => inc).strftime('%H:%M'))
47
+ assert_equal('09:56', round_minutes(time5, :increment => inc).strftime('%H:%M'))
48
+ assert_equal('10:00', round_minutes(time6, :increment => inc).strftime('%H:%M'))
49
+ end
50
+
51
+ def test_direction_down
52
+ quarter1 = Time.new(2012, 04, 30, 9, 6, 0)
53
+ quarter2 = Time.new(2012, 04, 30, 9, 16, 0)
54
+ quarter3 = Time.new(2012, 04, 30, 9, 36, 0)
55
+ quarter4 = Time.new(2012, 04, 30, 9, 46, 0)
56
+ assert_equal('09:00', round_minutes(quarter1, :direction => :down).strftime('%H:%M'))
57
+ assert_equal('09:15', round_minutes(quarter2, :direction => :down).strftime('%H:%M'))
58
+ assert_equal('09:30', round_minutes(quarter3, :direction => :down).strftime('%H:%M'))
59
+ assert_equal('09:45', round_minutes(quarter4, :direction => :down).strftime('%H:%M'))
60
+ end
61
+
62
+ def test_increments_and_direction_together
63
+ inc = 8
64
+ time1 = Time.new(2012, 04, 30, 9, 6, 0)
65
+ time2 = Time.new(2012, 04, 30, 9, 16, 0)
66
+ time3 = Time.new(2012, 04, 30, 9, 34, 0)
67
+ time4 = Time.new(2012, 04, 30, 9, 46, 0)
68
+ time5 = Time.new(2012, 04, 30, 9, 56, 0)
69
+ time6 = Time.new(2012, 04, 30, 9, 59, 0)
70
+ assert_equal('09:00', round_minutes(time1, :increment => inc, :direction => :down).strftime('%H:%M'))
71
+ assert_equal('09:16', round_minutes(time2, :increment => inc, :direction => :down).strftime('%H:%M'))
72
+ assert_equal('09:32', round_minutes(time3, :increment => inc, :direction => :down).strftime('%H:%M'))
73
+ assert_equal('09:40', round_minutes(time4, :direction => :down, :increment => inc).strftime('%H:%M'))
74
+ assert_equal('09:56', round_minutes(time5, :direction => :down, :increment => inc).strftime('%H:%M'))
75
+ assert_equal('09:56', round_minutes(time6, :direction => :down, :increment => inc).strftime('%H:%M'))
76
+ end
77
+
78
+ # Test humanize_time
79
+ def test_humanize_time_works
80
+ time1 = Time.new(2012, 01, 01, 0, 0, 0)
81
+ time2 = Time.new(2012, 01, 01, 9, 36, 59)
82
+ time3 = Time.new(2012, 01, 01, 11, 11, 11)
83
+ time4 = Time.new(2012, 01, 01, 15, 14, 15)
84
+ assert_equal '12:00 am', humanize_time(time1)
85
+ assert_equal '09:36 am', humanize_time(time2)
86
+ assert_equal '11:11 am', humanize_time(time3)
87
+ assert_equal '03:14 pm', humanize_time(time4)
88
+ end
89
+
90
+ def test_humanize_time_works_with_military_time
91
+ time1 = Time.new(2012, 01, 01, 0, 0, 0)
92
+ time2 = Time.new(2012, 01, 01, 9, 36, 59)
93
+ time3 = Time.new(2012, 01, 01, 11, 11, 11)
94
+ time4 = Time.new(2012, 01, 01, 15, 14, 15)
95
+ assert_equal '00:00', humanize_time(time1, :ampm => false)
96
+ assert_equal '09:36', humanize_time(time2, :ampm => false)
97
+ assert_equal '11:11', humanize_time(time3, :ampm => false)
98
+ assert_equal '15:14', humanize_time(time4, :ampm => false)
99
+ end
100
+
101
+ def test_humanize_time_works_with_seconds_option
102
+ time1 = Time.new(2012, 01, 01, 0, 0, 0)
103
+ time2 = Time.new(2012, 01, 01, 9, 36, 59)
104
+ time3 = Time.new(2012, 01, 01, 11, 11, 11)
105
+ time4 = Time.new(2012, 01, 01, 15, 14, 15)
106
+ assert_equal '12:00:00 am', humanize_time(time1, :with_seconds => true)
107
+ assert_equal '09:36:59 am', humanize_time(time2, :with_seconds => true)
108
+ assert_equal '11:11:11 am', humanize_time(time3, :with_seconds => true)
109
+ assert_equal '03:14:15 pm', humanize_time(time4, :with_seconds => true)
110
+ end
111
+
112
+ def test_humanize_time_fails_for_nondates
113
+ time1 = nil
114
+ time2 = "10/04/1986"
115
+ time3 = 3.14159
116
+ assert_nil humanize_time(time1)
117
+ assert_nil humanize_time(time2)
118
+ assert_nil humanize_time(time3)
119
+ end
120
+ end
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.version = TimeDateHelpers::VERSION
8
8
  s.authors = ["Klingon Code Warrior"]
9
9
  s.email = ["profh@cmu.edu"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/profh/time_date_helpers"
11
11
  s.summary = %q{A series of helpers to deal with time and date issues in Rails.}
12
12
  s.description = %q{The initial version has only a few date and time helpers and was created primarily for teaching purposes (how to create gems), but does have some value. I will try to get back to this gem and add more methods as time allows.}
13
13
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_date_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chronic
16
- requirement: &2153346800 !ruby/object:Gem::Requirement
16
+ requirement: &2154777240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2153346800
24
+ version_requirements: *2154777240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: chronic
27
- requirement: &2153362400 !ruby/object:Gem::Requirement
27
+ requirement: &2154776540 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2153362400
35
+ version_requirements: *2154776540
36
36
  description: The initial version has only a few date and time helpers and was created
37
37
  primarily for teaching purposes (how to create gems), but does have some value.
38
38
  I will try to get back to this gem and add more methods as time allows.
@@ -44,13 +44,16 @@ extra_rdoc_files: []
44
44
  files:
45
45
  - .gitignore
46
46
  - Gemfile
47
+ - README.mkd
47
48
  - Rakefile
48
49
  - lib/time_date_helpers.rb
49
50
  - lib/time_date_helpers/date_helpers.rb
50
51
  - lib/time_date_helpers/time_helpers.rb
51
52
  - lib/time_date_helpers/version.rb
53
+ - tests/date_tests.rb
54
+ - tests/time_tests.rb
52
55
  - time_date_helpers.gemspec
53
- homepage: ''
56
+ homepage: https://github.com/profh/time_date_helpers
54
57
  licenses: []
55
58
  post_install_message:
56
59
  rdoc_options: []