<18> go time

本文介绍使用Go语言进行时间处理的方法,包括获取当前时间、构造指定日期时间、获取时间的各个部分(如年月日等)、比较时间、计算时间间隔及格式化时间。

import (
    "fmt"
    "time"
)

func main() {
    p := fmt.Println
    // 2015-11-30 19:18:48.264366857 +0800 CST
    now := time.Now()
    p(now)

    // 返回time.Time 
    // 2009-11-17 20:34:58.651387237 +0000 UTC
    then := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
    p(then)

    // 2009
    p(then.Year())
    // November
    p(then.Month())
    // 17
    p(then.Day())
    // 20
    p(then.Hour())
    // 34
    p(then.Minute())
    // 58
    p(then.Second())
    // 651387237
    p(then.Nanosecond())
    // UTC
    p(then.Location())
    // Tuesday
    p(then.Weekday())

    // true
    p(then.Before(now))
    // false
    p(then.After(now))
    // false
    p(then.Equal(now))

    // 52886h48m22.514729668s
    diff := now.Sub(then)
    p(diff)

    p(diff.Hours())
    p(diff.Minutes())
    p(diff.Seconds())
    p(diff.Nanoseconds())

    // 2015-11-30 11:23:21.166116905 +0000 UTC
    p(then.Add(diff))
    // 2003-11-06 05:46:36.136657569 +0000 UTC
    p(then.Add(-diff))

    now := time.Now()
    secs := now.Unix()
    nanos := now.UnixNano()
    fmt.Println(now)

    p(t.Format("3:04PM"))
    p(t.Format("Mon Jan _2 15:04:05 2006"))
    p(t.Format("2006-01-02T15:04:05.999999-07:00"))
    p(t.Format("2006-01-02 15:04:05"))
    form := "3 04 PM"
    t2, _ := time.Parse(form, "8 41 PM")
    p(t2)

    fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
        t.Year(), t.Month(), t.Day(),
        t.Hour(), t.Minute(), t.Second())

}
10月 28 10:00:22 localhost.localdomain systemd[1]: [/usr/lib/systemd/system/alertmanager.service:1] Assignment outside of section. Ignoring. 10月 28 10:00:22 localhost.localdomain systemd[1]: [/usr/lib/systemd/system/alertmanager.service:5] Unknown lvalue 'wants' in section 'Unit' 10月 28 10:00:22 localhost.localdomain systemd[1]: Started Alert Manager. 10月 28 10:00:22 localhost.localdomain alertmanager[87820]: time=2025-10-28T02:00:22.270Z level=INFO source=main.go:191 msg="Starting Alertmanager" version="(version=0.28.0, branch=HEAD, revision=4ce04fb010bd626fca35928dcfe82f6f2da52ced)" 10月 28 10:00:22 localhost.localdomain alertmanager[87820]: time=2025-10-28T02:00:22.270Z level=INFO source=main.go:192 msg="Build context" build_context="(go=go1.23.4, platform=linux/amd64, user=root@40be7f318ba7, date=20250115-14:22:34, tags=netgo)" 10月 28 10:00:22 localhost.localdomain alertmanager[87820]: time=2025-10-28T02:00:22.273Z level=INFO source=cluster.go:185 msg="setting advertise address explicitly" component=cluster addr=10.213.70.7 port=9094 10月 28 10:00:22 localhost.localdomain alertmanager[87820]: time=2025-10-28T02:00:22.276Z level=INFO source=cluster.go:674 msg="Waiting for gossip to settle..." component=cluster interval=2s 10月 28 10:00:22 localhost.localdomain alertmanager[87820]: time=2025-10-28T02:00:22.310Z level=INFO source=coordinator.go:112 msg="Loading configuration file" component=configuration file=/prometheus/alertmanager/alertmanager.yml 10月 28 10:00:22 localhost.localdomain alertmanager[87820]: time=2025-10-28T02:00:22.311Z level=INFO source=coordinator.go:125 msg="Completed loading of configuration file" component=configuration file=/prometheus/alertmanager/alertmanager.yml 10月 28 10:00:22 localhost.localdomain alertmanager[87820]: time=2025-10-28T02:00:22.313Z level=ERROR source=coordinator.go:131 msg="one or more config change subscribers failed to apply new config" component=configuration file=/prometheus/alertmanager/alertmanager.yml err="failed to parse templates: template: email_final.tmpl:66: function \"now\" not defined" 10月 28 10:00:22 localhost.localdomain alertmanager[87820]: time=2025-10-28T02:00:22.313Z level=INFO source=cluster.go:683 msg="gossip not settled but continuing anyway" component=cluster polls=0 elapsed=37.055332ms 10月 28 10:00:22 localhost.localdomain systemd[1]: alertmanager.service: main process exited, code=exited, status=1/FAILURE 10月 28 10:00:22 localhost.localdomain systemd[1]: Unit alertmanager.service entered failed state. 10月 28 10:00:22 localhost.localdomain systemd[1]: alertmanager.service failed. 10月 28 10:00:22 localhost.localdomain systemd[1]: alertmanager.service holdoff time over, scheduling restart. 10月 28 10:00:22 localhost.localdomain systemd[1]: Stopped Alert Manager. 10月 28 10:00:22 localhost.localdomain systemd[1]: Started Alert Manager. 10月 28 10:00:22 localhost.localdomain alertmanager[87840]: time=2025-10-28T02:00:22.622Z level=INFO source=main.go:191 msg="Starting Alertmanager" version="(version=0.28.0, branch=HEAD, revision=4ce04fb010bd626fca35928dcfe82f6f2da52ced)" 10月 28 10:00:22 localhost.localdomain alertmanager[87840]: time=2025-10-28T02:00:22.622Z level=INFO source=main.go:192 msg="Build context" build_context="(go=go1.23.4, platform=linux/amd64, user=root@40be7f318ba7, date=20250115-14:22:34, tags=netgo)" 10月 28 10:00:22 localhost.localdomain alertmanager[87840]: time=2025-10-28T02:00:22.624Z level=INFO source=cluster.go:185 msg="setting advertise address explicitly" component=cluster addr=10.213.70.7 port=9094 10月 28 10:00:22 localhost.localdomain alertmanager[87840]: time=2025-10-28T02:00:22.627Z level=INFO source=cluster.go:674 msg="Waiting for gossip to settle..." component=cluster interval=2s 10月 28 10:00:22 localhost.localdomain alertmanager[87840]: time=2025-10-28T02:00:22.659Z level=INFO source=coordinator.go:112 msg="Loading configuration file" component=configuration file=/prometheus/alertmanager/alertmanager.yml 10月 28 10:00:22 localhost.localdomain alertmanager[87840]: time=2025-10-28T02:00:22.659Z level=INFO source=coordinator.go:125 msg="Completed loading of configuration file" component=configuration file=/prometheus/alertmanager/alertmanager.yml 10月 28 10:00:22 localhost.localdomain alertmanager[87840]: time=2025-10-28T02:00:22.663Z level=ERROR source=coordinator.go:131 msg="one or more config change subscribers failed to apply new config" component=configuration file=/prometheus/alertmanager/alertmanager.yml err="failed to parse templates: template: email_final.tmpl:66: function \"now\" not defined" 10月 28 10:00:22 localhost.localdomain alertmanager[87840]: time=2025-10-28T02:00:22.663Z level=INFO source=cluster.go:683 msg="gossip not settled but continuing anyway" component=cluster polls=0 elapsed=36.323876ms 10月 28 10:00:22 localhost.localdomain systemd[1]: alertmanager.service: main process exited, code=exited, status=1/FAILURE 10月 28 10:00:22 localhost.localdomain systemd[1]: Unit alertmanager.service entered failed state. 10月 28 10:00:22 localhost.localdomain systemd[1]: alertmanager.service failed. 10月 28 10:00:22 localhost.localdomain systemd[1]: alertmanager.service holdoff time over, scheduling restart. 10月 28 10:00:22 localhost.localdomain systemd[1]: Stopped Alert Manager. 10月 28 10:00:22 localhost.localdomain systemd[1]: Started Alert Manager. 10月 28 10:00:22 localhost.localdomain alertmanager[87856]: time=2025-10-28T02:00:22.871Z level=INFO source=main.go:191 msg="Starting Alertmanager" version="(version=0.28.0, branch=HEAD, revision=4ce04fb010bd626fca35928dcfe82f6f2da52ced)" 10月 28 10:00:22 localhost.localdomain alertmanager[87856]: time=2025-10-28T02:00:22.871Z level=INFO source=main.go:192 msg="Build context" build_context="(go=go1.23.4, platform=linux/amd64, user=root@40be7f318ba7, date=20250115-14:22:34, tags=netgo)" 10月 28 10:00:22 localhost.localdomain alertmanager[87856]: time=2025-10-28T02:00:22.874Z level=INFO source=cluster.go:185 msg="setting advertise address explicitly" component=cluster addr=10.213.70.7 port=9094 10月 28 10:00:22 localhost.localdomain alertmanager[87856]: time=2025-10-28T02:00:22.876Z level=INFO source=cluster.go:674 msg="Waiting for gossip to settle..." component=cluster interval=2s 10月 28 10:00:22 localhost.localdomain alertmanager[87856]: time=2025-10-28T02:00:22.907Z level=INFO source=coordinator.go:112 msg="Loading configuration file" component=configuration file=/prometheus/alertmanager/alertmanager.yml 10月 28 10:00:22 localhost.localdomain alertmanager[87856]: time=2025-10-28T02:00:22.908Z level=INFO source=coordinator.go:125 msg="Completed loading of configuration file" component=configuration file=/prometheus/alertmanager/alertmanager.yml 10月 28 10:00:22 localhost.localdomain alertmanager[87856]: time=2025-10-28T02:00:22.911Z level=ERROR source=coordinator.go:131 msg="one or more config change subscribers failed to apply new config" component=configuration file=/prometheus/alertmanager/alertmanager.yml err="failed to parse templates: template: email_final.tmpl:66: function \"now\" not defined" 10月 28 10:00:22 localhost.localdomain alertmanager[87856]: time=2025-10-28T02:00:22.911Z level=INFO source=cluster.go:683 msg="gossip not settled but continuing anyway" component=cluster polls=0 elapsed=34.577104ms 10月 28 10:00:22 localhost.localdomain systemd[1]: alertmanager.service: main process exited, code=exited, status=1/FAILURE 10月 28 10:00:22 localhost.localdomain systemd[1]: Unit alertmanager.service entered failed state. 10月 28 10:00:22 localhost.localdomain systemd[1]: alertmanager.service failed. 10月 28 10:00:23 localhost.localdomain systemd[1]: alertmanager.service holdoff time over, scheduling restart. 10月 28 10:00:23 localhost.localdomain systemd[1]: Stopped Alert Manager. 10月 28 10:00:23 localhost.localdomain systemd[1]: Started Alert Manager. 10月 28 10:00:23 localhost.localdomain alertmanager[87872]: time=2025-10-28T02:00:23.122Z level=INFO source=main.go:191 msg="Starting Alertmanager" version="(version=0.28.0, branch=HEAD, revision=4ce04fb010bd626fca35928dcfe82f6f2da52ced)" 10月 28 10:00:23 localhost.localdomain alertmanager[87872]: time=2025-10-28T02:00:23.122Z level=INFO source=main.go:192 msg="Build context" build_context="(go=go1.23.4, platform=linux/amd64, user=root@40be7f318ba7, date=20250115-14:22:34, tags=netgo)" 10月 28 10:00:23 localhost.localdomain alertmanager[87872]: time=2025-10-28T02:00:23.125Z level=INFO source=cluster.go:185 msg="setting advertise address explicitly" component=cluster addr=10.213.70.7 port=9094 10月 28 10:00:23 localhost.localdomain alertmanager[87872]: time=2025-10-28T02:00:23.127Z level=INFO source=cluster.go:674 msg="Waiting for gossip to settle..." component=cluster interval=2s 10月 28 10:00:23 localhost.localdomain alertmanager[87872]: time=2025-10-28T02:00:23.161Z level=INFO source=coordinator.go:112 msg="Loading configuration file" component=configuration file=/prometheus/alertmanager/alertmanager.yml 10月 28 10:00:23 localhost.localdomain alertmanager[87872]: time=2025-10-28T02:00:23.161Z level=INFO source=coordinator.go:125 msg="Completed loading of configuration file" component=configuration file=/prometheus/alertmanager/alertmanager.yml 10月 28 10:00:23 localhost.localdomain alertmanager[87872]: time=2025-10-28T02:00:23.163Z level=ERROR source=coordinator.go:131 msg="one or more config change subscribers failed to apply new config" component=configuration file=/prometheus/alertmanager/alertmanager.yml err="failed to parse templates: template: email_final.tmpl:66: function \"now\" not defined" 10月 28 10:00:23 localhost.localdomain alertmanager[87872]: time=2025-10-28T02:00:23.163Z level=INFO source=cluster.go:683 msg="gossip not settled but continuing anyway" component=cluster polls=0 elapsed=35.955593ms 10月 28 10:00:23 localhost.localdomain systemd[1]: alertmanager.service: main process exited, code=exited, status=1/FAILURE 10月 28 10:00:23 localhost.localdomain systemd[1]: Unit alertmanager.service entered failed state. 10月 28 10:00:23 localhost.localdomain systemd[1]: alertmanager.service failed. 10月 28 10:00:23 localhost.localdomain systemd[1]: alertmanager.service holdoff time over, scheduling restart. 10月 28 10:00:23 localhost.localdomain systemd[1]: Stopped Alert Manager. 10月 28 10:00:23 localhost.localdomain systemd[1]: Started Alert Manager. 10月 28 10:00:23 localhost.localdomain alertmanager[87888]: time=2025-10-28T02:00:23.371Z level=INFO source=main.go:191 msg="Starting Alertmanager" version="(version=0.28.0, branch=HEAD, revision=4ce04fb010bd626fca35928dcfe82f6f2da52ced)" 10月 28 10:00:23 localhost.localdomain alertmanager[87888]: time=2025-10-28T02:00:23.371Z level=INFO source=main.go:192 msg="Build context" build_context="(go=go1.23.4, platform=linux/amd64, user=root@40be7f318ba7, date=20250115-14:22:34, tags=netgo)" 10月 28 10:00:23 localhost.localdomain alertmanager[87888]: time=2025-10-28T02:00:23.374Z level=INFO source=cluster.go:185 msg="setting advertise address explicitly" component=cluster addr=10.213.70.7 port=9094 10月 28 10:00:23 localhost.localdomain alertmanager[87888]: time=2025-10-28T02:00:23.376Z level=INFO source=cluster.go:674 msg="Waiting for gossip to settle..." component=cluster interval=2s 10月 28 10:00:23 localhost.localdomain alertmanager[87888]: time=2025-10-28T02:00:23.408Z level=INFO source=coordinator.go:112 msg="Loading configuration file" component=configuration file=/prometheus/alertmanager/alertmanager.yml 10月 28 10:00:23 localhost.localdomain alertmanager[87888]: time=2025-10-28T02:00:23.409Z level=INFO source=coordinator.go:125 msg="Completed loading of configuration file" component=configuration file=/prometheus/alertmanager/alertmanager.yml 10月 28 10:00:23 localhost.localdomain alertmanager[87888]: time=2025-10-28T02:00:23.411Z level=ERROR source=coordinator.go:131 msg="one or more config change subscribers failed to apply new config" component=configuration file=/prometheus/alertmanager/alertmanager.yml err="failed to parse templates: template: email_final.tmpl:66: function \"now\" not defined" 10月 28 10:00:23 localhost.localdomain alertmanager[87888]: time=2025-10-28T02:00:23.411Z level=INFO source=cluster.go:683 msg="gossip not settled but continuing anyway" component=cluster polls=0 elapsed=34.366194ms 10月 28 10:00:23 localhost.localdomain systemd[1]: alertmanager.service: main process exited, code=exited, status=1/FAILURE 10月 28 10:00:23 localhost.localdomain systemd[1]: Unit alertmanager.service entered failed state. 10月 28 10:00:23 localhost.localdomain systemd[1]: alertmanager.service failed. 10月 28 10:00:23 localhost.localdomain systemd[1]: alertmanager.service holdoff time over, scheduling restart. 10月 28 10:00:23 localhost.localdomain systemd[1]: Stopped Alert Manager. 10月 28 10:00:23 localhost.localdomain systemd[1]: start request repeated too quickly for alertmanager.service 10月 28 10:00:23 localhost.localdomain systemd[1]: Failed to start Alert Manager. 10月 28 10:00:23 localhost.localdomain systemd[1]: Unit alertmanager.service entered failed state. 10月 28 10:00:23 localhost.localdomain systemd[1]: alertmanager.service failed. [root@localhost ~]# cat /prometheus/alertmanager/templates/email_final.tmpl {{ define "custom.subject" }} {{- if eq .Status "firing" -}} {{- if eq .CommonLabels.alertname "WeeklyUnresolvedAlerts" -}} 📋 长期未处理告警汇总 - {{ .GroupLabels.instance }} {{- else -}} 🚨 监控报警 - {{ .GroupLabels.instance }} - {{ .CommonLabels.alertname }} {{- end -}} {{- else -}} ✅ 恢复通知 - {{ .GroupLabels.instance }} - {{ .CommonLabels.alertname }} {{- end -}} {{ end }} {{ define "custom.html" }} <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>{{ template "custom.subject" . }}</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } .alert-box { border: 2px solid; padding: 15px; margin: 10px 0; border-radius: 5px; } .firing { border-color: #ff4d4d; background-color: #fff5f5; } .resolved { border-color: #52c41a; background-color: #f6ffed; } .unresolved { border-color: #ffc107; background-color: #fff8e1; } table { width: 100%; border-collapse: collapse; margin: 10px 0; } th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid #ddd; } th { background-color: #f5f5f5; font-weight: bold; } .promql { background-color: #f0f0f0; padding: 8px; border-radius: 3px; font-family: monospace; word-break: break-all; } </style> </head> <body> {{ if eq .CommonLabels.alertname "WeeklyUnresolvedAlerts" }} <div class="alert-box unresolved"> <h2>📋 长期未处理告警汇总</h2> <table> <tr><th>汇总时间:</th><td>{{ (index .Alerts 0).StartsAt.Format "2006-01-02 15:04:05" }}</td></tr> <tr><th>告警数量:</th><td>{{ len .Alerts }}</td></tr> <tr><th>汇总描述:</th><td>以下告警在过去一周内未处理,请及时处理</td></tr> </table> <h3>未处理告警详情</h3> <table> <tr> <th>告警类型</th> <th>告警级别</th> <th>故障主机</th> <th>告警主题</th> <th>故障时间</th> <th>持续时间</th> <th>查询地址</th> </tr> {{ range .Alerts }} <tr> <td>{{ .Labels.alertname }}</td> <td> {{- if eq .Labels.severity "critical" }}<strong style="color: red;">严重</strong> {{- else if eq .Labels.severity "warning" }}<strong style="color: orange;">警告</strong> {{- else }}{{ .Labels.severity }}{{ end -}} </td> <td>{{ .Labels.instance }}</td> <td>{{ .Annotations.summary }}</td> <td>{{ .StartsAt.Format "2006-01-02 15:04:05" }}</td> <td>{{ now.Sub .StartsAt }}</td> <td><a href="{{ .GeneratorURL }}">查看详情</a></td> </tr> {{ end }} </table> <p><strong>历史数据:</strong><a href="http://10.213.70.8:3000">http://10.213.70.8:3000</a></p> <p>请及时处理以上长期未处理的告警</p> </div> {{ else if eq .Status "firing" }} <div class="alert-box firing"> <h2>监控报警(🚨 故障告警通知)</h2> <table> <tr><th>告警类型:</th><td>{{ .CommonLabels.alertname }}</td></tr> <tr><th>告警级别:</th><td><strong style="color: red;"> {{- if eq .CommonLabels.severity "critical" }}严重 {{- else if eq .CommonLabels.severity "warning" }}警告 {{- else }}{{ .CommonLabels.severity }}{{ end -}} </strong></td></tr> <tr><th>告警状态:</th><td><strong style="color: red;">触发中</strong></td></tr> <tr><th>故障主机:</th><td>{{ .GroupLabels.instance }}</td></tr> <tr><th>服务名称:</th><td>{{ .CommonLabels.job }}</td></tr> <tr><th>告警主题:</th><td>{{ (index .Alerts 0).Annotations.summary }}</td></tr> <tr><th>告警详情:</th><td>{{ (index .Alerts 0).Annotations.description }}</td></tr> <tr><th>故障时间:</th><td>{{ (index .Alerts 0).StartsAt.Format "2006-01-02 15:04:05" }}</td></tr> </table> <p><strong>查询地址:</strong></p> <div class="promql"> <a href="{{ (index .Alerts 0).GeneratorURL }}">{{ (index .Alerts 0).GeneratorURL }}</a> </div> <p><strong>历史数据:</strong><a href="http://10.213.70.8:3000">http://10.213.70.8:3000</a></p> </div> {{ else }} <div class="alert-box resolved"> <h2>✅ 恢复通知 - 监控报警</h2> <table> <tr><th>告警类型:</th><td>{{ .CommonLabels.alertname }}</td></tr> <tr><th>告警级别:</th><td> {{- if eq .CommonLabels.severity "critical" }}严重 {{- else if eq .CommonLabels.severity "warning" }}警告 {{- else }}{{ .CommonLabels.severity }}{{ end -}} </td></tr> <tr><th>告警状态:</th><td><strong style="color: green;">已恢复</strong></td></tr> <tr><th>故障主机:</th><td>{{ .GroupLabels.instance }}</td></tr> <tr><th>服务名称:</th><td>{{ .CommonLabels.job }}</td></tr> <tr><th>告警主题:</th><td>{{ (index .Alerts 0).Annotations.summary }}</td></tr> <tr><th>告警详情:</th><td>{{ (index .Alerts 0).Annotations.description }}</td></tr> <tr><th>故障时间:</th><td>{{ (index .Alerts 0).StartsAt.Format "2006-01-02 15:04:05" }}</td></tr> <tr><th>恢复时间:</th><td>{{ (index .Alerts 0).EndsAt.Format "2006-01-02 15:04:05" }}</td></tr> <tr><th>持续时间:</th><td>{{ (index .Alerts 0).EndsAt.Sub (index .Alerts 0).StartsAt }}</td></tr> </table> <p><strong>查询地址:</strong></p> <div class="promql"> <a href="{{ (index .Alerts 0).GeneratorURL }}">{{ (index .Alerts 0).GeneratorURL }}</a> </div> <p><strong>历史数据:</strong><a href="http://10.213.70.8:3000">http://10.213.70.8:3000</a></p> <p>问题已自动恢复,无需人工干预</p> </div> {{ end }} </body> </html> {{ end }} 修复
最新发布
10-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值