ruby-sun-times 0.1.1 → 0.1.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.
- data/lib/sun_times.rb +31 -10
- data/test/calculate_test.rb +27 -6
- metadata +2 -2
data/lib/sun_times.rb
CHANGED
@@ -30,13 +30,14 @@ module SunTimes
|
|
30
30
|
module VERSION #:nodoc:
|
31
31
|
MAJOR = 0
|
32
32
|
MINOR = 1
|
33
|
-
TINY
|
34
|
-
|
33
|
+
TINY = 2
|
34
|
+
|
35
35
|
STRING = [MAJOR, MINOR, TINY].join('.')
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
DEFAULT_ZENITH = 90.83333
|
39
39
|
KNOWN_EVENTS = [:rise, :set]
|
40
|
+
DEGREES_PER_HOUR = 360.0 / 24.0
|
40
41
|
|
41
42
|
# Helper method: calculates sunrise, with the same parameters as calculate
|
42
43
|
def SunTimes.rise(date, latitude, longitude, options = {})
|
@@ -65,7 +66,7 @@ module SunTimes
|
|
65
66
|
zenith = options.delete(:zenith) || DEFAULT_ZENITH
|
66
67
|
|
67
68
|
# lngHour
|
68
|
-
longitude_hour = longitude /
|
69
|
+
longitude_hour = longitude / DEGREES_PER_HOUR
|
69
70
|
|
70
71
|
# t
|
71
72
|
base_time = event == :rise ? 6.0 : 18.0
|
@@ -85,21 +86,21 @@ module SunTimes
|
|
85
86
|
tan_right_ascension = 0.91764 * Math.tan(degrees_to_radians(sun_true_longitude))
|
86
87
|
sun_right_ascension = radians_to_degrees(Math.atan(tan_right_ascension))
|
87
88
|
sun_right_ascension = coerce_degrees(sun_right_ascension)
|
88
|
-
|
89
|
+
|
89
90
|
# right ascension value needs to be in the same quadrant as L
|
90
91
|
sun_true_longitude_quadrant = (sun_true_longitude / 90.0).floor * 90.0
|
91
92
|
sun_right_ascension_quadrant = (sun_right_ascension / 90.0).floor * 90.0
|
92
93
|
sun_right_ascension += (sun_true_longitude_quadrant - sun_right_ascension_quadrant)
|
93
94
|
|
94
95
|
# RA = RA / 15
|
95
|
-
sun_right_ascension_hours = sun_right_ascension /
|
96
|
+
sun_right_ascension_hours = sun_right_ascension / DEGREES_PER_HOUR
|
96
97
|
|
97
98
|
sin_declination = 0.39782 * Math.sin(degrees_to_radians(sun_true_longitude))
|
98
99
|
cos_declination = Math.cos(Math.asin(sin_declination))
|
99
100
|
|
100
101
|
cos_local_hour_angle =
|
101
102
|
(Math.cos(degrees_to_radians(zenith)) - (sin_declination * Math.sin(degrees_to_radians(latitude)))) /
|
102
|
-
|
103
|
+
(cos_declination * Math.cos(degrees_to_radians(latitude)))
|
103
104
|
|
104
105
|
# the sun never rises on this location (on the specified date)
|
105
106
|
return nil if cos_local_hour_angle > 1
|
@@ -112,10 +113,10 @@ module SunTimes
|
|
112
113
|
360 - radians_to_degrees(Math.acos(cos_local_hour_angle))
|
113
114
|
else
|
114
115
|
radians_to_degrees(Math.acos(cos_local_hour_angle))
|
115
|
-
end
|
116
|
+
end
|
116
117
|
|
117
118
|
# H = H / 15
|
118
|
-
suns_local_hour_hours = suns_local_hour /
|
119
|
+
suns_local_hour_hours = suns_local_hour / DEGREES_PER_HOUR
|
119
120
|
|
120
121
|
# T = H + RA - (0.06571 * t) - 6.622
|
121
122
|
local_mean_time = suns_local_hour_hours + sun_right_ascension_hours - (0.06571 * approximate_time) - 6.622
|
@@ -125,6 +126,26 @@ module SunTimes
|
|
125
126
|
gmt_hours -= 24.0 if gmt_hours > 24
|
126
127
|
gmt_hours += 24.0 if gmt_hours < 0
|
127
128
|
|
129
|
+
case
|
130
|
+
when date.respond_to?( :offset )
|
131
|
+
offset_hours = date.offset * 24
|
132
|
+
when date.respond_to?( :gmt_offset )
|
133
|
+
offset_hours = date.gmt_offset / 3600
|
134
|
+
else
|
135
|
+
offset_hours = nil
|
136
|
+
end
|
137
|
+
|
138
|
+
if ! offset_hours.nil?
|
139
|
+
if gmt_hours + offset_hours < 0
|
140
|
+
next_day = Date.new(date.year, date.month, date.day + 1)
|
141
|
+
return calculate(event, next_day, latitude, longitude, options = {})
|
142
|
+
end
|
143
|
+
if gmt_hours + offset_hours > 24
|
144
|
+
previous_day = Date.new(date.year, date.month, date.day + 1)
|
145
|
+
return calculate(event, previous_day, latitude, longitude, options = {})
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
128
149
|
hour = gmt_hours.floor
|
129
150
|
hour_remainder = (gmt_hours.to_f - hour.to_f) * 60.0
|
130
151
|
minute = hour_remainder.floor
|
@@ -138,7 +159,7 @@ module SunTimes
|
|
138
159
|
def SunTimes.degrees_to_radians(d)
|
139
160
|
d.to_f / 360.0 * 2.0 * Math::PI
|
140
161
|
end
|
141
|
-
|
162
|
+
|
142
163
|
def SunTimes.radians_to_degrees(r)
|
143
164
|
r.to_f * 360.0 / (2.0 * Math::PI)
|
144
165
|
end
|
data/test/calculate_test.rb
CHANGED
@@ -2,27 +2,28 @@
|
|
2
2
|
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
3
3
|
require 'test/unit'
|
4
4
|
require 'sun_times'
|
5
|
+
require 'date'
|
5
6
|
|
6
7
|
class SunTimesTest < Test::Unit::TestCase
|
7
8
|
|
8
9
|
def test_rise_20100308_pontassieve
|
9
10
|
rise = SunTimes.calculate(:rise, Date.new(2010, 3, 8), 43.779, 11.432)
|
10
|
-
assert_equal(
|
11
|
+
assert_equal(Time.gm(2010, 3, 8, 5, 39, 53), rise)
|
11
12
|
end
|
12
13
|
|
13
14
|
def test_set_20100308_pontassieve
|
14
|
-
|
15
|
-
assert_equal(
|
15
|
+
set = SunTimes.calculate(:set, Date.new(2010, 3, 8), 43.779, 11.432)
|
16
|
+
assert_equal(Time.gm(2010, 3, 8, 17, 11, 16), set)
|
16
17
|
end
|
17
18
|
|
18
19
|
def test_rise_helper
|
19
20
|
rise = SunTimes.rise(Date.new(2010, 3, 8), 43.779, 11.432)
|
20
|
-
assert_equal(
|
21
|
+
assert_equal(Time.gm(2010, 3, 8, 5, 39, 53), rise)
|
21
22
|
end
|
22
23
|
|
23
24
|
def test_set_helper
|
24
|
-
|
25
|
-
assert_equal(
|
25
|
+
set = SunTimes.set(Date.new(2010, 3, 8), 43.779, 11.432)
|
26
|
+
assert_equal(Time.gm(2010, 3, 8, 17, 11, 16), set)
|
26
27
|
end
|
27
28
|
|
28
29
|
def test_midnight_sun_on_20100621_north_cape
|
@@ -36,4 +37,24 @@ class SunTimesTest < Test::Unit::TestCase
|
|
36
37
|
assert_raise(RuntimeError) { SunTimes.calculate(:foo, Date.new(2010, 3, 8), 43.779, 11.432) }
|
37
38
|
end
|
38
39
|
|
40
|
+
def test_time
|
41
|
+
datetime = Time.gm(2010, 6, 13, 0, 0, 0)
|
42
|
+
set = SunTimes.calculate(:set, datetime, 43.779, 11.432)
|
43
|
+
assert_equal(Time.gm(2010, 6, 13, 18, 56, 55), set)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_datetime
|
47
|
+
datetime = DateTime.new(2010, 6, 13, 0, 0, 0)
|
48
|
+
set = SunTimes.calculate(:set, datetime, 43.779, 11.432)
|
49
|
+
assert_equal(Time.gm(2010, 6, 13, 18, 56, 55), set)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_respects_timezone_if_supplied
|
53
|
+
pst = DateTime.new(2011, 12, 13, 0, 0, 0, Rational(-8, 24))
|
54
|
+
set = SunTimes.calculate(:set, pst, 45.52, -122.681944)
|
55
|
+
result_datetime = DateTime.new(set.year, set.month, set.day, set.hour, set.min, set.sec, 0)
|
56
|
+
assert(pst < result_datetime)
|
57
|
+
assert(pst + 1 > result_datetime)
|
58
|
+
end
|
59
|
+
|
39
60
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-sun-times
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Yates
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2011-12-20 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|