ruby-sun-times 0.1.3 → 0.1.4
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.
- checksums.yaml +4 -4
- data/{README → README.md} +31 -4
- data/lib/sun_times.rb +12 -5
- data/lib/sun_times/version.rb +1 -1
- data/spec/unit/ruby_sun_times_spec.rb +22 -7
- metadata +2 -4
- data/test/calculate_test.rb +0 -25
- data/test/test_all.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ecb13177df09644d48fc75563740cec84495ce00
|
4
|
+
data.tar.gz: c9002e7be0138db8ba4409844d97f7ecab102187
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b0a05c96603d12fcc58821a79b39ccc74a7f0b7034540b32c5aafba39cd41035dc330bb10eadcceefd4a4a8f11be523c6678901b0d600ee964d2e47a1ff1dee
|
7
|
+
data.tar.gz: 35671f27cd9a50ca1e1db445021a870bf87abb13257c544acd169e1c2a09c15088b7b7a2a26672a6d83c0060d42867729654d9e80efcbf7f973c472b96434735
|
data/{README → README.md}
RENAMED
@@ -1,20 +1,47 @@
|
|
1
|
-
|
1
|
+
# SunTimes
|
2
2
|
|
3
3
|
Calculates sunrise and sunset times.
|
4
4
|
|
5
5
|
An implementation of the algorithm descibed at http://williams.best.vwh.net/sunrise_sunset_algorithm.htm
|
6
6
|
|
7
|
-
|
7
|
+
# Usage
|
8
|
+
|
9
|
+
## Requiring
|
10
|
+
|
11
|
+
In a Gemfile/Gemspec:
|
12
|
+
|
13
|
+
```
|
14
|
+
gem 'ruby-sun-times', require: 'sun_times'
|
15
|
+
```
|
16
|
+
|
17
|
+
Directly:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'sun_times'
|
21
|
+
```
|
22
|
+
|
23
|
+
## Calls
|
24
|
+
|
25
|
+
There are two helper methods: 'rise' and 'set'.
|
26
|
+
|
27
|
+
```
|
28
|
+
day = Date.new(2010, 3, 8)
|
29
|
+
latitude = 43.779
|
30
|
+
longitude = 11.432
|
31
|
+
puts SunTimes.set(day, latitude, longitude)
|
32
|
+
```
|
33
|
+
|
34
|
+
# References
|
8
35
|
|
9
36
|
* http://www.astro.uu.nl/~strous/AA/en/reken/zonpositie.html - Calculations
|
10
37
|
* http://williams.best.vwh.net/sunrise_sunset_algorithm.htm - Algorithm
|
11
38
|
* http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/264573 - Ken Bloom's implementation
|
12
39
|
|
13
|
-
|
40
|
+
# Licence
|
14
41
|
|
15
42
|
This code is free to use under the terms of the MIT licence:
|
16
43
|
|
17
|
-
Copyright (c) 2010 Joe Yates
|
44
|
+
Copyright (c) 2010 - 2013 Joe Yates
|
18
45
|
|
19
46
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
20
47
|
of this software and associated documentation files (the "Software"), to
|
data/lib/sun_times.rb
CHANGED
@@ -47,15 +47,22 @@ module SunTimes
|
|
47
47
|
# * +date+ - An object that responds to :to_datetime.
|
48
48
|
# * +latitude+ - The latitude of the location in degrees.
|
49
49
|
# * +longitude+ - The longitude of the location in degrees.
|
50
|
-
# * +options+ -
|
50
|
+
# * +options+ -
|
51
|
+
# * <tt>:never_rises_result</tt> - the value to be returned if the sun never rises on the supplied date,
|
52
|
+
# * <tt>:never_sets_result</tt> - the value to be returned if the sun never sets on the supplied date,
|
53
|
+
# * <tt>:zenith</tt> - default 90.83333
|
51
54
|
#
|
52
55
|
# ==== Example
|
53
56
|
# SunTimes.calculate(:rise, Date.new(2010, 3, 8), 43.779, 11.432)
|
54
57
|
# > Mon Mar 08 05:39:53 UTC 2010
|
55
58
|
def self.calculate(event, date, latitude, longitude, options = {})
|
59
|
+
options = {
|
60
|
+
:never_sets_result => nil,
|
61
|
+
:never_rises_result => nil,
|
62
|
+
:zenith => DEFAULT_ZENITH,
|
63
|
+
}.merge(options)
|
56
64
|
datetime = date.to_datetime
|
57
65
|
raise "Unknown event '#{ event }'" if KNOWN_EVENTS.find_index(event).nil?
|
58
|
-
zenith = options.delete(:zenith) || DEFAULT_ZENITH
|
59
66
|
|
60
67
|
# lngHour
|
61
68
|
longitude_hour = longitude / DEGREES_PER_HOUR
|
@@ -91,13 +98,13 @@ module SunTimes
|
|
91
98
|
cos_declination = Math.cos(Math.asin(sin_declination))
|
92
99
|
|
93
100
|
cos_local_hour_angle =
|
94
|
-
(Math.cos(degrees_to_radians(zenith)) - (sin_declination * Math.sin(degrees_to_radians(latitude)))) /
|
101
|
+
(Math.cos(degrees_to_radians(options[:zenith])) - (sin_declination * Math.sin(degrees_to_radians(latitude)))) /
|
95
102
|
(cos_declination * Math.cos(degrees_to_radians(latitude)))
|
96
103
|
|
97
104
|
# the sun never rises on this location (on the specified date)
|
98
|
-
return
|
105
|
+
return options[:never_rises_result] if cos_local_hour_angle > 1
|
99
106
|
# the sun never sets on this location (on the specified date)
|
100
|
-
return
|
107
|
+
return options[:never_sets_result] if cos_local_hour_angle < -1
|
101
108
|
|
102
109
|
# H
|
103
110
|
suns_local_hour =
|
data/lib/sun_times/version.rb
CHANGED
@@ -6,6 +6,10 @@ describe SunTimes do
|
|
6
6
|
let(:longitude) { 11.432 }
|
7
7
|
let(:rise) { Time.gm(2010, 3, 8, 5, 39, 53) }
|
8
8
|
let(:set) { Time.gm(2010, 3, 8, 17, 11, 16) }
|
9
|
+
let(:midsummer) { Date.new(2010, 6, 21) }
|
10
|
+
let(:midwinter) { Date.new(2010, 12, 21) }
|
11
|
+
let(:north_cape_latitude) { 71.170219 }
|
12
|
+
let(:north_cape_longitude) { 25.785556 }
|
9
13
|
|
10
14
|
describe '#calculate' do
|
11
15
|
context ':rise' do
|
@@ -44,18 +48,13 @@ describe SunTimes do
|
|
44
48
|
end
|
45
49
|
|
46
50
|
context 'midnight sun' do
|
47
|
-
# North Cape
|
48
|
-
let(:midsummer) { Date.new(2010, 6, 21) }
|
49
|
-
let(:latitude) { 71.170219 }
|
50
|
-
let(:longitude) { 25.785556 }
|
51
|
-
|
52
51
|
it 'rise is nil' do
|
53
|
-
result = SunTimes.calculate(:rise, midsummer,
|
52
|
+
result = SunTimes.calculate(:rise, midsummer, north_cape_latitude, north_cape_longitude)
|
54
53
|
expect(result).to be_nil
|
55
54
|
end
|
56
55
|
|
57
56
|
it 'set is nil' do
|
58
|
-
result = SunTimes.calculate(:set, midsummer,
|
57
|
+
result = SunTimes.calculate(:set, midsummer, north_cape_latitude, north_cape_longitude)
|
59
58
|
expect(result).to be_nil
|
60
59
|
end
|
61
60
|
end
|
@@ -68,6 +67,22 @@ describe SunTimes do
|
|
68
67
|
SunTimes.calculate(:set, day, 47.5, -122)
|
69
68
|
end
|
70
69
|
end
|
70
|
+
|
71
|
+
context 'options' do
|
72
|
+
context ':never_rises_result' do
|
73
|
+
it 'uses the supplied value, instead of nil' do
|
74
|
+
result = SunTimes.calculate(:rise, midwinter, north_cape_latitude, north_cape_longitude, {:never_rises_result => :never_rises})
|
75
|
+
expect(result).to eq(:never_rises)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context ':never_sets_result' do
|
80
|
+
it 'uses the supplied value, instead of nil' do
|
81
|
+
result = SunTimes.calculate(:rise, midsummer, north_cape_latitude, north_cape_longitude, {:never_sets_result => :never_sets})
|
82
|
+
expect(result).to eq(:never_sets)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
71
86
|
end
|
72
87
|
|
73
88
|
describe '#rise' do
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Yates
|
@@ -48,15 +48,13 @@ files:
|
|
48
48
|
- .rspec
|
49
49
|
- COPYING
|
50
50
|
- Gemfile
|
51
|
-
- README
|
51
|
+
- README.md
|
52
52
|
- Rakefile
|
53
53
|
- lib/sun_times.rb
|
54
54
|
- lib/sun_times/version.rb
|
55
55
|
- ruby-sun-times.gemspec
|
56
56
|
- spec/spec_helper.rb
|
57
57
|
- spec/unit/ruby_sun_times_spec.rb
|
58
|
-
- test/calculate_test.rb
|
59
|
-
- test/test_all.rb
|
60
58
|
homepage: https://github.com/joeyates/ruby-sun-times
|
61
59
|
licenses: []
|
62
60
|
metadata: {}
|
data/test/calculate_test.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
3
|
-
require 'test/unit'
|
4
|
-
require 'sun_times'
|
5
|
-
require 'date'
|
6
|
-
|
7
|
-
class SunTimesTest < Test::Unit::TestCase
|
8
|
-
|
9
|
-
=begin
|
10
|
-
def test_time
|
11
|
-
datetime = Time.gm(2010, 6, 13, 0, 0, 0)
|
12
|
-
set = SunTimes.calculate(:set, datetime, 43.779, 11.432)
|
13
|
-
assert_equal(Time.gm(2010, 6, 13, 18, 56, 55), set)
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_datetime
|
17
|
-
datetime = DateTime.new(2010, 6, 13, 0, 0, 0)
|
18
|
-
set = SunTimes.calculate(:set, datetime, 43.779, 11.432)
|
19
|
-
assert_equal(Time.gm(2010, 6, 13, 18, 56, 55), set)
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_31st_december
|
23
|
-
end
|
24
|
-
=end
|
25
|
-
end
|
data/test/test_all.rb
DELETED