返回顶部

收藏

[iProg]用Go语言根据出生日期获取年龄、生肖和星座信息

更多
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("出生日期解析错误!")
        return 0,0,0
    }
    year = d.Year()
    month = int(d.Month())
    day = d.Day()
    return
}

func GetZodiac(year int) (zodiac string) {
    if year <= 0 {
        return  "-1"
    }
    start := 1901
    x := (start - year) % 12
    if x == 1 || x == -11 {
        zodiac = "鼠"
    }
    if x == 0 {
        zodiac = "牛"
    }
    if x == 11 || x == -1 {
        zodiac = "虎"
    }
    if x == 10 || x == -2 {
        zodiac = "兔"
    }
    if x == 9 || x == -3 {
        zodiac = "龙"
    }
    if x == 8 || x == -4 {
        zodiac = "蛇"
    }
    if x == 7 || x == -5 {
        zodiac = "马"
    }
    if x == 6 || x == -6 {
        zodiac = "羊"
    }
    if x == 5 || x == -7 {
        zodiac = "猴"
    }
    if x == 4 || x == -8 {
        zodiac = "鸡"
    }
    if x == 3 || x == -9 {
        zodiac = "狗"
    }
    if x == 2 || x == -10 {
        zodiac = "猪"
    }
    return
}

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

func GetConstellation(month, day int) (star string) {

    if month <= 0 || month >= 13 {
        return  "-1"
    }
    if day <= 0 || day >= 32 {
        return  "-1"
    }
    if (month == 1 && day >= 20) || (month == 2 && day <= 18) {
        star = "水瓶座"
    }
    if (month == 2 && day >= 19) || (month == 3 && day <= 20) {
        star = "双鱼座"
    }
    if (month == 3 && day >= 21) || (month == 4 && day <= 19) {
        star = "白羊座"
    }
    if (month == 4 && day >= 20) || (month == 5 && day <= 20) {
        star = "金牛座"
    }
    if (month == 5 && day >= 21) || (month == 6 && day <= 21) {
        star = "双子座"
    }
    if (month == 6 && day >= 22) || (month == 7 && day <= 22) {
        star = "巨蟹座"
    }
    if (month == 7 && day >= 23) || (month == 8 && day <= 22) {
        star = "狮子座"
    }
    if (month == 8 && day >= 23) || (month == 9 && day <= 22) {
        star = "处女座"
    }
    if (month == 9 && day >= 23) || (month == 10 && day <= 22) {
        star = "天秤座"
    }
    if (month == 10 && day >= 23) || (month == 11 && day <= 21) {
        star = "天蝎座"
    }
    if (month == 11 && day >= 22) || (month == 12 && day <= 21) {
        star = "射手座"
    }
    if (month == 12 && day >= 22) || (month == 1 && day <= 19) {
        star = "魔蝎座"
    }

    return
}

func main() {
    y, m, d := GetTimeFromStrDate("1986-06-18")
    fmt.Println("年龄:",GetAge(y))
    fmt.Println("星座:",GetConstellation(m, d))
    fmt.Println("生肖:",GetZodiac(y))
}

标签:golang,golang

收藏

0人收藏

支持

1

反对

0

相关聚客文章
  1. rickey 发表 2013-08-19 16:03:13 Go语言在Windows环境下如何配置
  2. gouki 发表 2013-09-15 21:10:00 [转]Think2Go戈登营首期review
  3. 陈钢 发表 2013-12-09 06:44:36 Docker跟一般的虚拟机有什么区别?
  4. 博主 发表 2014-03-24 15:19:15 Running MongoDB Queries Concurrently With Go
  5. 博主 发表 2013-08-20 07:00:00 使用Go语言进行实时计算
  6. Andrew Gerrand 发表 2014-06-18 11:00:00 Go 1.3 is released
  7. bigwhite 发表 2014-09-26 07:09:52 Ubuntu Server 14.04安装docker
  8. 傅小黑 发表 2014-04-08 21:46:00 Fxh.Go的新计划
  9. 郭蕾 发表 2014-11-13 00:45:40 《GO并发编程实战》—— 临时对象池
  10. 博主 发表 2014-08-08 16:02:32 Go MoonMQ使用说明
  11. 博主 发表 2014-12-02 09:23:07 Go socket编程实践: UDP服务器和客户端实现
  12. bigwhite 发表 2014-12-20 09:18:36 使用Golang开发微信公众平台-接收文本消息