class BritishSuntimes

Attributes

bst_end[R]
bst_start[R]
longest_day[R]
shortest_day[R]
to_dx[R]
to_h[R]

Public Class Methods

new(year=Date.today.year.to_s, location: 'edinburgh', debug: false) click to toggle source
# File lib/british_suntimes.rb, line 21
def initialize(year=Date.today.year.to_s, location: 'edinburgh', 
               debug: false)
  
  @location, @debug = location, debug

  a = (Date.parse(year + ' Jan')...Date.parse(year.succ + ' Jan')).to_a
  g = Geocoder.search(location)

  puts 'finding the times ...' if @debug
  times = a.inject({}) do |r, date|

    calc = SolarEventCalculator.new(date, *g[0].coordinates)

    a2 = %w(sunrise sunset).map do |event|
      calc.method('compute_utc_official_'.+(event).to_sym).call\
        .strftime("%H:%M")
    end

    r.merge(date.strftime("%d %b") => a2)
  end

  # alter the times for British summertime

  @bst_start = ChronicCron.new('last sunday in March at 1am', 
        Time.local(year)).to_date
  @bst_end = ChronicCron.new('last sunday in October at 2am', 
        Time.local(year)).to_date


  (@bst_start...@bst_end).to_a.each do |d|
    t1, t2 = times[d.strftime("%d %b")].map {|x| Time.parse(x)}
    t1 += 60 * 60; t2 += 60 * 60
    times[d.strftime("%d %b")] = [t1.strftime("%H:%M"), t2.strftime("%H:%M")]
  end

  @year, @location, @to_h = year, location, times
  puts 'buklding @to_dx' if @debug
  @to_dx = build_dx()    
  
end

Public Instance Methods

export(filename=@location + "-suntimes- click to toggle source

exports the sun times to a raw Dynarex file

# File lib/british_suntimes.rb, line 64
def export(filename=@location + "-suntimes-#{@year}.txt")
  File.write filename, @to_dx.to_s
end

Private Instance Methods

build_dx() click to toggle source
# File lib/british_suntimes.rb, line 70
def build_dx()

  dx = Dynarex.new 'times[title, tags, desc, bst_start, ' + 
      'bst_end, longest_day, shortest_day]/day(date, sunrise, sunset)'
  dx.title = @location + " sunrise sunset times " + @year
  dx.tags = @location + " times sunrise sunset %s" % [@year]
  dx.delimiter = ' # '
  dx.bst_start, dx.bst_end = @bst_start.to_s, @bst_end.to_s
  dx.desc = 'Adjusted for British summertime'

  @to_h.each {|k,v| dx.create date: k, sunrise: v[0], sunset: v[1] }
  
  a = dx.all.map do |x| 
    Time.parse(x.date + ' ' + x.sunset) - Time.parse(x.date + ' ' + x.sunrise)
  end

  shortest, longest = a.minmax.map {|x| dx.all[a.index(x)]}    
  @longest_day = dx.longest_day = longest.date
  @shortest_day = dx.shortest_day = shortest.date
  
  dx
end