Skip to content

Commit

Permalink
implemented: life cycle return
Browse files Browse the repository at this point in the history
  • Loading branch information
tommitoan committed Aug 8, 2024
1 parent 480b49b commit 9b0844e
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 36 deletions.
3 changes: 2 additions & 1 deletion get_bazi_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bazica
import (
"github.com/tommitoan/bazica/internal/fourpillars"
"github.com/tommitoan/bazica/internal/luckpillars"
"github.com/tommitoan/bazica/internal/ultis"
"github.com/tommitoan/bazica/model"
"time"
)
Expand All @@ -19,7 +20,7 @@ func GetBaziChart(dateTime time.Time, loc *time.Location, gender int, prefixPath
if err != nil {
return nil, err
}
baziChart.FourPillar = fourPillar
baziChart.FourPillar = ultis.GetLifeCycleFromFourPillar(fourPillar)

lucksPillar, err := luckpillars.GetLuckPillars(fourPillar, gender, passed, remaining, dateTime, path)
if err != nil {
Expand Down
90 changes: 90 additions & 0 deletions get_bazi_chart_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package bazica

import (
"fmt"
"github.com/tommitoan/bazica/model"
"testing"
"time"
)

func TestGetBaziChart(t *testing.T) {
type args struct {
year, month, day, hour, minute int
loc string
gender int
prefixPath []string
}
tests := []struct {
name string
args args
want *model.BaziChart
wantErr bool
}{
{"123", args{
year: 1995, month: 6, day: 8, hour: 22, minute: 05,
loc: "Asia/Ho_Chi_Minh",
gender: 1,
prefixPath: nil,
}, &model.BaziChart{
PersonalInfo: nil,
FourPillar: &model.FourPillars{
YearPillar: &model.YearPillar{
HeavenlyStem: model.HeavenlyStem{Name: model.YinWoodName, Value: model.YinWoodValue},
EarthlyBranch: model.EarthlyBranch{Name: model.Pig, Value: model.PigValue},
Year: 1995,
GanZhi: model.GanZhi{
Name: "Fire on the Mountain",
ElementName: "Fire",
ElementValue: 2,
},
},
MonthPillar: &model.MonthPillar{
HeavenlyStem: model.HeavenlyStem{Name: model.YangWaterName, Value: model.YangWaterValue},
EarthlyBranch: model.EarthlyBranch{Name: model.Horse, Value: model.HorseValue},
Month: 6,
GanZhi: model.GanZhi{
Name: "Willow Tree Wood",
ElementName: "Wood",
ElementValue: 3,
},
},
DayPillar: &model.DayPillar{
HeavenlyStem: model.HeavenlyStem{Name: model.YangMetalName, Value: model.YangMetalValue},
EarthlyBranch: model.EarthlyBranch{Name: model.Horse, Value: model.HorseValue},
Day: 8,
GanZhi: model.GanZhi{
Name: "Earth on the Roadside",
ElementName: "Earth",
ElementValue: 5,
},
},
HourPillar: &model.HourPillar{
HeavenlyStem: model.HeavenlyStem{Name: model.YinFireName, Value: model.YinFireValue},
EarthlyBranch: model.EarthlyBranch{Name: model.Pig, Value: model.PigValue},
Hour: model.TimeOfDay{Hour: 22, Minute: 05},
GanZhi: model.GanZhi{
Name: "Earth on the Roof",
ElementName: "Earth",
ElementValue: 5,
},
},
},
LuckPillars: nil,
}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
location, _ := time.LoadLocation(tt.args.loc)
date := time.Date(tt.args.year, time.Month(tt.args.month), tt.args.day, tt.args.hour, tt.args.minute, 0, 0, location)
got, err := GetBaziChart(date, location, tt.args.gender, tt.args.prefixPath...)
if (err != nil) != tt.wantErr {
t.Errorf("GetBaziChart() error = %v, wantErr %v", err, tt.wantErr)
return
}
fmt.Println(got)
//if !reflect.DeepEqual(got, tt.want) {
// t.Errorf("GetBaziChart() got = %v, want %v", got, tt.want)
//}
})
}
}
2 changes: 1 addition & 1 deletion internal/fourpillars/get_pillar_hour.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GetHourPillar(dayPillar *model.DayPillar, dateTime time.Time) (*model.HourP
hourPillar.EarthlyBranch = branch

// Get heavenly stem
valueOfRatHour := ultis.GetStemRuleByFireRats(dayPillar.HeavenlyStem.Value)
valueOfRatHour := ultis.GetStemRuleByFiveRats(dayPillar.HeavenlyStem.Value)
var stem int
diff := hourPillar.EarthlyBranch.Value - 11
if diff < 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/fourpillars/get_pillar_month.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func GetMonthPillar(path string, yearPillar *model.YearPillar, dateTime time.Tim
monthPillar.EarthlyBranch = earthBranch

// Get heavenly stem
valueOfFirstMonth := ultis.GetStemRuleByFireTigers(yearPillar.HeavenlyStem.Value)
valueOfFirstMonth := ultis.GetStemRuleByFiveTigers(yearPillar.HeavenlyStem.Value)
valueToCal := (valueOfFirstMonth - 1) + monthPillar.EarthlyBranch.Value
if valueToCal > 10 {
valueToCal = valueToCal - 10
Expand Down
23 changes: 23 additions & 0 deletions internal/ultis/get_life_cycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ultis

import "github.com/tommitoan/bazica/model"

func GetLifeCycleFromFourPillar(pillars *model.FourPillars) *model.FourPillars {
dayValue := pillars.DayPillar.HeavenlyStem.Value
lifeCycleValue := GetLifeCycleRule(dayValue)

pillars.YearPillar.LifeCycle = GetLifeCycleByRuleAndBranch(pillars.YearPillar.EarthlyBranch.Value, lifeCycleValue)
pillars.MonthPillar.LifeCycle = GetLifeCycleByRuleAndBranch(pillars.MonthPillar.EarthlyBranch.Value, lifeCycleValue)
pillars.DayPillar.LifeCycle = GetLifeCycleByRuleAndBranch(pillars.DayPillar.EarthlyBranch.Value, lifeCycleValue)
pillars.HourPillar.LifeCycle = GetLifeCycleByRuleAndBranch(pillars.HourPillar.EarthlyBranch.Value, lifeCycleValue)

return pillars
}

func GetLifeCycleByRuleAndBranch(branchValue int, lifeCycleValue int) string {
valueToCal := (lifeCycleValue - 1) + branchValue
if valueToCal > 12 {
valueToCal = valueToCal - 12
}
return ConvertValueToLifeCycle(valueToCal)
}
65 changes: 61 additions & 4 deletions internal/ultis/ultis.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,41 @@ func ConvertTermToBranch(termName string) model.EarthlyBranch {
return earthlyBranch
}

// GetStemRuleByFireTigers use Five-tigers Seek to get the Stem of Tiger month base on Year's Stem
func ConvertValueToLifeCycle(value int) string {
var lifeCycle string
switch value {
case 1:
lifeCycle = model.LC1
case 2:
lifeCycle = model.LC2
case 3:
lifeCycle = model.LC3
case 4:
lifeCycle = model.LC4
case 5:
lifeCycle = model.LC5
case 6:
lifeCycle = model.LC6
case 7:
lifeCycle = model.LC7
case 8:
lifeCycle = model.LC8
case 9:
lifeCycle = model.LC9
case 10:
lifeCycle = model.LC10
case 11:
lifeCycle = model.LC11
case 12:
lifeCycle = model.LC12
}
return lifeCycle
}

// GetStemRuleByFiveTigers use Five-tigers Seek to get the Stem of Tiger month base on Year's Stem
// https://www.chinesefortunecalendar.com/Five-Tigers-Year-Month-Table.htm
// (VN) Dùng Ngũ hổ độn (ngũ dần) để tính Can Tháng dựa trên Can Năm
func GetStemRuleByFireTigers(yearValue int) int {
func GetStemRuleByFiveTigers(yearValue int) int {
var stemValueOfFirstMonth int
switch yearValue {
case 1, 6:
Expand All @@ -151,10 +182,10 @@ func GetStemRuleByFireTigers(yearValue int) int {
return stemValueOfFirstMonth
}

// GetStemRuleByFireRats use Five-rats Seek to get the Stem of Rat hour base on Day's Stem
// GetStemRuleByFiveRats use Five-rats Seek to get the Stem of Rat hour base on Day's Stem
// https://www.chinesefortunecalendar.com/Five-Rats-Day-Hour-Table.htm
// (VN) Dùng Ngũ tí độn để tính Can Giờ dựa trên Can Ngày
func GetStemRuleByFireRats(dayValue int) int {
func GetStemRuleByFiveRats(dayValue int) int {
var stemValueOfRatHour int
switch dayValue {
case 1, 6:
Expand All @@ -170,3 +201,29 @@ func GetStemRuleByFireRats(dayValue int) int {
}
return stemValueOfRatHour
}

// GetLifeCycleRule gets the Life Stages value of the Tiger branch
// https://en.wikibooks.org/wiki/Ba_Zi/Life_Cycle
// (VN) Tính Vòng tràng sinh dựa trên Can Ngày và 4 Chi
func GetLifeCycleRule(dayValue int) int {
var lifeCycleValue int
switch dayValue {
case 1:
lifeCycleValue = 4
case 2:
lifeCycleValue = 5
case 3, 5:
lifeCycleValue = 1
case 4, 6:
lifeCycleValue = 8
case 7:
lifeCycleValue = 10
case 8:
lifeCycleValue = 11
case 9:
lifeCycleValue = 7
case 10:
lifeCycleValue = 2
}
return lifeCycleValue
}
29 changes: 0 additions & 29 deletions model/model_earthly-branches.go

This file was deleted.

16 changes: 16 additions & 0 deletions model/model_life-cycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package model

const (
LC1 = "Birth"
LC2 = "Bath"
LC3 = "Youth"
LC4 = "Thriving"
LC5 = "Prosperous"
LC6 = "Weakening"
LC7 = "Sick"
LC8 = "Death"
LC9 = "Grave"
LC10 = "Extinction"
LC11 = "Conceived"
LC12 = "Nourishing"
)
28 changes: 28 additions & 0 deletions model/model_heavenly-stems.go → model/model_pillar.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

const (
// HeavenlyStem default values
YangWoodValue = 1
YinWoodValue = 2
YangFireValue = 3
Expand All @@ -22,4 +23,31 @@ const (
YinMetalName = "Yin Metal"
YangWaterName = "Yang Water"
YinWaterName = "Yin Water"

// EarthlyBranch default values
TigerValue = 1
RabbitValue = 2
DragonValue = 3
SnakeValue = 4
HorseValue = 5
GoatValue = 6
MonkeyValue = 7
RoosterValue = 8
DogValue = 9
PigValue = 10
RatValue = 11
OxValue = 12

Tiger = "Tiger"
Rabbit = "Rabbit"
Dragon = "Dragon"
Snake = "Snake"
Horse = "Horse"
Goat = "Goat"
Monkey = "Monkey"
Rooster = "Rooster"
Dog = "Dog"
Pig = "Pig"
Rat = "Rat"
Ox = "Ox"
)

0 comments on commit 9b0844e

Please sign in to comment.