~huyngo/amlich

f1deca5482135335b7fd86c249ce7f4a77c67ed5 — Ngô Ngọc Đức Huy 4 months ago main
add initial code

clean up later
2 files changed, 190 insertions(+), 0 deletions(-)

A LICENSE
A amlich.py
A  => LICENSE +21 -0
@@ 1,21 @@
MIT License

Copyright (c) 2022 Ngô Ngọc Đức Huy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

A  => amlich.py +169 -0
@@ 1,169 @@
#!/usr/bin/env python3
from datetime import date
from math import floor, pi, sin
from typing import Tuple


class LunarDate:
    CAN = ['giáp', 'ất', 'bính', 'đinh', 'mậu', 'kỷ', 'canh', 'tân', 'nhâm', 'quý']
    CHI = ['tý', 'sửu', 'dần', 'mão', 'thìn', 'tỵ', 'ngọ', 'mùi', 'thân', 'dậu', 'tuất', 'hợi']
    def __init__(self, year: int, month: int, day: int) -> None:
        """Don't directly use this"""
        # we use corresponding solar year
        self.year = year
        self.month = month
        self.day = day

    @staticmethod
    def from_solar_date(solar_date: date, tz: int):
        y, m, d = date_to_lunar(solar_date, tz)
        lunar_date = LunarDate(y, m, d)
        lunar_date.julian_days = get_julian_days(solar_date)
        return lunar_date

    @property
    def year_can(self):
        return self.CAN[(self.year + 6) % 10]

    @property
    def year_chi(self):
        return self.CHI[(self.year + 8) % 12]

    @property
    def day_can(self):
        return self.CAN[(self.julian_days + 9) % 10]

    @property
    def day_chi(self):
        return self.CHI[(self.julian_days + 1) % 12]

    def __repr__(self) -> str:
        return __str__(self)

    def __str__(self) -> str:
        return f'năm {self.year} ({self.year_can} {self.year_chi}) tháng {self.month} ngày {self.day}'


def get_julian_days(d: date) -> int:
    a = floor((14 - d.month) / 12)
    y = d.year + 4800 - a
    m = d.month + 12 * a - 3
    n_leap_years = floor(y/4) - floor(y/100) + floor(y/400)
    julian_days = d.day + floor((153 * m + 2) / 5) \
        + 365 * y + n_leap_years - 32045
    if julian_days < 2299161:
        julian_days = d.day + floor((153*m+2)/5) + 365*y + floor(y/4) - 32083
    return julian_days


def get_new_moon_day(k: int, tz: int) -> int:
    # Time in Julian centuries from 1900 January 0.5
    T = k/1236.85
    T2 = T * T
    T3 = T2 * T
    dr = pi / 180
    Jd1 = 2415020.75933 + 29.53058868*k + 0.0001178*T2 - 0.000000155*T3
    # Mean new moon
    Jd1 = Jd1 + 0.00033*sin((166.56 + 132.87*T - 0.009173*T2)*dr)
    # Sun's mean anomaly
    M = 359.2242 + 29.10535608*k - 0.0000333*T2 - 0.00000347*T3
    # Moon's mean anomaly
    Mpr = 306.0253 + 385.81691806*k + 0.0107306*T2 + 0.00001236*T3
    # Moon's argument of latitude
    F = 21.2964 + 390.67050646*k - 0.0016528*T2 - 0.00000239*T3
    C1=(0.1734 - 0.000393*T)*sin(M*dr) + 0.0021*sin(2*dr*M)
    C1 = C1 - 0.4068*sin(Mpr*dr) + 0.0161*sin(dr*2*Mpr)
    C1 = C1 - 0.0004*sin(dr*3*Mpr)
    C1 = C1 + 0.0104*sin(dr*2*F) - 0.0051*sin(dr*(M+Mpr))
    C1 = C1 - 0.0074*sin(dr*(M-Mpr)) + 0.0004*sin(dr*(2*F+M))
    C1 = C1 - 0.0004*sin(dr*(2*F-M)) - 0.0006*sin(dr*(2*F+Mpr))
    C1 = C1 + 0.0010*sin(dr*(2*F-Mpr)) + 0.0005*sin(dr*(2*Mpr+M))
    if T < -11:
        deltat= 0.001 + 0.000839*T + 0.0002261*T2 - 0.00000845*T3 - 0.000000081*T*T3
    else:
        deltat= -0.000278 + 0.000265*T + 0.000262*T2
    JdNew = Jd1 + C1 - deltat
    return floor(JdNew + 0.5 + tz/24)


def get_sun_long(julian_days: int, tz: int) -> int:
    # Time in Julian centuries from 2000-01-01 12:00:00 GMT
    t = (julian_days - 2451545.5 - tz/24) / 36525
    t2 = t*t
    # degree to radian
    dr = pi/180
    # mean anomaly, degree
    m = 357.52910 + 35999.05030*t - 0.0001559*t2 - 0.00000048*t*t2
    # mean longitude, degree
    l0 = 280.46645 + 36000.76983*t + 0.0003032*t2
    dl = (1.914600 - 0.004817*t - 0.000014*t2)*sin(dr*m)
    dl = dl + (0.019993 - 0.000101*t)*sin(dr*2*m) + 0.000290*sin(dr*3*m)
    # true longitude, degree
    l = l0 + dl
    l = l*dr
    l = l - pi*2*(floor(l/(pi*2))) # normalize to (0, 2*pi)
    return floor(l / pi * 6)


def get_lunar_month_11(year: int, tz: int) -> int:
    d = date(year, 12, 31)
    off = get_julian_days(d) - 2415021
    k = floor(off / 29.530588853)
    nm = get_new_moon_day(k, tz)
    # sun longitude at local midnight
    sun_long = get_sun_long(nm, tz)
    if sun_long >= 9:
        nm = get_new_moon_day(k-1, tz)
    return nm


def get_leap_month_offset(d_11: int, tz: int) -> int:
    k = floor((d_11 - 2415021.076998695) / 29.530588853 + 0.5)
    last = 0
    # We start with the month following lunar month 11
    i = 1
    arc = get_sun_long(get_new_moon_day(k+i, tz), tz)
    while True:
        last = arc
        i += 1
        arc = get_sun_long(get_new_moon_day(k+i, tz), tz)
        if arc == last or i >= 14:
            break
    return i-1


def date_to_lunar(solar_date: date, tz: int) -> Tuple[int, int, int]:
    julian_days = get_julian_days(solar_date)
    k = floor((julian_days - 2415021.076998695) / 29.530588853)
    month_start = get_new_moon_day(k+1, tz)
    if month_start > julian_days:
        month_start = get_new_moon_day(k, tz)
    a11 = get_lunar_month_11(solar_date.year, tz)
    b11 = a11
    if a11 >= month_start:
        lunar_year = solar_date.year
        a11 = get_lunar_month_11(solar_date.year-1, tz)
    else:
        lunar_year = solar_date.year+1
        b11 = get_lunar_month_11(solar_date.year+1, tz)
    lunar_day = julian_days - month_start + 1
    diff = floor((month_start - a11)/29)
    lunar_leap = 0
    lunar_month = diff+11
    if b11 - a11 > 365:
        leap_month_diff = get_leap_month_offset(a11, tz)
        if diff >= leap_month_diff:
            lunar_month = diff + 10
            if diff == leap_month_diff:
                lunar_leap = 1
    if lunar_month > 12:
        lunar_month = lunar_month - 12
    if lunar_month >= 11 and diff < 4:
        lunar_year -= 1
    return lunar_year, lunar_month, lunar_day


if __name__ == '__main__':
    today = date.today()
    timezone = 7
    print(LunarDate.from_solar_date(today, timezone))