tod 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -83,7 +83,12 @@ then require 'tod/time_of_day' instead of 'tod'.
83
83
  Date.today.at tod # => 2010-12-29 08:30:00 -0600
84
84
  Time.now.to_time_of_day # => 16:30:43
85
85
 
86
+ Rails Time Zone Support
87
+ =======================
88
+
89
+ If Rails time zone support is loaded, Date#on and TimeOfDay#at will automatically use Time.zone.
90
+
86
91
  License
87
92
  =======
88
93
 
89
- Copyright (c) 2010 Jack Christensen, released under the MIT license
94
+ Copyright (c) 2010-2103 Jack Christensen, released under the MIT license
data/lib/tod/date.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  class Date
2
2
  # Returns a local Time instance with this date and time_of_day
3
- def at(time_of_day)
4
- Time.local year, month, day, time_of_day.hour, time_of_day.minute, time_of_day.second
3
+ # Pass in time_zone to use ActiveSupport::TimeZone
4
+ def at(time_of_day, time_zone=Tod::TimeOfDay.time_zone)
5
+ time_zone.local year, month, day, time_of_day.hour, time_of_day.minute, time_of_day.second
5
6
  end
6
7
  end
@@ -46,7 +46,7 @@ module Tod
46
46
  raise ArgumentError, "second must be between 0 and 59" unless (0..59).include?(@second)
47
47
 
48
48
  @second_of_day = @hour * 60 * 60 + @minute * 60 + @second
49
-
49
+
50
50
  freeze # TimeOfDay instances are value objects
51
51
  end
52
52
 
@@ -76,8 +76,9 @@ module Tod
76
76
  end
77
77
 
78
78
  # Returns a Time instance on date using self as the time of day
79
- def on(date)
80
- Time.local date.year, date.month, date.day, @hour, @minute, @second
79
+ # Optional time_zone will build time in that zone
80
+ def on(date, time_zone=Tod::TimeOfDay.time_zone)
81
+ time_zone.local date.year, date.month, date.day, @hour, @minute, @second
81
82
  end
82
83
 
83
84
  # Build a new TimeOfDay instance from second_of_day
@@ -123,6 +124,11 @@ module Tod
123
124
  raise ArgumentError, "Invalid time of day string"
124
125
  end
125
126
  end
127
+
128
+ # If ActiveSupport TimeZone is available and set use current time zone else return Time
129
+ def self.time_zone
130
+ (Time.respond_to?(:zone) && Time.zone) || Time
131
+ end
126
132
  end
127
133
  end
128
134
 
data/lib/tod/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class TimeOfDay
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1,12 @@
1
+ # This test depends on not having ActiveSupport loaded yet. So it is named
2
+ # with AAA prefix to ensure it runs before anything that loads it.
3
+
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','test_helper'))
5
+
6
+ class AAATimeOfDayWithoutActiveSupportTest < Test::Unit::TestCase
7
+ context "self.time_zone" do
8
+ should "be Time" do
9
+ assert_equal Time, TimeOfDay.time_zone
10
+ end
11
+ end
12
+ end
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__),'..','test_helper'))
2
+ require 'active_support/time'
2
3
 
3
4
  class DateTest < Test::Unit::TestCase
4
5
  context "at" do
@@ -7,5 +8,14 @@ class DateTest < Test::Unit::TestCase
7
8
  tod = TimeOfDay.new 8,30
8
9
  assert_equal Time.local(2000,1,1, 8,30), date.at(tod)
9
10
  end
11
+
12
+ context "with a time zone" do
13
+ should "accept TimeOfDay and TimeWithZone on same date" do
14
+ date = Date.civil 2000,1,1
15
+ tod = TimeOfDay.new 8,30
16
+ time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
17
+ assert_equal time_zone.local(2000,1,1, 8,30), date.at(tod, time_zone)
18
+ end
19
+ end
10
20
  end
11
21
  end
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__),'..','test_helper'))
2
+ require 'active_support/time'
2
3
 
3
4
  class TimeOfDayTest < Test::Unit::TestCase
4
5
  context "initialize" do
@@ -179,5 +180,14 @@ class TimeOfDayTest < Test::Unit::TestCase
179
180
  should "be local Time on given date" do
180
181
  assert_equal Time.local(2010,12,29, 8,30), TimeOfDay.new(8,30).on(Date.civil(2010,12,29))
181
182
  end
183
+
184
+ context "with a time zone" do
185
+ should "be TimeWithZone on given date" do
186
+ date = Date.civil 2000,1,1
187
+ tod = TimeOfDay.new 8,30
188
+ time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
189
+ assert_equal time_zone.local(2000,1,1, 8,30), tod.on(date, time_zone)
190
+ end
191
+ end
182
192
  end
183
193
  end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','test_helper'))
2
+ require 'active_support/time'
3
+
4
+ class TimeOfDayWithActiveSupportTest < Test::Unit::TestCase
5
+ context "self.time_zone" do
6
+ context "when Time.zone is nil" do
7
+ should "be Time" do
8
+ assert_equal Time, TimeOfDay.time_zone
9
+ end
10
+ end
11
+
12
+ context "when Time.zone is set" do
13
+ should "be Time.zone" do
14
+ Time.zone = "Central Time (US & Canada)"
15
+ assert_equal Time.zone, TimeOfDay.time_zone
16
+ end
17
+ end
18
+ end
19
+ end
data/tod.gemspec CHANGED
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.add_development_dependency "test-unit"
16
16
  s.add_development_dependency "shoulda"
17
17
  s.add_development_dependency "mocha"
18
+ s.add_development_dependency "active_support", ">= 3.0"
18
19
 
19
20
  s.files = `git ls-files`.split("\n")
20
21
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
metadata CHANGED
@@ -1,71 +1,88 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: tod
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 0
9
- version: 1.0.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Jack Christensen
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2010-12-29 00:00:00 -06:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: test-unit
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
31
22
  type: :development
32
- version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
34
- name: shoulda
35
23
  prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: shoulda
32
+ requirement: !ruby/object:Gem::Requirement
37
33
  none: false
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
- version: "0"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
44
38
  type: :development
45
- version_requirements: *id002
46
- - !ruby/object:Gem::Dependency
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
47
  name: mocha
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
48
55
  prerelease: false
49
- requirement: &id003 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: active_support
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- segments:
55
- - 0
56
- version: "0"
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
57
70
  type: :development
58
- version_requirements: *id003
59
- description: Supplies TimeOfDay class that includes parsing, strftime, comparison, and arithmetic.
60
- email:
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '3.0'
78
+ description: Supplies TimeOfDay class that includes parsing, strftime, comparison,
79
+ and arithmetic.
80
+ email:
61
81
  - jack@jackchristensen.com
62
82
  executables: []
63
-
64
83
  extensions: []
65
-
66
84
  extra_rdoc_files: []
67
-
68
- files:
85
+ files:
69
86
  - .gitignore
70
87
  - MIT-LICENSE
71
88
  - README.markdown
@@ -76,41 +93,40 @@ files:
76
93
  - lib/tod/time_of_day.rb
77
94
  - lib/tod/version.rb
78
95
  - test/test_helper.rb
96
+ - test/tod/a_a_a_time_of_day_time_zone_without_active_support_test.rb
79
97
  - test/tod/date_test.rb
80
98
  - test/tod/time_of_day_test.rb
99
+ - test/tod/time_of_day_time_zone_with_active_support_test.rb
81
100
  - test/tod/time_test.rb
82
101
  - tod.gemspec
83
- has_rdoc: true
84
102
  homepage: https://github.com/JackC/tod
85
103
  licenses: []
86
-
87
104
  post_install_message:
88
105
  rdoc_options: []
89
-
90
- require_paths:
106
+ require_paths:
91
107
  - lib
92
- required_ruby_version: !ruby/object:Gem::Requirement
108
+ required_ruby_version: !ruby/object:Gem::Requirement
93
109
  none: false
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- segments:
98
- - 0
99
- version: "0"
100
- required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
115
  none: false
102
- requirements:
103
- - - ">="
104
- - !ruby/object:Gem::Version
105
- segments:
106
- - 0
107
- version: "0"
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
108
120
  requirements: []
109
-
110
121
  rubyforge_project:
111
- rubygems_version: 1.3.7
122
+ rubygems_version: 1.8.23
112
123
  signing_key:
113
124
  specification_version: 3
114
125
  summary: Supplies TimeOfDay class
115
- test_files: []
116
-
126
+ test_files:
127
+ - test/test_helper.rb
128
+ - test/tod/a_a_a_time_of_day_time_zone_without_active_support_test.rb
129
+ - test/tod/date_test.rb
130
+ - test/tod/time_of_day_test.rb
131
+ - test/tod/time_of_day_time_zone_with_active_support_test.rb
132
+ - test/tod/time_test.rb