RubySunrise 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -128,77 +128,85 @@ class SolarEventCalculator
128
128
  end
129
129
 
130
130
  def compute_utc_civil_sunrise
131
- compute_utc_solar_event(96, true)
131
+ convert_to_datetime(compute_utc_solar_event(96, true))
132
132
  end
133
133
 
134
134
  def compute_utc_civil_sunset
135
- compute_utc_solar_event(96, false)
135
+ convert_to_datetime(compute_utc_solar_event(96, false))
136
136
  end
137
137
 
138
138
  def compute_utc_official_sunrise
139
- compute_utc_solar_event(90.8333, true)
139
+ convert_to_datetime(compute_utc_solar_event(90.8333, true))
140
140
  end
141
141
 
142
142
  def compute_utc_official_sunset
143
- compute_utc_solar_event(90.8333, false)
143
+ convert_to_datetime(compute_utc_solar_event(90.8333, false))
144
144
  end
145
145
 
146
146
  def compute_utc_nautical_sunrise
147
- compute_utc_solar_event(102, true)
147
+ convert_to_datetime(compute_utc_solar_event(102, true))
148
148
  end
149
149
 
150
150
  def compute_utc_nautical_sunset
151
- compute_utc_solar_event(102, false)
151
+ convert_to_datetime(compute_utc_solar_event(102, false))
152
152
  end
153
153
 
154
154
  def compute_utc_astronomical_sunrise
155
- compute_utc_solar_event(108, true)
155
+ convert_to_datetime(compute_utc_solar_event(108, true))
156
156
  end
157
157
 
158
158
  def compute_utc_astronomical_sunset
159
- compute_utc_solar_event(108, false)
159
+ convert_to_datetime(compute_utc_solar_event(108, false))
160
+ end
161
+
162
+ def convert_to_datetime(time)
163
+ DateTime.parse("#{@date.strftime}T#{time.hour}:#{time.min}:00+0000") unless time == nil
160
164
  end
161
165
 
162
166
  def compute_civil_sunrise(timezone)
163
- put_in_timezone(compute_utc_civil_sunrise, timezone)
167
+ put_in_timezone(compute_utc_solar_event(96, true), timezone)
164
168
  end
165
169
 
166
170
  def compute_civil_sunset(timezone)
167
- put_in_timezone(compute_utc_civil_sunset, timezone)
171
+ put_in_timezone(compute_utc_solar_event(96, false), timezone)
168
172
  end
169
173
 
170
174
  def compute_official_sunrise(timezone)
171
- put_in_timezone(compute_utc_official_sunrise, timezone)
175
+ put_in_timezone(compute_utc_solar_event(90.8333, true), timezone)
172
176
  end
173
177
 
174
178
  def compute_official_sunset(timezone)
175
- put_in_timezone(compute_utc_official_sunset, timezone)
179
+ put_in_timezone(compute_utc_solar_event(90.8333, false), timezone)
176
180
  end
177
181
 
178
182
  def compute_nautical_sunrise(timezone)
179
- put_in_timezone(compute_utc_nautical_sunrise, timezone)
183
+ put_in_timezone(compute_utc_solar_event(102, true), timezone)
180
184
  end
181
185
 
182
186
  def compute_nautical_sunset(timezone)
183
- put_in_timezone(compute_utc_nautical_sunset, timezone)
187
+ put_in_timezone(compute_utc_solar_event(102, false), timezone)
184
188
  end
185
189
 
186
190
  def compute_astronomical_sunrise(timezone)
187
- put_in_timezone(compute_utc_astronomical_sunrise, timezone)
191
+ put_in_timezone(compute_utc_solar_event(108, true), timezone)
188
192
  end
189
193
 
190
194
  def compute_astronomical_sunset(timezone)
191
- put_in_timezone(compute_utc_astronomical_sunset, timezone)
195
+ put_in_timezone(compute_utc_solar_event(108, false), timezone)
192
196
  end
193
197
 
194
198
  def put_in_timezone(utcTime, timezone)
195
199
  tz = TZInfo::Timezone.get(timezone)
196
- noonUTC = Time.gm(@date.year, @date.mon, @date.mday, 12, 0)
197
- noonLocal = tz.utc_to_local(noonUTC)
198
- offset = noonLocal - noonUTC
199
-
200
+ # puts "UTCTime #{utcTime}"
200
201
  local = utcTime + get_utc_offset(timezone)
201
- Time.parse("#{@date.strftime('%a %b %d')} #{local.strftime(' %H:%M:%S')} #{offset} #{@date.year}")
202
+ # puts "LocalTime #{local}"
203
+
204
+ offset = (get_utc_offset(timezone) / 60 / 60).to_i
205
+ offset = (offset > 0) ? "+" + offset.to_s : offset.to_s
206
+
207
+ timeInZone = DateTime.parse("#{@date.strftime}T#{local.strftime('%H:%M:%S')}#{offset}")
208
+ # puts "CALC:timeInZone #{timeInZone}"
209
+ timeInZone
202
210
  end
203
211
 
204
212
  def get_utc_offset(timezone)
data/rubysunrise.gemspec CHANGED
@@ -1,12 +1,13 @@
1
1
  spec = Gem::Specification.new do | s |
2
2
  s.name = "RubySunrise"
3
- s.version = "0.2"
3
+ s.version = "0.3"
4
4
  s.author = "Mike Reedell / LuckyCatLabs"
5
- s.email = "mike@luckycatlabs.com"
6
- s.homepage = "http://www.luckycatlabs.com"
5
+ s.email = "mike@reedell.com"
6
+ s.homepage = "http://www.mikereedell.com"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.summary = "Calculate the sunrise/sunset given lat/long coordinates and the date. Computes civil, official, nautical, and astronomical sunrise/sunset."
9
9
  s.files = ["rubysunrise.gemspec"] + Dir.glob("lib/**/*")
10
10
  s.test_files = Dir.glob("{test}/**/*test.rb")
11
11
  s.has_rdoc = false
12
+ s.add_runtime_dependency "tzinfo"
12
13
  end
data/test/bulkdatatest.rb CHANGED
@@ -3,8 +3,26 @@ require '../lib/solareventcalculator'
3
3
  describe SolarEventCalculator, "Test the sunset algorithm" do
4
4
 
5
5
  it "returns correct sunrise/sunset data over a year" do
6
- dataFile = File.open("39_9937N-75_7850W#America-New_York.txt", 'r')
7
- tz = TZInfo::Timezone.get("America/New_York")
6
+ Dir.glob("*.txt") do | dataFileName |
7
+ puts dataFileName
8
+
9
+ nameParts = dataFileName.split('#')
10
+ timeZone = nameParts[1].split('.')[0].sub!('-', '/')
11
+ latString = nameParts[0].split('-')[0].sub!('_', '.')
12
+ longString = nameParts[0].split('-')[1].sub!('_', '.')
13
+
14
+ latitude = BigDecimal.new(latString.chop)
15
+ longitude = BigDecimal.new(longString.chop)
16
+ if latString.end_with?('S')
17
+ latitude = BigDecimal.new("0") - latitude
18
+ end
19
+
20
+ if longString.end_with?('W')
21
+ longitude = BigDecimal.new("0") - longitude
22
+ end
23
+
24
+ tz = TZInfo::Timezone.get(timeZone)
25
+ dataFile = File.open(dataFileName, 'r')
8
26
 
9
27
  dataFile.readlines.each do |dataLine|
10
28
  parts = dataLine.split(',')
@@ -12,47 +30,69 @@ describe SolarEventCalculator, "Test the sunset algorithm" do
12
30
  calc = SolarEventCalculator.new(date, BigDecimal.new("39.9937"), BigDecimal.new("-75.7850"))
13
31
 
14
32
  time = parts[0].split(':')
15
- expectedAstronomicalRise = Time.local(date.year, date.mon, date.mday, time[0], time[1])
16
- calc.compute_astronomical_sunrise("America/New_York").should be_close_to(expectedAstronomicalRise)
33
+ expectedAstronomicalRise = put_in_timezone(date, time[0], time[1], timeZone)
34
+ calc.compute_astronomical_sunrise(timeZone).should be_close_to(expectedAstronomicalRise, "Astronomical Rise")
17
35
 
18
36
  time = parts[1].split(':')
19
- expectedNauticalRise = Time.local(date.year, date.mon, date.mday, time[0], time[1])
20
- calc.compute_nautical_sunrise("America/New_York").should be_close_to(expectedNauticalRise)
37
+ expectedNauticalRise = put_in_timezone(date, time[0], time[1], timeZone)
38
+ calc.compute_nautical_sunrise(timeZone).should be_close_to(expectedNauticalRise, "Nautical Rise")
21
39
 
22
40
  time = parts[2].split(':')
23
- expectedCivilRise = Time.local(date.year, date.mon, date.mday, time[0], time[1])
24
- calc.compute_civil_sunrise("America/New_York").should be_close_to(expectedCivilRise.getlocal)
41
+ expectedCivilRise = put_in_timezone(date, time[0], time[1], timeZone)
42
+ calc.compute_civil_sunrise(timeZone).should be_close_to(expectedCivilRise, "Civil Rise")
25
43
 
26
44
  time = parts[3].split(':')
27
- expectedOfficialRise = Time.local(date.year, date.mon, date.mday, time[0], time[1])
28
- calc.compute_official_sunrise("America/New_York").should be_close_to(expectedOfficialRise.getlocal)
45
+ expectedOfficialRise = put_in_timezone(date, time[0], time[1], timeZone)
46
+ calc.compute_official_sunrise(timeZone).should be_close_to(expectedOfficialRise, "Official Rise")
29
47
 
30
48
  time = parts[4].split(':')
31
- expectedOfficialSet = Time.local(date.year, date.mon, date.mday, time[0], time[1])
32
- calc.compute_official_sunset("America/New_York").should be_close_to(expectedOfficialSet)
49
+ expectedOfficialSet = put_in_timezone(date, time[0], time[1], timeZone)
50
+ calc.compute_official_sunset(timeZone).should be_close_to(expectedOfficialSet, "Official Set")
33
51
 
34
52
  time = parts[5].split(':')
35
- expectedCivilSet = Time.local(date.year, date.mon, date.mday, time[0], time[1])
36
- calc.compute_civil_sunset("America/New_York").should be_close_to(expectedCivilSet)
53
+ expectedCivilSet = put_in_timezone(date, time[0], time[1], timeZone)
54
+ calc.compute_civil_sunset(timeZone).should be_close_to(expectedCivilSet, "Civil Set")
37
55
 
38
56
  time = parts[6].split(':')
39
- expectedNauticalSet = Time.local(date.year, date.mon, date.mday, time[0], time[1])
40
- calc.compute_nautical_sunset("America/New_York").should be_close_to(expectedNauticalSet)
57
+ expectedNauticalSet = put_in_timezone(date, time[0], time[1], timeZone)
58
+ calc.compute_nautical_sunset(timeZone).should be_close_to(expectedNauticalSet, "Nautical Set")
41
59
 
42
60
  time = parts[7].split(':')
43
- expectedAstronomicalSet = Time.local(date.year, date.mon, date.mday, time[0], time[1])
44
- calc.compute_astronomical_sunset("America/New_York").should be_close_to(expectedAstronomicalSet)
61
+ expectedAstronomicalSet = put_in_timezone(date, time[0], time[1], timeZone)
62
+ calc.compute_astronomical_sunset(timeZone).should be_close_to(expectedAstronomicalSet, "Astronomical Set")
63
+ end
45
64
  end
46
65
  end
47
66
  end
48
67
 
49
- Spec::Matchers.define :be_close_to do |expected|
68
+ def put_in_timezone(date, hours, minutes, timezone)
69
+ if hours.to_i == 99
70
+ return nil
71
+ end
72
+ offset = get_utc_offset(date, timezone)
73
+
74
+ timeInZone = DateTime.parse("#{date.strftime}T#{hours}:#{minutes}:00#{offset}")
75
+ timeInZone
76
+ end
77
+
78
+ def get_utc_offset(date, timezone)
79
+ tz = TZInfo::Timezone.get(timezone)
80
+ noonUTC = Time.utc(date.year,date.mon, date.mday, 12, 0)
81
+ offset = ((tz.utc_to_local(noonUTC) - noonUTC) / 60 / 60).to_i
82
+ offset = (offset > 0) ? "+" + offset.to_s : offset.to_s
83
+ end
84
+
85
+ Spec::Matchers.define :be_close_to do |expected, type|
50
86
  match do |actual|
51
- (expected - 61) < actual && actual < (expected + 61)
87
+ if expected != nil && actual != nil
88
+ (expected - 61) < actual && actual < (expected + 61)
89
+ else
90
+ expected == nil && actual == nil
91
+ end
52
92
  end
53
93
 
54
94
  failure_message_for_should do |actual|
55
- " actual #{actual} is outside +-1 minute of expected #{expected}"
95
+ " actual #{type} #{actual} is outside +-1 minute of expected #{type} #{expected}"
56
96
  end
57
97
 
58
98
  failure_message_for_should_not do |actual|
data/test/sunrisetest.rb CHANGED
@@ -55,36 +55,36 @@ describe SolarEventCalculator, "test the math for home" do
55
55
  @calc.compute_local_mean_time(trueLong, longHour, t, localHour).should eql(BigDecimal.new("11.0818"))
56
56
  end
57
57
 
58
- it "returns correct civil sunrise time" do
59
- @calc.compute_utc_civil_sunrise.should eql(Time.gm(@date.year, @date.mon, @date.mday, 11, 4))
58
+ it "returns correct UTC civil sunrise time" do
59
+ @calc.compute_utc_civil_sunrise.should eql(DateTime.parse("#{@date.strftime}T11:04:00-00:00"))
60
60
  end
61
61
 
62
- it "returns correct official sunrise time" do
63
- @calc.compute_utc_official_sunrise.should eql(Time.gm(@date.year, @date.mon, @date.mday, 11, 33))
62
+ it "returns correct UTC official sunrise time" do
63
+ @calc.compute_utc_official_sunrise.should eql(DateTime.parse("#{@date.strftime}T11:33:00-00:00"))
64
64
  end
65
65
 
66
- it "returns correct nautical sunrise time" do
67
- @calc.compute_utc_nautical_sunrise.should eql(Time.gm(@date.year, @date.mon, @date.mday, 10, 32))
66
+ it "returns correct UTC nautical sunrise time" do
67
+ @calc.compute_utc_nautical_sunrise.should eql(DateTime.parse("#{@date.strftime}T10:32:00-00:00"))
68
68
  end
69
69
 
70
- it "returns correct astronomical sunrise time" do
71
- @calc.compute_utc_astronomical_sunrise.should eql(Time.gm(@date.year, @date.mon, @date.mday, 10, 1))
70
+ it "returns correct UTC astronomical sunrise time" do
71
+ @calc.compute_utc_astronomical_sunrise.should eql(DateTime.parse("#{@date.strftime}T10:01:00-00:00"))
72
72
  end
73
73
 
74
74
  it "returns correct 'America/New_York' official sunrise time" do
75
- @calc.compute_official_sunrise('America/New_York').should eql(Time.local(@date.year, @date.mon, @date.mday, 7, 33))
75
+ @calc.compute_official_sunrise('America/New_York').should eql(DateTime.parse("#{@date.strftime}T07:33:00-04:00"))
76
76
  end
77
77
 
78
78
  it "returns correct 'America/New_York' civil sunrise time" do
79
- @calc.compute_civil_sunrise('America/New_York').should eql(Time.local(@date.year, @date.mon, @date.mday, 7, 4))
79
+ @calc.compute_civil_sunrise('America/New_York').should eql(DateTime.parse("#{@date.strftime}T07:04:00-04:00"))
80
80
  end
81
81
 
82
82
  it "returns correct 'America/New_York' nautical sunrise time" do
83
- @calc.compute_nautical_sunrise('America/New_York').should eql(Time.local(@date.year, @date.mon, @date.mday, 6, 32))
83
+ @calc.compute_nautical_sunrise('America/New_York').should eql(DateTime.parse("#{@date.strftime}T06:32:00-04:00"))
84
84
  end
85
85
 
86
86
  it "returns correct 'America/New_York' astronomical sunrise time" do
87
- @calc.compute_astronomical_sunrise('America/New_York').should eql(Time.local(@date.year, @date.mon, @date.mday, 6, 1))
87
+ @calc.compute_astronomical_sunrise('America/New_York').should eql(DateTime.parse("#{@date.strftime}T06:01:00-04:00"))
88
88
  end
89
89
  end
90
90
 
data/test/sunsettest.rb CHANGED
@@ -56,37 +56,37 @@ describe SolarEventCalculator, "Test the sunset algorithm" do
56
56
  end
57
57
 
58
58
  it "returns correct UTC civil sunset time" do
59
- @calc.compute_utc_civil_sunset.should eql(Time.gm(@date.year, @date.mon, @date.mday, 22, 28))
59
+ @calc.compute_utc_civil_sunset.should eql(DateTime.parse("#{@date.strftime}T22:28:00-00:00"))
60
60
  end
61
61
 
62
62
  it "returns correct UTC official sunset time" do
63
- @calc.compute_utc_official_sunset.should eql(Time.gm(@date.year, @date.mon, @date.mday, 21, 59))
63
+ @calc.compute_utc_official_sunset.should eql(DateTime.parse("#{@date.strftime}T21:59:00-00:00"))
64
64
  end
65
65
 
66
66
  it "returns correct UTC nautical sunset time" do
67
- @calc.compute_utc_nautical_sunset.should eql(Time.gm(@date.year, @date.mon, @date.mday, 23, 0))
67
+ @calc.compute_utc_nautical_sunset.should eql(DateTime.parse("#{@date.strftime}T23:00:00-00:00"))
68
68
  end
69
69
 
70
70
  it "returns correct UTC astronomical sunset time" do
71
- @calc.compute_utc_astronomical_sunset.should eql(Time.gm(@date.year, @date.mon, @date.mday, 23, 31))
71
+ @calc.compute_utc_astronomical_sunset.should eql(DateTime.parse("#{@date.strftime}T23:31:00-00:00"))
72
72
  end
73
73
 
74
74
  it "returns correct 'America/New_York' offical sunset time" do
75
- @calc.compute_official_sunset("America/New_York").should eql(Time.local(@date.year, @date.mon, @date.mday, 17, 59))
75
+ @calc.compute_official_sunset("America/New_York").should eql(DateTime.parse("#{@date.strftime}T17:59:00-04:00"))
76
76
  end
77
77
 
78
78
  it "returns correct 'America/New_York' civil sunset time" do
79
- @calc.compute_civil_sunset("America/New_York").should eql(Time.local(@date.year, @date.mon, @date.mday, 18, 28))
79
+ @calc.compute_civil_sunset("America/New_York").should eql(DateTime.parse("#{@date.strftime}T18:28:00-04:00"))
80
80
  end
81
81
 
82
82
  it "returns correct 'America/New_York' nautical sunset time" do
83
- @calc.compute_nautical_sunset("America/New_York").should eql(Time.local(@date.year, @date.mon, @date.mday, 19, 00))
83
+ @calc.compute_nautical_sunset("America/New_York").should eql(DateTime.parse("#{@date.strftime}T19:00:00-04:00"))
84
84
  end
85
85
 
86
86
  it "returns correct 'America/New_York' astronomical sunset time" do
87
- @calc.compute_astronomical_sunset("America/New_York").should eql(Time.local(@date.year, @date.mon, @date.mday, 19, 31))
87
+ @calc.compute_astronomical_sunset("America/New_York").should eql(DateTime.parse("#{@date.strftime}T19:31:00-04:00"))
88
88
  end
89
- # 18:00,18:28,19:00,19:32
89
+ # DateTime.parse("#{@date.strftime}T06:32:00-04:00")
90
90
  end
91
91
 
92
92
  describe SolarEventCalculator, "test the math for areas where the sun doesn't set" do
data/test/test.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'date'
2
+ require '../lib/solareventcalculator'
3
+
4
+ date = Date.parse('2008-11-01')
5
+ calc = SolarEventCalculator.new(date, BigDecimal.new("39.9537"), BigDecimal.new("-75.7850"))
6
+
7
+ utcOfficialSunrise = calc.compute_utc_official_sunrise
8
+ localOfficialSunrise = calc.compute_official_sunrise('America/New_York')
9
+
10
+ puts "utcOfficialSunrise #{utcOfficialSunrise}"
11
+ puts "localOfficialSunrise #{localOfficialSunrise}"
metadata CHANGED
@@ -1,64 +1,66 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: RubySunrise
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 2
8
- version: "0.2"
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.3'
5
+ prerelease:
9
6
  platform: ruby
10
- authors:
7
+ authors:
11
8
  - Mike Reedell / LuckyCatLabs
12
9
  autorequire:
13
10
  bindir: bin
14
11
  cert_chain: []
15
-
16
- date: 2010-03-13 00:00:00 -05:00
17
- default_executable:
18
- dependencies: []
19
-
12
+ date: 2012-03-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: tzinfo
16
+ requirement: &70286578500580 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70286578500580
20
25
  description:
21
- email: mike@luckycatlabs.com
26
+ email: mike@reedell.com
22
27
  executables: []
23
-
24
28
  extensions: []
25
-
26
29
  extra_rdoc_files: []
27
-
28
- files:
30
+ files:
29
31
  - rubysunrise.gemspec
30
32
  - lib/solareventcalculator.rb
31
- has_rdoc: true
32
- homepage: http://www.luckycatlabs.com
33
+ - test/bulkdatatest.rb
34
+ - test/sunrisetest.rb
35
+ - test/sunsettest.rb
36
+ - test/test.rb
37
+ homepage: http://www.mikereedell.com
33
38
  licenses: []
34
-
35
39
  post_install_message:
36
40
  rdoc_options: []
37
-
38
- require_paths:
41
+ require_paths:
39
42
  - lib
40
- required_ruby_version: !ruby/object:Gem::Requirement
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- segments:
45
- - 0
46
- version: "0"
47
- required_rubygems_version: !ruby/object:Gem::Requirement
48
- requirements:
49
- - - ">="
50
- - !ruby/object:Gem::Version
51
- segments:
52
- - 0
53
- version: "0"
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
54
55
  requirements: []
55
-
56
56
  rubyforge_project:
57
- rubygems_version: 1.3.6
57
+ rubygems_version: 1.8.12
58
58
  signing_key:
59
59
  specification_version: 3
60
- summary: Calculate the sunrise/sunset given lat/long coordinates and the date. Computes civil, official, nautical, and astronomical sunrise/sunset.
61
- test_files:
60
+ summary: Calculate the sunrise/sunset given lat/long coordinates and the date. Computes
61
+ civil, official, nautical, and astronomical sunrise/sunset.
62
+ test_files:
62
63
  - test/bulkdatatest.rb
63
64
  - test/sunrisetest.rb
64
65
  - test/sunsettest.rb
66
+ - test/test.rb