package main
import (
"fmt"
"time"
)
// 天干数组
var heavenlyStems = []string{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"}
// 地支数组
var earthlyBranches = []string{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}
// 五虎遁,用于根据年干确定月干
var fiveTigerEscape = [][]int{
{2, 3, 4, 5, 6, 7, 8, 9, 0, 1}, // 甲己年
{4, 5, 6, 7, 8, 9, 0, 1, 2, 3}, // 乙庚年
{6, 7, 8, 9, 0, 1, 2, 3, 4, 5}, // 丙辛年
{8, 9, 0, 1, 2, 3, 4, 5, 6, 7}, // 丁壬年
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, // 戊癸年
}
// 五鼠遁,用于根据日干确定时干
var fiveRatEscape = [][]int{
{4, 5, 6, 7, 8, 9, 0, 1, 2, 3}, // 甲己日
{6, 7, 8, 9, 0, 1, 2, 3, 4, 5}, // 乙庚日
{8, 9, 0, 1, 2, 3, 4, 5, 6, 7}, // 丙辛日
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, // 丁壬日
{2, 3, 4, 5, 6, 7, 8, 9, 0, 1}, // 戊癸日
}
// 公历年份转干支纪年
func yearToGanzhi(year int) string {
// 公元 4 年是甲子年
offset := year - 4
ganIndex := offset % 10
zhiIndex := offset % 12
return heavenlyStems[ganIndex] + earthlyBranches[zhiIndex]
}
// 公历月份转干支纪月
func monthToGanzhi(year, month int) string {
// 年干对应的五虎遁口诀
yearGanIndex := (year - 4) % 10
startGanIndex := fiveTigerEscape[yearGanIndex/5][yearGanIndex%5]
monthZhiIndex := (month + 1) % 12
monthGanIndex := (startGanIndex + month - 1) % 10
return heavenlyStems[monthGanIndex] + earthlyBranches[monthZhiIndex]
}
// 公历日期转干支纪日
func dayToGanzhi(year, month, day int) string {
// 1900 年 1 月 31 日是甲子日(这里为了更准确采用1949年10月1日为甲子日)
startDate := time.Date(1949, 10, 1, 0, 0, 0, 0, time.UTC)
currentDate := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
days := int(currentDate.Sub(startDate).Hours() / 24)
ganIndex := days % 10
zhiIndex := days % 12
return heavenlyStems[ganIndex] + earthlyBranches[zhiIndex]
}
// 公历小时转干支纪时
func hourToGanzhi(dayGan string, hour int) string {
// 日干对应的五鼠遁口诀
dayGanIndex := 0
for i, v := range heavenlyStems {
if v == dayGan {
dayGanIndex = i
break
}
}
startGanIndex := fiveRatEscape[dayGanIndex/5][dayGanIndex%5]
hourZhiIndex := (hour + 1) / 2 % 12
hourGanIndex := (startGanIndex + (hour+1)/2 - 1) % 10
return heavenlyStems[hourGanIndex] + earthlyBranches[hourZhiIndex]
}
func main() {
// 获取当前时间
now := time.Now()
year := now.Year()
month := int(now.Month())
day := now.Day()
hour := now.Hour()
// 计算干支
ganzhiYear := yearToGanzhi(year)
ganzhiMonth := monthToGanzhi(year, month)
ganzhiDay := dayToGanzhi(year, month, day)
dayGan := string(ganzhiDay[0])
ganzhiHour := hourToGanzhi(dayGan, hour)
fmt.Printf("公历 %d 年 %d 月 %d 日 %d 时,对应的天干地支表示为:%s 年 %s 月 %s 日 %s 时\n",
year, month, day, hour, ganzhiYear, ganzhiMonth, ganzhiDay, ganzhiHour)
}
转化为c语言