tranzito_utils 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 122dd1fc8e93ac70a1f33f4c284b60e6c04d5e33571563a231481175d0ab9d72
4
- data.tar.gz: 00c6cb246bbad57d5ebb155fe2cf0786fe77d59327716b5982cdec20aa6a2e1f
3
+ metadata.gz: f9200b266283acc5bca16516873cfa19fd2df8f3da6ebdf3c00678224f797ff5
4
+ data.tar.gz: 3bb4b8c641003c58dbca7925b82fce1e9e9d536a8127bf545587de01a11611a8
5
5
  SHA512:
6
- metadata.gz: 98ddbdd2ee84b5f685d1a6caa43139140b04086639b12dc4c9044bde561a935133ca87d14dfba61083c3d62e8a8ebba61e021cc4081601aea9ad8737d1916939
7
- data.tar.gz: ef1ae50d044e96a2cc8734f1155b0086a6043169518b454b9ed5ae8edbce17b88b3fb635d0d5a7d99df3a79ecadbf2dff858cdd717a48309e6b93e3edb53ed01
6
+ metadata.gz: a3bab2fe8fa1c67a8fe1aaee70d8ff6737ab22429cd9392012cf403ee7f519ac8ec18569e54ee335a4a7a16d1976fcebe7e1d733cbb0098e66b5085aa29b4bc9
7
+ data.tar.gz: 6586213cd8e7dabfcf5eb483c41ec57167eacca52e51d5d5de1d54d4b3b5730eb2908e64f54b5dee68e80c1ff3f593c2564fe56159f23afe2be5436c7559298e
@@ -20,7 +20,7 @@
20
20
  %a#showDateField{ href: "", data: { period: "custom" }}
21
21
  Custom date range
22
22
 
23
- %form#dropdownCustomSelectionForm
24
- = hidden_field_tag :start_time_selector, (@period == "custom" ? @start_time : Date.today).strftime("%Y-%m-%d")
25
- = hidden_field_tag :end_time_selector, (@period == "custom" ? @end_time : Date.today).strftime("%Y-%m-%d")
26
- %input#customPeriodSelectDatePicker.hidden{type: "text"}
23
+ %form#dropdownCustomSelectionForm
24
+ = hidden_field_tag :start_time_selector, (@period == "custom" ? @start_time : Date.today).strftime("%Y-%m-%d")
25
+ = hidden_field_tag :end_time_selector, (@period == "custom" ? @end_time : Date.today).strftime("%Y-%m-%d")
26
+ %input#customPeriodSelectDatePicker.hidden{type: "text"}
@@ -0,0 +1,85 @@
1
+ class SolarPositionCalculator
2
+ LATITUDE_RANGE = (-90..90)
3
+ LONGITUDE_RANGE = (-180..180)
4
+
5
+ attr_reader :latitude, :longitude, :timezone
6
+
7
+ def initialize(latitude, longitude, timezone)
8
+ validate_latitude(latitude)
9
+ validate_longitude(longitude)
10
+ @latitude = latitude
11
+ @longitude = longitude
12
+ @timezone = timezone
13
+ end
14
+
15
+ def sunrise_time(date)
16
+ calculate_sun_time(date, :sunrise)
17
+ end
18
+
19
+ def sunset_time(date)
20
+ calculate_sun_time(date, :sunset)
21
+ end
22
+
23
+ private
24
+
25
+ def calculate_sun_time(date, type)
26
+ validate_date(date)
27
+ utc_time = calculate_utc_time(date, type)
28
+ utc_to_timezone(date, utc_time)
29
+ end
30
+
31
+ def calculate_utc_time(date, type)
32
+ time = Time.new(date.year, date.month, date.day, 12, 0, 0, timezone)
33
+ gamma = calculate_gamma(time)
34
+ eqtime = calculate_eqtime(gamma)
35
+
36
+ decl = calculate_decl(gamma)
37
+ ha = calculate_ha(decl)
38
+ calculate_sun_time_utc(ha, eqtime, type)
39
+ end
40
+
41
+ def calculate_sun_time_utc(ha, eqtime, type)
42
+ if type == :sunrise
43
+ 720 - 4 * (longitude + ha * 180 / Math::PI) - eqtime
44
+ else
45
+ 720 - 4 * (longitude - ha * 180 / Math::PI) - eqtime
46
+ end
47
+ end
48
+
49
+ def calculate_gamma(time)
50
+ 2 * Math::PI / (leap_year?(time.year) ? 366 : 365) * (time.yday - 1 + (time.hour - 12) / 24.0)
51
+ end
52
+
53
+ def calculate_eqtime(gamma)
54
+ 229.18 * (0.000075 + 0.001868 * Math.cos(gamma) - 0.032077 * Math.sin(gamma) - 0.014615 * Math.cos(2 * gamma) - 0.040849 * Math.sin(2 * gamma))
55
+ end
56
+
57
+ def calculate_decl(gamma)
58
+ 0.006918 - 0.399912 * Math.cos(gamma) + 0.070257 * Math.sin(gamma) - 0.006758 * Math.cos(2 * gamma) + 0.000907 * Math.sin(2 * gamma) - 0.002697 * Math.cos(3 * gamma) + 0.00148 * Math.sin(3 * gamma)
59
+ end
60
+
61
+ def calculate_ha(decl)
62
+ Math.acos(Math.cos(Math::PI / 180 * 90.833) / (Math.cos(Math::PI / 180 * latitude) * Math.cos(decl)) - Math.tan(Math::PI / 180 * latitude) * Math.tan(decl))
63
+ end
64
+
65
+ def utc_to_timezone(date, utc_time)
66
+ response = Time.parse("#{date} #{Time.at(utc_time * 60).utc.strftime("%H:%M:%S")}") + timezone * 3600
67
+ response.strftime("%H:%M:%S")
68
+ end
69
+
70
+ def leap_year?(year)
71
+ year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
72
+ end
73
+
74
+ def validate_latitude(latitude)
75
+ raise ArgumentError, "Latitude should be between -90 and 90 degrees" unless LATITUDE_RANGE.include?(latitude)
76
+ end
77
+
78
+ def validate_longitude(longitude)
79
+ raise ArgumentError, "Longitude should be between -180 and 180 degrees" unless LONGITUDE_RANGE.include?(longitude)
80
+ end
81
+
82
+ def validate_date(date)
83
+ raise ArgumentError, "Invalid date" unless date.is_a?(Date)
84
+ end
85
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TranzitoUtils
4
- VERSION = "1.2.0"
4
+ VERSION = "1.3.0"
5
5
  end
@@ -21,4 +21,5 @@ require "tranzito_utils/helpers/sortable_helper"
21
21
  require "tranzito_utils/helpers/helpers"
22
22
  require "tranzito_utils/services/time_parser"
23
23
  require "tranzito_utils/services/normalize"
24
+ require "tranzito_utils/services/solar_position_calculator"
24
25
  require "tranzito_utils/gem"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tranzito_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - willbarrettdev
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2023-03-03 00:00:00.000000000 Z
13
+ date: 2023-06-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -112,6 +112,7 @@ files:
112
112
  - lib/tranzito_utils/helpers/helpers.rb
113
113
  - lib/tranzito_utils/helpers/sortable_helper.rb
114
114
  - lib/tranzito_utils/services/normalize.rb
115
+ - lib/tranzito_utils/services/solar_position_calculator.rb
115
116
  - lib/tranzito_utils/services/time_parser.rb
116
117
  - lib/tranzito_utils/version.rb
117
118
  homepage: https://github.com/Tranzito/tranzito_utils