当前位置:网站首页>Code example of golang date time package: get age, zodiac and constellation based on birthday

Code example of golang date time package: get age, zodiac and constellation based on birthday

2022-06-23 16:23:00 Learn programming notes

package main

import (
	"fmt"
	"time"
)

func GetTimeFromStrDate(date string) (year, month, day int) {
    
	const shortForm = "2006-01-02"
	d, err := time.Parse(shortForm, date)
	if err != nil {
    
		fmt.Println(" Birth date parsing error !")
		return 0, 0, 0
	}
	year = d.Year()
	month = int(d.Month())
	day = d.Day()
	return
}

func GetZodiac(year int) (zodiac string) {
    
	if year <= 0 {
    
		zodiac = "-1"
	}
	start := 1901
	x := (start - year) % 12
	if x == 1 || x == -11 {
    
		zodiac = " rat "
	}
	if x == 0 {
    
		zodiac = " cattle "
	}
	if x == 11 || x == -1 {
    
		zodiac = " The tiger "
	}
	if x == 10 || x == -2 {
    
		zodiac = " rabbit "
	}
	if x == 9 || x == -3 {
    
		zodiac = " dragon "
	}
	if x == 8 || x == -4 {
    
		zodiac = " The snake "
	}
	if x == 7 || x == -5 {
    
		zodiac = " Horse "
	}
	if x == 6 || x == -6 {
    
		zodiac = " sheep "
	}
	if x == 5 || x == -7 {
    
		zodiac = " Monkey "
	}
	if x == 4 || x == -8 {
    
		zodiac = " chicken "
	}
	if x == 3 || x == -9 {
    
		zodiac = " Dog "
	}
	if x == 2 || x == -10 {
    
		zodiac = " The pig "
	}
	return
}

func GetAge(year int) (age int) {
    
	if year <= 0 {
    
		age = -1
	}
	nowyear := time.Now().Year()
	age = nowyear - year
	return
}

func GetConstellation(month, day int) (star string) {
    
	if month <= 0 || month >= 13 {
    
		star = "-1"
	}
	if day <= 0 || day >= 32 {
    
		star = "-1"
	}
	if (month == 1 && day >= 20) || (month == 2 && day <= 18) {
    
		star = " Aquarius: "
	}
	if (month == 2 && day >= 19) || (month == 3 && day <= 20) {
    
		star = " Pisces "
	}
	if (month == 3 && day >= 21) || (month == 4 && day <= 19) {
    
		star = " Aries "
	}
	if (month == 4 && day >= 20) || (month == 5 && day <= 20) {
    
		star = " Taurus "
	}
	if (month == 5 && day >= 21) || (month == 6 && day <= 21) {
    
		star = " Gemini "
	}
	if (month == 6 && day >= 22) || (month == 7 && day <= 22) {
    
		star = " Cancer "
	}
	if (month == 7 && day >= 23) || (month == 8 && day <= 22) {
    
		star = " Leo "
	}
	if (month == 8 && day >= 23) || (month == 9 && day <= 22) {
    
		star = " Virgo "
	}
	if (month == 9 && day >= 23) || (month == 10 && day <= 22) {
    
		star = " libra "
	}
	if (month == 10 && day >= 23) || (month == 11 && day <= 21) {
    
		star = " scorpio "
	}
	if (month == 11 && day >= 22) || (month == 12 && day <= 21) {
    
		star = " Sagittarius "
	}
	if (month == 12 && day >= 22) || (month == 1 && day <= 19) {
    
		star = " Scorpio "
	}

	return star
}

func main() {
    
	y, m, d := GetTimeFromStrDate("1986-06-18")
	fmt.Println(GetAge(y)) //  For age 
	fmt.Println(GetConstellation(m, d)) //  The constellation 
	fmt.Println(GetZodiac(y)) //  the Chinese zodiac 
}
原网站

版权声明
本文为[Learn programming notes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231538135162.html