camp_daylight 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a72f5f02a197a110d67f13cd11e7f59852180e09b29dcf99a0256d64f0af813d
4
+ data.tar.gz: cad31890748d57b3526119731bcc1a1c2d18598c3f112353c9a9b0418a4aca00
5
+ SHA512:
6
+ metadata.gz: ca8e7d9e39319c0b80f3a76f37d0c8e2e0e8b88471820a48f5ae4853eb930305a64091731c7aa24251b86660c4d75171d2e7b28dbf8e07904d3b239ea8660774
7
+ data.tar.gz: dc5d8cb155524af64614a876110e7ad9af34cf679890cb1cb5752ff214927441b898f7ac4a628dc26363fce7afab7ef63dd984b4a7e53fd3b03bef699f9d6838
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Camp Kitchen Field Notes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # camp_daylight
2
+
3
+ Know when the light runs out before you start dinner. `camp_daylight` computes sunrise, sunset and civil dusk for a latitude, longitude and date, and tells you when to light the stove so cooking and washing up finish in daylight. It is a small, dependency-free Ruby library with a command line tool, written for the people at [Camp Kitchen](https://campkitchen.us/) who plan evening meals at campsites, trailheads and tailgates.
4
+
5
+ It uses the NOAA general solar position equations. Results agree with an independent implementation (the Python `astral` library) to within about two minutes in the test suite. It does not account for terrain: in a canyon or behind a ridge the sun disappears earlier than the horizon-based time.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ gem install camp_daylight
11
+ ```
12
+
13
+ ## Library
14
+
15
+ ```ruby
16
+ require "camp_daylight"
17
+
18
+ # Valencia, California, 4 July 2026, Pacific Daylight Time (UTC-7)
19
+ r = CampDaylight.sun_times(lat: 34.4344, lon: -118.5714, date: "2026-07-04", utc_offset: -7 * 3600)
20
+ r.sunrise # => 2026-07-04 05:46 -0700
21
+ r.sunset # => 2026-07-04 20:10 -0700
22
+ r.civil_dusk # => 2026-07-04 20:39 -0700
23
+ r.day_length_minutes # => 864
24
+
25
+ # When must the stove be lit for a 45 minute meal plus a 30 minute washing-up buffer?
26
+ CampDaylight.start_cooking_by(lat: 34.4344, lon: -118.5714, date: "2026-07-04",
27
+ meal_minutes: 45, utc_offset: -7 * 3600)
28
+ # => 2026-07-04 18:55 -0700
29
+ ```
30
+
31
+ Latitude and longitude are decimal degrees (east and north positive). Times are returned in UTC unless you pass `utc_offset` in seconds. Near the poles the sun may not rise or set; `CampDaylight::PolarError` is raised in that case.
32
+
33
+ ## Command line
34
+
35
+ ```sh
36
+ camp-daylight --lat 34.4344 --lon -118.5714 --date 2026-07-04 --utc-offset -7 --meal 45
37
+ ```
38
+
39
+ ```
40
+ Date: 2026-07-04
41
+ Sunrise: 05:46
42
+ Sunset: 20:10
43
+ Civil dusk: 20:39
44
+ Day length: 14h 24m
45
+ Light the stove by: 18:55 (meal 45 min + 30 min buffer)
46
+ ```
47
+
48
+ ## Development
49
+
50
+ ```sh
51
+ ruby -Ilib -Itest test/camp_daylight_test.rb
52
+ ```
53
+
54
+ ## License
55
+
56
+ MIT
data/exe/camp-daylight ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "camp_daylight"
5
+ require "optparse"
6
+
7
+ opts = { date: Date.today.to_s, offset: 0.0 }
8
+ OptionParser.new do |o|
9
+ o.banner = "Usage: camp-daylight --lat 34.43 --lon -118.57 [--date 2026-07-04] [--utc-offset -7] [--meal 45]"
10
+ o.on("--lat F", Float) { |v| opts[:lat] = v }
11
+ o.on("--lon F", Float) { |v| opts[:lon] = v }
12
+ o.on("--date D") { |v| opts[:date] = v }
13
+ o.on("--utc-offset H", Float, "hours from UTC, e.g. -7") { |v| opts[:offset] = v }
14
+ o.on("--meal MIN", Float, "meal prep+cook minutes") { |v| opts[:meal] = v }
15
+ end.parse!
16
+ abort "need --lat and --lon" unless opts[:lat] && opts[:lon]
17
+
18
+ off = (opts[:offset] * 3600).to_i
19
+ r = CampDaylight.sun_times(lat: opts[:lat], lon: opts[:lon], date: opts[:date], utc_offset: off)
20
+ fmt = ->(t) { t.strftime("%H:%M") }
21
+ puts "Date: #{r.date}"
22
+ puts "Sunrise: #{fmt.(r.sunrise)}"
23
+ puts "Sunset: #{fmt.(r.sunset)}"
24
+ puts "Civil dusk: #{fmt.(r.civil_dusk)}"
25
+ puts "Day length: #{r.day_length_minutes / 60}h #{r.day_length_minutes % 60}m"
26
+ if opts[:meal]
27
+ t = CampDaylight.start_cooking_by(lat: opts[:lat], lon: opts[:lon], date: opts[:date],
28
+ meal_minutes: opts[:meal], utc_offset: off)
29
+ puts "Light the stove by: #{fmt.(t)} (meal #{opts[:meal].to_i} min + 30 min buffer)"
30
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CampDaylight
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "time"
5
+ require_relative "camp_daylight/version"
6
+
7
+ # Sunrise, sunset and "start cooking by" times for a campsite.
8
+ #
9
+ # Uses the NOAA general solar position equations (accuracy about one minute
10
+ # at mid latitudes). No dependencies, no network.
11
+ module CampDaylight
12
+ class PolarError < StandardError; end
13
+
14
+ ZENITH_OFFICIAL = 90.833 # sunrise/sunset: sun's upper limb at the horizon, with refraction
15
+ ZENITH_CIVIL = 96.0 # end of civil twilight
16
+
17
+ Result = Struct.new(:date, :sunrise, :sunset, :civil_dusk, :day_length_minutes, keyword_init: true)
18
+
19
+ module_function
20
+
21
+ # Compute sun times for a latitude/longitude (decimal degrees, east positive)
22
+ # on a calendar date. Times are returned as UTC +Time+ objects; pass
23
+ # +utc_offset+ (seconds, e.g. -8 * 3600) to get local times instead.
24
+ #
25
+ # Raises PolarError when the sun does not rise or set that day.
26
+ def sun_times(lat:, lon:, date:, utc_offset: 0)
27
+ raise ArgumentError, "latitude must be between -90 and 90" unless lat.between?(-90, 90)
28
+ raise ArgumentError, "longitude must be between -180 and 180" unless lon.between?(-180, 180)
29
+
30
+ date = Date.parse(date) if date.is_a?(String)
31
+ sunrise = event(lat, lon, date, ZENITH_OFFICIAL, :rise)
32
+ sunset = event(lat, lon, date, ZENITH_OFFICIAL, :set)
33
+ dusk = event(lat, lon, date, ZENITH_CIVIL, :set)
34
+ to_local = ->(t) { t.getlocal(utc_offset) }
35
+ Result.new(
36
+ date: date,
37
+ sunrise: to_local.call(sunrise),
38
+ sunset: to_local.call(sunset),
39
+ civil_dusk: to_local.call(dusk),
40
+ day_length_minutes: ((sunset - sunrise) / 60.0).round
41
+ )
42
+ end
43
+
44
+ # Time by which you should have the stove lit so dinner is done before
45
+ # sunset. +meal_minutes+ is prep plus cooking time, +buffer_minutes+ is slack
46
+ # for washing up in daylight.
47
+ def start_cooking_by(lat:, lon:, date:, meal_minutes:, buffer_minutes: 30, utc_offset: 0)
48
+ raise ArgumentError, "meal_minutes must be positive" unless meal_minutes.to_f.positive?
49
+
50
+ r = sun_times(lat: lat, lon: lon, date: date, utc_offset: utc_offset)
51
+ r.sunset - (meal_minutes + buffer_minutes) * 60
52
+ end
53
+
54
+ def event(lat, lon, date, zenith, kind)
55
+ # Two passes: first at solar noon guess, then refined at the event time.
56
+ jd_midnight = date.ajd.to_f - 0.5 # julian day at 00:00 UTC of date
57
+ minutes = 720.0 - 4.0 * lon
58
+ 2.times do
59
+ t = (jd_midnight + minutes / 1440.0 - 2_451_545.0) / 36_525.0
60
+ eq_time, decl = solar_terms(t)
61
+ ha = hour_angle(lat, decl, zenith)
62
+ minutes = 720.0 - 4.0 * (lon + (kind == :rise ? ha : -ha)) - eq_time
63
+ end
64
+ Time.utc(date.year, date.month, date.day) + minutes * 60.0
65
+ end
66
+ private_class_method :event
67
+
68
+ def hour_angle(lat, decl, zenith)
69
+ la = rad(lat)
70
+ d = rad(decl)
71
+ cos_h = (Math.cos(rad(zenith)) / (Math.cos(la) * Math.cos(d))) - Math.tan(la) * Math.tan(d)
72
+ raise PolarError, "the sun does not rise or set on this date at this latitude" if cos_h.abs > 1
73
+
74
+ deg(Math.acos(cos_h))
75
+ end
76
+ private_class_method :hour_angle
77
+
78
+ # Returns [equation_of_time_minutes, declination_degrees] for Julian century t.
79
+ def solar_terms(t)
80
+ l0 = (280.46646 + t * (36_000.76983 + t * 0.0003032)) % 360
81
+ m = 357.52911 + t * (35_999.05029 - 0.0001537 * t)
82
+ e = 0.016708634 - t * (0.000042037 + 0.0000001267 * t)
83
+ mr = rad(m)
84
+ c = Math.sin(mr) * (1.914602 - t * (0.004817 + 0.000014 * t)) +
85
+ Math.sin(2 * mr) * (0.019993 - 0.000101 * t) +
86
+ Math.sin(3 * mr) * 0.000289
87
+ true_long = l0 + c
88
+ omega = 125.04 - 1934.136 * t
89
+ lambda = true_long - 0.00569 - 0.00478 * Math.sin(rad(omega))
90
+ eps0 = 23.0 + (26.0 + (21.448 - t * (46.815 + t * (0.00059 - t * 0.001813))) / 60.0) / 60.0
91
+ eps = eps0 + 0.00256 * Math.cos(rad(omega))
92
+ decl = deg(Math.asin(Math.sin(rad(eps)) * Math.sin(rad(lambda))))
93
+ y = Math.tan(rad(eps) / 2)**2
94
+ l0r = rad(l0)
95
+ eq = 4.0 * deg(y * Math.sin(2 * l0r) - 2 * e * Math.sin(mr) +
96
+ 4 * e * y * Math.sin(mr) * Math.cos(2 * l0r) -
97
+ 0.5 * y * y * Math.sin(4 * l0r) - 1.25 * e * e * Math.sin(2 * mr))
98
+ [eq, decl]
99
+ end
100
+ private_class_method :solar_terms
101
+
102
+ def rad(x) = x * Math::PI / 180.0
103
+ def deg(x) = x * 180.0 / Math::PI
104
+ private_class_method :rad, :deg
105
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: camp_daylight
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Camp Kitchen Field Notes
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Small dependency-free Ruby library and command line tool that computes
13
+ sunrise, sunset and civil dusk for a latitude, longitude and date (NOAA solar equations,
14
+ about one minute accuracy), and works out when to light the camp stove so dinner
15
+ and washing up finish in daylight.
16
+ email:
17
+ - campkitchenus@gmail.com
18
+ executables:
19
+ - camp-daylight
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - LICENSE.txt
24
+ - README.md
25
+ - exe/camp-daylight
26
+ - lib/camp_daylight.rb
27
+ - lib/camp_daylight/version.rb
28
+ homepage: https://campkitchen.us/
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ homepage_uri: https://campkitchen.us/
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.6.7
48
+ specification_version: 4
49
+ summary: Sunrise, sunset and start-cooking-by times for a campsite.
50
+ test_files: []