其他分享
首页 > 其他分享> > golang判断用户是否成年

golang判断用户是否成年

作者:互联网

func IsAdualt(cardid string) bool {

   const Layout = "20060102"
   loc, _ := time.LoadLocation("Asia/Shanghai")
   reg, _ := regexp.Compile("^(\\d{6})(\\d{8})(.*)")

   submatch := reg.FindStringSubmatch(cardid)
   if len(submatch) < 2 {

      fmt.Println("IsAdualt 数据不合法", cardid, submatch)
      return false
   }
   birth := submatch[2]
   btime, err := time.ParseInLocation(Layout, birth, loc)

   if err != nil {
      fmt.Println("IsAdualt 解析失败", err, cardid, submatch)

      return false
   }
   //18年前
   start_time := time.Now().AddDate(-18, 0, 0)

   if start_time.Unix() > btime.Unix() {
      return true
   }

   return false
}

标签:return,err,成年,submatch,time,用户,golang,cardid,false
来源: https://blog.csdn.net/qq_28740133/article/details/120234093