RubySunrise 0.1.1 → 0.2

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.
@@ -1,5 +1,6 @@
1
1
  require 'bigdecimal'
2
2
  require 'date'
3
+ require 'tzinfo'
3
4
 
4
5
  class SolarEventCalculator
5
6
 
@@ -123,7 +124,7 @@ class SolarEventCalculator
123
124
  mins = pad_minutes(mins.to_i)
124
125
  hours = timeParts[0]
125
126
 
126
- Time.gm(@date.year, @date.mon, @date.mday, hours, pad_minutes(mins.to_i))
127
+ Time.utc(@date.year, @date.mon, @date.mday, hours, pad_minutes(mins.to_i))
127
128
  end
128
129
 
129
130
  def compute_utc_civil_sunrise
@@ -158,6 +159,54 @@ class SolarEventCalculator
158
159
  compute_utc_solar_event(108, false)
159
160
  end
160
161
 
162
+ def compute_civil_sunrise(timezone)
163
+ put_in_timezone(compute_utc_civil_sunrise, timezone)
164
+ end
165
+
166
+ def compute_civil_sunset(timezone)
167
+ put_in_timezone(compute_utc_civil_sunset, timezone)
168
+ end
169
+
170
+ def compute_official_sunrise(timezone)
171
+ put_in_timezone(compute_utc_official_sunrise, timezone)
172
+ end
173
+
174
+ def compute_official_sunset(timezone)
175
+ put_in_timezone(compute_utc_official_sunset, timezone)
176
+ end
177
+
178
+ def compute_nautical_sunrise(timezone)
179
+ put_in_timezone(compute_utc_nautical_sunrise, timezone)
180
+ end
181
+
182
+ def compute_nautical_sunset(timezone)
183
+ put_in_timezone(compute_utc_nautical_sunset, timezone)
184
+ end
185
+
186
+ def compute_astronomical_sunrise(timezone)
187
+ put_in_timezone(compute_utc_astronomical_sunrise, timezone)
188
+ end
189
+
190
+ def compute_astronomical_sunset(timezone)
191
+ put_in_timezone(compute_utc_astronomical_sunset, timezone)
192
+ end
193
+
194
+ def put_in_timezone(utcTime, timezone)
195
+ 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
+ local = utcTime + get_utc_offset(timezone)
201
+ Time.parse("#{@date.strftime('%a %b %d')} #{local.strftime(' %H:%M:%S')} #{offset} #{@date.year}")
202
+ end
203
+
204
+ def get_utc_offset(timezone)
205
+ tz = TZInfo::Timezone.get(timezone)
206
+ noonUTC = Time.gm(@date.year, @date.mon, @date.mday, 12, 0)
207
+ tz.utc_to_local(noonUTC) - noonUTC
208
+ end
209
+
161
210
  def pad_minutes(minutes)
162
211
  if(minutes < 10)
163
212
  "0" + minutes.to_s
@@ -1,6 +1,6 @@
1
1
  spec = Gem::Specification.new do | s |
2
2
  s.name = "RubySunrise"
3
- s.version = "0.1.1"
3
+ s.version = "0.2"
4
4
  s.author = "Mike Reedell / LuckyCatLabs"
5
5
  s.email = "mike@luckycatlabs.com"
6
6
  s.homepage = "http://www.luckycatlabs.com"
@@ -0,0 +1,65 @@
1
+ require '../lib/solareventcalculator'
2
+
3
+ describe SolarEventCalculator, "Test the sunset algorithm" do
4
+
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")
8
+
9
+ dataFile.readlines.each do |dataLine|
10
+ parts = dataLine.split(',')
11
+ date = Date.parse(parts.shift)
12
+ calc = SolarEventCalculator.new(date, BigDecimal.new("39.9937"), BigDecimal.new("-75.7850"))
13
+
14
+ 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)
17
+
18
+ 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)
21
+
22
+ 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)
25
+
26
+ 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)
29
+
30
+ 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)
33
+
34
+ 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)
37
+
38
+ 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)
41
+
42
+ 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)
45
+ end
46
+ end
47
+ end
48
+
49
+ Spec::Matchers.define :be_close_to do |expected|
50
+ match do |actual|
51
+ (expected - 61) < actual && actual < (expected + 61)
52
+ end
53
+
54
+ failure_message_for_should do |actual|
55
+ " actual #{actual} is outside +-1 minute of expected #{expected}"
56
+ end
57
+
58
+ failure_message_for_should_not do |actual|
59
+
60
+ end
61
+
62
+ description do
63
+ " tests whether the actual time is within a minute of the expected time"
64
+ end
65
+ end
@@ -70,6 +70,22 @@ describe SolarEventCalculator, "test the math for home" do
70
70
  it "returns correct astronomical sunrise time" do
71
71
  @calc.compute_utc_astronomical_sunrise.should eql(Time.gm(@date.year, @date.mon, @date.mday, 10, 1))
72
72
  end
73
+
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))
76
+ end
77
+
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))
80
+ end
81
+
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))
84
+ end
85
+
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))
88
+ end
73
89
  end
74
90
 
75
91
  describe SolarEventCalculator, "test the math for areas where there could be no rise/set" do
@@ -3,7 +3,7 @@ require '../lib/solareventcalculator'
3
3
  describe SolarEventCalculator, "Test the sunset algorithm" do
4
4
 
5
5
  before do
6
- @date = Date.parse('2008-11-01') #01 November 2008
6
+ @date = Date.parse('2008-11-01') #01 November 2008 (DST)
7
7
  @calc = SolarEventCalculator.new(@date, BigDecimal.new("39.9537"), BigDecimal.new("-75.7850"))
8
8
  end
9
9
 
@@ -55,21 +55,38 @@ describe SolarEventCalculator, "Test the sunset algorithm" do
55
55
  @calc.compute_local_mean_time(trueLong, longHour, t, localHour).should eql(BigDecimal.new("22.4675"))
56
56
  end
57
57
 
58
- it "returns correct civil sunset time" do
58
+ it "returns correct UTC civil sunset time" do
59
59
  @calc.compute_utc_civil_sunset.should eql(Time.gm(@date.year, @date.mon, @date.mday, 22, 28))
60
60
  end
61
61
 
62
- it "returns correct official sunset time" do
62
+ it "returns correct UTC official sunset time" do
63
63
  @calc.compute_utc_official_sunset.should eql(Time.gm(@date.year, @date.mon, @date.mday, 21, 59))
64
64
  end
65
65
 
66
- it "returns correct nautical sunset time" do
66
+ it "returns correct UTC nautical sunset time" do
67
67
  @calc.compute_utc_nautical_sunset.should eql(Time.gm(@date.year, @date.mon, @date.mday, 23, 0))
68
68
  end
69
69
 
70
- it "returns correct astronomical sunset time" do
70
+ it "returns correct UTC astronomical sunset time" do
71
71
  @calc.compute_utc_astronomical_sunset.should eql(Time.gm(@date.year, @date.mon, @date.mday, 23, 31))
72
72
  end
73
+
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))
76
+ end
77
+
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))
80
+ end
81
+
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))
84
+ end
85
+
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))
88
+ end
89
+ # 18:00,18:28,19:00,19:32
73
90
  end
74
91
 
75
92
  describe SolarEventCalculator, "test the math for areas where the sun doesn't set" do
metadata CHANGED
@@ -4,9 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ version: "0.2"
10
9
  platform: ruby
11
10
  authors:
12
11
  - Mike Reedell / LuckyCatLabs
@@ -14,7 +13,7 @@ autorequire:
14
13
  bindir: bin
15
14
  cert_chain: []
16
15
 
17
- date: 2010-03-08 00:00:00 -05:00
16
+ date: 2010-03-13 00:00:00 -05:00
18
17
  default_executable:
19
18
  dependencies: []
20
19
 
@@ -60,5 +59,6 @@ signing_key:
60
59
  specification_version: 3
61
60
  summary: Calculate the sunrise/sunset given lat/long coordinates and the date. Computes civil, official, nautical, and astronomical sunrise/sunset.
62
61
  test_files:
62
+ - test/bulkdatatest.rb
63
63
  - test/sunrisetest.rb
64
64
  - test/sunsettest.rb