tyme4ts
repository·master·Indexed 19 days ago
https://github.com/6tail/tyme4tsA 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.
What's inside tyme4ts
- For full API details and comprehensive documentation, visit the official website: https://6tail.cn/tyme.html.
Install tyme4ts
masterTo use
tyme4tsin a TypeScript project, you need to install the package along withtypescriptandts-nodefor execution. Run the following commands in your terminal:npm init -y npm i typescript -D npm i ts-node -D npm i tyme4tsUse LoopTyme classes for cyclical time units
masterMany entities in the library (Animals, Stars, Elements, etc.) are implemented as
LoopTymeclasses. These represent values that cycle through a fixed set of names.Common patterns for
LoopTymesubclasses:- Creation: Use
fromIndex(index)orfromName(name)to instantiate an object. - Navigation: Use
next(n)to move forward or backward in the cycle. - Metadata: Most classes provide
getName(),getIndex(), andgetSize().
- Creation: Use
Core Interfaces: Culture and Tyme
masterThe library is built around two primary interfaces:
Culture: Represents a specific cultural time system. It must implementgetName()(returning the culture's name) andtoString()(returning a string representation).Tyme: ExtendsCultureand adds thenext(n: number)method, which returns the nextTymeinstance afternsteps, ornullif no next value exists.
All time units (Year, Month, Day, etc.) are implementations of these interfaces.
Configure EightCharProvider for BaZi calculations
masterThe
EightCharProviderinterface defines how to derive anEightCharfrom aLunarHour. You can swap the global provider onLunarHourto change the calculation logic.Available implementations:
DefaultEightCharProvider: Uses the standardhour.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();Convert Solar dates to Lunar, Rab Byung, and Hijri calendars
masterYou can create a
SolarDayinstance usingSolarDay.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());Work with RabByungYear and RabByungMonth
masterThe
RabByungYearclass handles the complex RabByung calendar system, including leap months and sixty-cycle integration.RabByungMonthhandles 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 ofRabByungMonthobjects 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 ofRabByungDayobjects for the month.next(n: number): Returns the next month afternsteps.
const year = RabByungYear.fromYear(2024); const months = year.getMonths(); const firstMonth = year.getFirstMonth(); const month = RabByungMonth.fromYm(2024, 1); const days = month.getDays();Use the TenStar class
masterThe
TenStarclass 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('正财');Use KitchenGodSteed for traditional cultural predictions
masterThe
KitchenGodSteedclass (extendingAbstractCulture) 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日得金"Use RabByungDay for specific date calculations
masterThe
RabByungDayclass represents a specific day in the RabByung calendar. It supports leap days and conversion toSolarDay.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 aSolarDay.subtract(target: RabByungDay): Returns the difference in days between two dates.next(n: number): Returns the date afterndays (via solar conversion).
const day = RabByungDay.fromYmd(2024, 1, 1); const solar = day.getSolarDay(); const diff = day.subtract(someOtherRabByungDay);Work with LunarMonth (农历月份)
masterThe
LunarMonthclass represents a month in the lunar calendar.Methods:
static fromYm(year: number | string, month: number | string): Create aLunarMonthinstance. A negative month value indicates a leap month.isLeap(): Returnstrueif this is a leap month.getName(): Returns the name (e.g., '正月', '闰二月').getDays(): Returns an array ofLunarDayobjects for this month.getFirstDay(): Returns the firstLunarDayof the month.getWeeks(start: number): Returns an array ofLunarWeekobjects starting from a specific week index.getSixtyCycle(): Returns theSixtyCyclefor this month.getFetus(): Returns theFetusMonth(if not a leap month).getSeason(): Returns theLunarSeasonthis month belongs to.
const month = LunarMonth.fromYm(2024, 1); // 正月 const days = month.getDays();Build custom Events using EventBuilder
masterUse the
EventBuilderto programmatically createEventobjects 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