Module: LunarYear

Defined in:
lib/lunaryear.rb

Class Method Summary collapse

Class Method Details

.date_of_moons(date) ⇒ Object

Calculate the DateTime of the previous New and Full Moons and the DateTime of the next New and Full moons. Returns [previous New Moon, next New Moon, previous Full Moon, next Full Moon]

Example: For November 18, 2007, calculate the date and time of the previous New Moon, the next New Moon, the previous Full Moon, and the next Full Moon.

LunarYear.date_of_moons(DateTime.civil(2007, 11, 18)).collect {|d| d.asctime}
#=> ["Fri Nov  9 23:03:07 2007", "Sun Dec  9 17:40:21 2007", "Fri Oct 26 04:51:33 2007", "Sat Nov 24 14:29:47 2007"]


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lunaryear.rb', line 66

def LunarYear.date_of_moons(date)
    next_new_moon = nil
    next_full_moon = nil

    k = ((date.year - 2000)*12.3685).floor - 1
    prev_new_moon = Astro.date_of_moon(k, Astro::PhaseNew).to_utc
    lun = k
    loop do
        lun += 1
        next_new_moon = Astro.date_of_moon(lun, Astro::PhaseNew).to_utc
        break if next_new_moon > date
        prev_new_moon = next_new_moon
    end
    
    lun = k
    prev_full_moon = Astro.date_of_moon(k, Astro::PhaseFull).to_utc
    loop do
        lun += 1
        next_full_moon = Astro.date_of_moon(lun, Astro::PhaseFull).to_utc
        break if next_full_moon > date
        prev_full_moon = next_full_moon
    end

    [prev_new_moon, next_new_moon, prev_full_moon, next_full_moon]
end

.lunar_date(date) ⇒ Object

Calculate the lunar calendar date for the given DateTime. Returns [year, moonth, day].



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lunaryear.rb', line 18

def LunarYear.lunar_date(date)
    year = date.year
    lun0 = LunarYear.new_moon_before_vernal_equinox(year)        # Year begins at Vernal Equinox
    if date < Astro.date_of_moon(lun0, Astro::PhaseNew).to_date
        year -= 1
        lun0 = new_moon_before_vernal_equinox(year)
    end
    prev_moon = Astro.date_of_moon(lun0, Astro::PhaseNew).to_date
    lun = lun0
    loop do
        lun += 1
        new_moon = Astro.date_of_moon(lun, Astro::PhaseNew).to_date
        break if new_moon > date
        prev_moon = new_moon
    end
    moonth = lun - lun0 - 1
    day = (date - prev_moon).to_i
    [year, moonth, day]
end

.new_moon_before_vernal_equinox(year) ⇒ Object

Find number of lunation (New Moon) just before Vernal Equinox for a given year. Lunation 0 is the first New Moon in the year 2000. Returns an integer lunation number.

Example: first lunation before Vernal Equinox of the year 1984.

LunarYear.new_moon_before_vernal_equinox(1984)      # => -196


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lunaryear.rb', line 45

def LunarYear.new_moon_before_vernal_equinox(year)
    equ = Astro.date_of_vernal_equinox(year)
    lunation = Astro.first_lunation_of_year(year)
    new_moon = Astro.date_of_moon(lunation, Astro::PhaseNew)
    loop do
        previous_new_moon = new_moon
        lunation += 1
        new_moon = Astro.date_of_moon(lunation, Astro::PhaseNew)
        return lunation-1 if new_moon > equ
    end
end