惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
Microsoft Azure Blog
Microsoft Azure Blog
雷峰网
雷峰网
Recent Announcements
Recent Announcements
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
博客园_首页
The Cloudflare Blog
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
S
SegmentFault 最新的问题
P
Proofpoint News Feed
Y
Y Combinator Blog
Jina AI
Jina AI
博客园 - 聂微东
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
F
Full Disclosure
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
T
Tailwind CSS Blog
J
Java Code Geeks
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 三生石上(FineUI控件)
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
宝玉的分享
宝玉的分享
IT之家
IT之家
Hacker News: Ask HN
Hacker News: Ask HN
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs

博客园 - dribs

PD分离 基于GPT2训练构建医疗问诊机器人 物流行业信息咨询智能问答系统 day8 golang-chan-协程-定时器-锁-等待组 day7 golang GMP golang面试题单向链表和双向链表 golang 自行实现一个base64加密 golang标准库log+第三方zerolog golang标准库序列化反序列化 json + 第三方msgpack golang 标准库 目录操作 golang-day1 进制 golang-标准库文件操作 day6 golang-标准库(随时更新) golang--day5结构体+结构体排序+错误处理+结构体和接口实现面向对象的例子 golang-day4函数 ceph warn oldest client/flush tid ceph 扩容后healt-error k8s系列---pod手动驱逐 k8s系列---Chart(下)
golang-标准库时间time
dribs · 2023-02-26 · via 博客园 - dribs

2023-02-26 18:00  dribs  阅读(78)  评论()    收藏  举报

package main
 
import (
    "fmt"
    "time"
)
 
func main() {
    t := time.Now()
    //time.Time time.Date(2023, time.February, 19, 14, 38, 1, 393023500, time.Local) 2023-02-19 14:38:01.3930235 +0800 CST m=+0.008177501
    fmt.Printf("%T %#v %v\n", t, t, t)
    //2023-02-19 06:38:42.351399 +0000 UTC
    fmt.Printf("%v\n", t.UTC())
    //time.Date(2023, time.February, 19, 6, 39, 44, 393454500, time.UTC)
    fmt.Printf("%#v\n", t.UTC())
    //2023/02/19 14:40:40 +0800
    fmt.Println(t.Format("2006/01/02 15:04:05 -0700"))
    //2023/02/19 14:40:40
    fmt.Println(t.Format("2006/01/02 15:04:05"))
    //2023/02/19 06:41:41
    fmt.Println(t.UTC().Format("2006/01/02 15:04:05"))
 
    println("========================")
    //根据字符串 格式化成时间 格式要一毛一样
    timeStr := "2023#02/19\t14$42:23"
    parse, err := time.Parse("2006#01/02\t15$04:05", timeStr)
    if err != nil {
        fmt.Println("faile`")
    } else {
        //2023-02-19 14:42:23 +0000 UTC 注意时区
        fmt.Println("success:", parse)
    }
    fmt.Println(parse.Location()) //UTC 返回parse时区
    fmt.Println(parse.Local())    //2023-02-19 22:42:23 +0800 CST 时间加了8小时转到了CST
 
    //加上时区 把时间格式化成真正的当地时间和时区
    loc, _ := time.LoadLocation("Asia/Shanghai")
    t1, err := time.ParseInLocation("2006#01/02\t15$04:05", timeStr, loc)
    if err != nil {
        fmt.Println("fail")
    } else {
        //2023-02-19 14:42:23 +0800 CST
        fmt.Println("t1:", t1)
    }
 
    //时间成分
    fmt.Println(
        //2023 February 2 19
        parse.Year(), parse.Month(), int(parse.Month()), parse.Day(),
        //50
        parse.YearDay(),
        //14 42 23
        parse.Hour(), parse.Minute(), parse.Second(),
        "\n",
        //时间戳
        parse.Unix(), parse.UnixMilli(), parse.UnixMicro(),
    )
 
    //小数部分匹配,可以使用0或9,0需要和小数部分个数匹配,9不需要
    //time.Parse(
    //  "2006/01/02 15:04:05.000 -0700",
    //  "2008/09/08 20:36:50.123 +0800"
    //  )
    //time.Parse(
    //  "2006/01/02 15:04:05.9 -0700",
    //  "2008/09/08 20:36:50.1234 +0800"
    //  )
 
    //时间增量
    println("===========")
    delta := t.Sub(t1) //duration类型
    //5h59m18.468554s 5.988463487222222 21558.468554 5h59m18.468554s
    fmt.Println(delta, delta.Hours(), delta.Seconds(), delta.String())
 
    d1 := time.Duration(3) //3纳秒 3ns
    fmt.Println(d1)
    //3s 3h0m0s
    fmt.Println(time.Duration(3*time.Second), time.Duration(3*time.Hour))
    t2 := t1.Add(time.Duration(3 * time.Second))
    //t2: 2023-02-19 14:42:26 +0800 CST
    fmt.Println("t2:", t2)
    //2023-02-19 14:42:20 +0800 CST
    fmt.Println(t1.Add(-time.Duration(3 * time.Second)))
    fmt.Println(t2.After(t))  //false
    fmt.Println(t2.After(t1)) //true
}
t.Format("2006/01/02 15:04:05 -0700") 转成字符串
time.Parse("2006#01/02\t15$04:05", timeStr) 字符串转成time类型
对time类型进行各种操作