class NewTime::NewTime
Attributes
day[RW]
fractional[RW]
hours[RW]
minutes[RW]
month[RW]
seconds[RW]
year[RW]
Public Class Methods
convert(time, point)
click to toggle source
# File lib/new_time.rb, line 57 def self.convert(time, point) sunrise_today = sunrise(time.to_date, point) sunset_today = sunset(time.to_date, point) # During daylight hours? if time >= sunrise_today && time < sunset_today start, finish = sunrise_today, sunset_today new_start_hour = 6 new_date = time.to_date else # Is it before sunrise or after sunset? if time < sunrise_today new_date = time.to_date - 1 else new_date = time.to_date end new_start_hour = 18 start = sunset(new_date, point) finish = sunrise(new_date + 1, point) end new_seconds = (new_start_hour + (time - start).to_f / (finish - start) * 12) * 60 * 60 new_fractional = new_seconds - new_seconds.floor new_seconds = new_seconds.floor new_minutes = new_seconds / 60 new_seconds -= new_minutes * 60 new_hours = new_minutes / 60 new_minutes -= new_hours * 60 if new_hours >= 24 new_hours -= 24 new_date += 1 end NewTime.new(new_date.year, new_date.month, new_date.day, new_hours, new_minutes, new_seconds, new_fractional) end
current_time(point)
click to toggle source
# File lib/new_time.rb, line 20 def self.current_time(point) convert(Time.now, point) end
new(year, month, day, hours, minutes, seconds, fractional)
click to toggle source
TODO Make seconds a float and get rid of fractional
# File lib/new_time.rb, line 16 def initialize(year, month, day, hours, minutes, seconds, fractional) @year, @month, @day, @hours, @minutes, @seconds, @fractional = year, month, day, hours, minutes, seconds, fractional end
sunrise(date, point)
click to toggle source
# File lib/new_time.rb, line 24 def self.sunrise(date, point) SolarEventCalculator.new(date, point.latitude, point.longitude).compute_official_sunrise(point.tz).to_time end
sunset(date, point)
click to toggle source
# File lib/new_time.rb, line 28 def self.sunset(date, point) SolarEventCalculator.new(date, point.latitude, point.longitude).compute_official_sunset(point.tz).to_time end
Public Instance Methods
convert(point)
click to toggle source
Convert back to “normal” time
# File lib/new_time.rb, line 33 def convert(point) today = Date.new(year, month, day) new_seconds = seconds + fractional + (minutes + hours * 60) * 60 # During daylight hours? if hours >= 6 && hours < 18 start = NewTime.sunrise(today, point) finish = NewTime.sunset(today, point) new_start_hour = 6 else # Is it before sunrise or after sunset? if hours < 6 start = NewTime.sunset(today - 1, point) finish = NewTime.sunrise(today, point) new_start_hour = 18 - 24 else start = NewTime.sunset(today, point) finish = NewTime.sunrise(today + 1, point) new_start_hour = 18 end end start + (new_seconds.to_f / (60 * 60) - new_start_hour) * (finish - start) / 12 end
date_to_s()
click to toggle source
# File lib/new_time.rb, line 102 def date_to_s Date.new(year, month, day).to_s end
time_to_s()
click to toggle source
# File lib/new_time.rb, line 94 def time_to_s if hours > 12 "%i:%02i pm" % [hours - 12, minutes] else "%i:%02i am" % [hours, minutes] end end
to_s()
click to toggle source
# File lib/new_time.rb, line 106 def to_s date_to_s + " " + time_to_s end