tyme4ts

repository·master·Indexed 19 days ago

https://github.com/6tail/tyme4ts

A powerful calendar utility library for TypeScript that supports multiple calendar systems including Solar, Lunar, Tibetan (Rab Byung), and Hijri. It provides comprehensive tools for astrological and cultural time calculations, including zodiacs, solar terms, moon phases, statutory holidays, the 60-cycle combination of Heaven Stems and Earth Branches, and traditional Chinese taboos.

Tokens
12.9K
Snippets
56
Records
65
Agent score
17%

What's inside tyme4ts

  1. Install tyme4ts

    master

    To use tyme4ts in a TypeScript project, you need to install the package along with typescript and ts-node for execution. Run the following commands in your terminal:

    npm init -y
    npm i typescript -D
    npm i ts-node -D
    npm i tyme4ts
  2. Use LoopTyme classes for cyclical time units

    master

    Many entities in the library (Animals, Stars, Elements, etc.) are implemented as LoopTyme classes. These represent values that cycle through a fixed set of names.

    Common patterns for LoopTyme subclasses:

    • Creation: Use fromIndex(index) or fromName(name) to instantiate an object.
    • Navigation: Use next(n) to move forward or backward in the cycle.
    • Metadata: Most classes provide getName(), getIndex(), and getSize().
  3. Core Interfaces: Culture and Tyme

    master

    The library is built around two primary interfaces:

    • Culture: Represents a specific cultural time system. It must implement getName() (returning the culture's name) and toString() (returning a string representation).
    • Tyme: Extends Culture and adds the next(n: number) method, which returns the next Tyme instance after n steps, or null if no next value exists.

    All time units (Year, Month, Day, etc.) are implementations of these interfaces.

  4. Configure EightCharProvider for BaZi calculations

    master

    The EightCharProvider interface defines how to derive an EightChar from a LunarHour. You can swap the global provider on LunarHour to change the calculation logic.

    Available implementations:

    • DefaultEightCharProvider: Uses the standard hour.getSixtyCycleHour().getEightChar() logic.
    • LunarSect2EightCharProvider: Uses a specific logic where the month is derived from the lunar year/month components.

    To change the provider:

    LunarHour.provider = new LunarSect2EightCharProvider();
  5. Convert Solar dates to Lunar, Rab Byung, and Hijri calendars

    master

    You can create a SolarDay instance using SolarDay.fromYmd(year, month, day) and then access different calendar systems using the following methods:

    • getLunarDay(): Returns the Lunar calendar representation.
    • getRabByungDay(): Returns the Rab Byung (Tibetan) calendar representation.
    • getHijriDay(): Returns the Hijri calendar representation.

    Each method returns a day object that can be converted to a string using .toString().

    import {SolarDay} from 'tyme4ts';
    
    const solar: SolarDay = SolarDay.fromYmd(1986, 5, 29);
    
    // 1986年5月29日
    console.log(solar.toString());
    
    // 农历丙寅年四月廿一
    console.log(solar.getLunarDay().toString());
    
    // 第十七饶迥火虎年四月廿一
    console.log(solar.getRabByungDay().toString());
    
    // 1406年赖买丹月20日
    console.log(solar.getHijriDay().toString());
  6. Work with RabByungYear and RabByungMonth

    master

    The RabByungYear class handles the complex RabByung calendar system, including leap months and sixty-cycle integration. RabByungMonth handles individual months within that year, including leap month logic.

    RabByungYear Methods:

    • fromYear(year: number | string): Creates an instance from a standard year.
    • getSixtyCycle(), getZodiac(), getElement(): Returns the associated cycle, zodiac, or element.
    • getYear(): Returns the calculated RabByung year.
    • getLeapMonth(): Returns the index of the leap month (0 if none).
    • getMonths(): Returns an array of RabByungMonth objects for that year.

    RabByungMonth Methods:

    • fromYm(year: number | string, month: number | string): Creates an instance.
    • isLeap(): Returns true if it is a leap month.
    • getDays(): Returns an array of RabByungDay objects for the month.
    • next(n: number): Returns the next month after n steps.
    const year = RabByungYear.fromYear(2024);
    const months = year.getMonths();
    const firstMonth = year.getFirstMonth();
    
    const month = RabByungMonth.fromYm(2024, 1);
    const days = month.getDays();
  7. Use the TenStar class

    master

    The TenStar class represents the ten stars (十神) used in analysis of heavenly stems.

    Key methods:

    • fromIndex(index: number | string): Creates a TenStar instance.
    • fromName(name: string): Creates a TenStar instance.
    import { TenStar } from 'tyme4ts';
    
    const star = TenStar.fromName('正财');
  8. Use KitchenGodSteed for traditional cultural predictions

    master

    The KitchenGodSteed class (extending AbstractCulture) provides traditional cultural strings (often related to '灶马头' or Kitchen God) based on a specific lunar year. It uses Earth Branches and Heaven Stems to return specific phrases for different animals or elements.

    Key methods:

    • fromLunarYear(lunarYear: number | string): Static factory to create an instance from a lunar year.
    • getMouse(), getGrass(), getCattle(), getFlower(), getDragon(), getHorse(), getChicken(), getSilkworm(), getPig(), getField(), getCake(), getGold(), getPeopleCakes(), getPeopleHoes(): Returns specific cultural strings/predictions.
    • getName(): Returns the name of the culture ('灶马头').
    const steed = KitchenGodSteed.fromLunarYear(2024);
    console.log(steed.getMouse()); // e.g., "X鼠偷粮"
    console.log(steed.getGold());  // e.g., "X日得金"
  9. Use RabByungDay for specific date calculations

    master

    The RabByungDay class represents a specific day in the RabByung calendar. It supports leap days and conversion to SolarDay.

    Key methods:

    • fromYmd(year, month, day): Creates an instance.
    • fromSolarDay(solarDay: SolarDay): Converts a solar date to a RabByung date.
    • getSolarDay(): Converts the RabByung date to a SolarDay.
    • subtract(target: RabByungDay): Returns the difference in days between two dates.
    • next(n: number): Returns the date after n days (via solar conversion).
    const day = RabByungDay.fromYmd(2024, 1, 1);
    const solar = day.getSolarDay();
    const diff = day.subtract(someOtherRabByungDay);
  10. Work with LunarMonth (农历月份)

    master

    The LunarMonth class represents a month in the lunar calendar.

    Methods:

    • static fromYm(year: number | string, month: number | string): Create a LunarMonth instance. A negative month value indicates a leap month.
    • isLeap(): Returns true if this is a leap month.
    • getName(): Returns the name (e.g., '正月', '闰二月').
    • getDays(): Returns an array of LunarDay objects for this month.
    • getFirstDay(): Returns the first LunarDay of the month.
    • getWeeks(start: number): Returns an array of LunarWeek objects starting from a specific week index.
    • getSixtyCycle(): Returns the SixtyCycle for this month.
    • getFetus(): Returns the FetusMonth (if not a leap month).
    • getSeason(): Returns the LunarSeason this month belongs to.
    const month = LunarMonth.fromYm(2024, 1); // 正月
    const days = month.getDays();
  11. Build custom Events using EventBuilder

    master

    Use the EventBuilder to programmatically create Event objects with specific recurrence rules. The builder uses a fluent interface.

    Supported event types via builder:

    • solarDay(solarMonth, solarDay, delayDays)
    • lunarDay(lunarMonth, lunarDay, delayDays)
    • solarWeek(solarMonth, weekIndex, week)
    • termDay(termIndex, delayDays)
    • termHeavenStem(termIndex, heavenStemIndex, delayDays)
    • termEarthBranch(termIndex, earthBranchIndex, delayDays)
    const myEvent = Event.builder()
      .name('My Custom Event')
      .solarDay(3, 21, 0) // March 21st, 0 days delay
      .build(); // Note: build() is implied by the pattern, though the segment shows the builder methods