package regx import ( "errors" "strconv" "time" ) // Verify birthday func (rr *Regx) CheckBirthdayCode(birthday string) (bool, error) { year, _ := strconv.Atoi(birthday[:4]) month, _ := strconv.Atoi(birthday[4:6]) day, _ := strconv.Atoi(birthday[6:]) curYear, curMonth, curDay := time.Now().Date() //Birth date greater than current date if year < 1900 || year > curYear || month <= 0 || month > 12 || day <= 0 || day > 31 { return false, errors.New("请检查生日部分的日期是否正确") } if year == curYear { if month > int(curMonth) { return false, errors.New("当前日期月份小于您身份证上的月份") } else if month == int(curMonth) && day > curDay { return false, errors.New("当前日期天数小于您身份证上的天数") } } //出生日期在2月份 if 2 == month { if rr.IsLeapYear(year) && day > 29 { return false, errors.New("闰年2月只有29号") } else if day > 28 { return false, errors.New("非闰年2月只有28号") } } else if 4 == month || 6 == month || 9 == month || 11 == month { if day > 30 { return false, errors.New("小月只有30号") } } return true, nil } // 判断是否为闰年 func (rr *Regx) IsLeapYear(year int) bool { if year <= 0 { return false } if (year%4 == 0 && year%100 != 0) || year%400 == 0 { return true } return false } // 根据生日计算星座 func (rr *Regx) GetZodiac(month, day int) string { month = month - 1 var ( DAY_ARR = [12]int{20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22} ZODIACS = [13]string{"摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"} ) if day < DAY_ARR[month] { return ZODIACS[month] } else { return ZODIACS[month+1] } } // 根据生日计算属相 func (rr *Regx) GetChineseZodiac(year int) string { var CHINESE_ZODIACS = [12]string{"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"} if year > 1900 { return CHINESE_ZODIACS[(year-1900)%len(CHINESE_ZODIACS)] } else { return "" } }