Skip to content

Instantly share code, notes, and snippets.

@donma
Created March 4, 2025 08:49
Show Gist options
  • Save donma/1223d1c2b2440a8d683eb5fe516b8015 to your computer and use it in GitHub Desktop.
Save donma/1223d1c2b2440a8d683eb5fe516b8015 to your computer and use it in GitHub Desktop.
static readonly string[] EarthlyBranches = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
static readonly string[] Zodiacs = { "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" };
static readonly int[] SixClashIndex = { 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5 };
public static void CalculateZodiacClash(DateTime date)
{
int earthlyIndex = GetEarthlyBranchIndex(date);
string earthlyBranch = EarthlyBranches[earthlyIndex];
int clashIndex = SixClashIndex[earthlyIndex];
string clashZodiac = Zodiacs[clashIndex];
Console.WriteLine($"日期: {date:yyyy-MM-dd}");
Console.WriteLine($"地支: {earthlyBranch}");
Console.WriteLine($"沖生肖: {clashZodiac}");
}
private static int GetEarthlyBranchIndex(DateTime date)
{
DateTime baseDate = new DateTime(1900, 1, 1);
int daysSinceBase = (date - baseDate).Days;
return (daysSinceBase + 10) % 12; // 修正偏移值
}
static void Main(string[] args)
{
DateTime date1 = new DateTime(2025, 7, 21);
CalculateZodiacClash(date1);
DateTime date2 = new DateTime(2026, 5, 11);
CalculateZodiacClash(date2);
DateTime date3 = new DateTime(2027, 2, 19);
CalculateZodiacClash(date3);
}
// Result
/*
日期: 2025-07-21
地支: 卯
沖生肖: 雞
日期: 2026-05-11
地支: 酉
沖生肖: 兔
日期: 2027-02-19
地支: 巳
沖生肖: 豬
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment