Class: DaylightChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/blink_tm/daylight_checker.rb

Constant Summary collapse

EARTH_ORBITAL_DEGREE_PER_DAY =

Each day earth moves across sun about 360 / 365.24 = 0.9856

0.9856
MEAN_ANOMALY_ADJUSTMENT =

Is a constant that adjusts the mean anomaly based on the time of year. It’s derived from a more complex formula involving the Earth’s orbital eccentricity and the position of perihelion (the point in Earth’s orbit that’s closest to the Sun).

3.289
AXIAL_TILT =
0.39782
TIME_CORRECTION_FACTOR =
0.06571
DEGREES_TO_RADIANS =
Math::PI / 180
RADIANS_TO_DEGREES =
180 / Math::PI
COSINE_OBLIQUITY_ECLIPTIC =

Math.cos(23.44 (angle in degrees) * (Math::PI / 180))

0.91747

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(latitude, longitude, time) ⇒ DaylightChecker

Returns a new instance of DaylightChecker.



21
22
23
24
25
26
27
# File 'lib/blink_tm/daylight_checker.rb', line 21

def initialize(latitude, longitude, time)
	@latitude = latitude
	@longitude = longitude
	@time = time
	@date = time.to_date
	@offset_in_hours = time.utc_offset / 3600.0
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



4
5
6
# File 'lib/blink_tm/daylight_checker.rb', line 4

def date
  @date
end

#latitudeObject (readonly)

Returns the value of attribute latitude.



4
5
6
# File 'lib/blink_tm/daylight_checker.rb', line 4

def latitude
  @latitude
end

#longitudeObject (readonly)

Returns the value of attribute longitude.



4
5
6
# File 'lib/blink_tm/daylight_checker.rb', line 4

def longitude
  @longitude
end

#offset_in_hoursObject (readonly)

Returns the value of attribute offset_in_hours.



4
5
6
# File 'lib/blink_tm/daylight_checker.rb', line 4

def offset_in_hours
  @offset_in_hours
end

#timeObject (readonly)

Returns the value of attribute time.



4
5
6
# File 'lib/blink_tm/daylight_checker.rb', line 4

def time
  @time
end

Instance Method Details

#is_it_dark?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/blink_tm/daylight_checker.rb', line 29

def is_it_dark?
	sunrise_time, sunset_time = sun_times(date, latitude, longitude, offset_in_hours)
	current_time = time.utc.hour + offset_in_hours

	current_time < sunrise_time or current_time > sunset_time
end