Golang时间

当前时间

golang 的时间是 time.Time

package main

import (
	"fmt"
	"time"
)

func main() {
 //当前时间 golang的时间是 time.Time
 fmt.Println(time.Now()) //2022-11-03 19:52:12.457665 +0800 CST m=+0.000146886
}

时间格式化

Time.Format(layout string) 进行格式化 layout:格式 2006:年 01:月 02:日 15:时 04:分 05:秒

package main

import (
	"fmt"
	"time"
)

func main() {
 now := time.Now()

 //Time.Format(layout string) layout:格式 2006:年 01:月 02:日 15:时 04:分 05:秒
 fmt.Println(now.Format("2006-01-02 15:04:05")) //2022-11-04 10:33:40
 fmt.Println(now.Format("2006-01-02")) //2022-11-04
 fmt.Println(now.Format("15:04:05")) //10:33:40

}

字符串转时间

1.time.Parse(layout, value string) (Time, error) 将字符串时间转换成time.Time,其中layout是时间格式,value是字符串时间,要求value格式匹配layout,使用默认UTC时区

2.time.ParseInLocation 和 time.Parse 基本一致,只不过提供设置时区(第三参数)
time.Local 根据本地系统区获取对应的时区
time.LoadLocation(name string) (*Location, error) 根据name查找对应的时区

package main

import (
	"fmt"
	"time"
)

func main() {
	format := "2006-01-02 15:04:05"
	value := "2022-02-02 22:22:22"

	// time.Parse(layout, value string) (Time, error) 将字符串时间转换成time.Time,其中layout是时间格式,value是字符串时间,要求value格式匹配layout,使用默认UTC时区
	parse, err := time.Parse("2006-01-02 15:04:05", value)
	if err != nil {
		panic(err)
	}
	fmt.Println(parse) //2022-02-02 22:22:22 +0000 UTC
	parseAndPrintTime(format,value,"") // 2022-02-02 22:22:22 +0800 CST
	parseAndPrintTime(format,value,"Japan") // 2022-02-02 22:22:22 +0900 JST
}

func parseAndPrintTime(layout, value, location string) {
	//time.ParseInLocation 和 time.Parse 基本一致,只不过提供设置时区(第三参数)
	//time.Local 根据本地系统区获取对应的时区
	//time.LoadLocation(name string) (*Location, error) 根据name查找对应的时区
	var loadLocation *time.Location
	if location == "" {
		loadLocation = time.Local
	}else {
		l, err := time.LoadLocation(location)
		if err != nil {
			panic(err)
		}
		loadLocation = l
	}

	t, err := time.ParseInLocation(layout, value, loadLocation) //time.Local:根据系统找到对应时区
	if err != nil {
		panic(err)
	}
	fmt.Println(t)
}

时间转时间戳

使用Time.Unix()将时间转换成unix时间戳,注意时间戳是UTC标准下的时间戳

package main

import (
	"fmt"
	"time"
)

func main() {
	format := "2006-01-02 15:04:05"
	value := "2022-02-02 22:22:22"
	parse, err := time.Parse(format, value)
	if err != nil {
		panic(err)
	}

	parse2 := parseAndTime(format,value,"PRC")
	fmt.Println(parse) //2022-02-02 22:22:22 +0000 UTC
	fmt.Println(parse2) //2022-02-02 22:22:22 +0800 CST

	//使用Time.Unix()将时间转换成unix时间戳,注意时间戳是UTC标准下的时间戳
	fmt.Println(parse.Unix()) // 1643840542
	fmt.Println(parse2.Unix()) // 1643811742
}

func parseAndTime(layout, value, location string) time.Time{
	//time.ParseInLocation 和 time.Parse 基本一致,只不过提供设置时区(第三参数)
	//time.Local 根据本地系统区获取对应的时区
	//time.LoadLocation(name string) (*Location, error) 根据name查找对应的时区
	var loadLocation *time.Location
	if location == "" {
		loadLocation = time.Local
	}else {
		l, err := time.LoadLocation(location)
		if err != nil {
			panic(err)
		}
		loadLocation = l
	}

	t, err := time.ParseInLocation(layout, value, loadLocation) //time.Local:根据系统找到对应时区
	if err != nil {
		panic(err)
	}
	return t
}
字符串转时间戳

1.字符串先转时间,时间再转时间戳

2.Time.Format是转换成当地时区的时间,所以要注意时间的时区,可能出现显示的时间和预期的不一样的结果

package main

import (
	"fmt"
	"time"
)

func main() {
	format := "2006-01-02 15:04:05"
	value := "2022-02-02 22:22:22"
	parse, err := time.Parse(format, value)
	if err != nil {
		panic(err)
	}

	parse2 := parseAndTime(format,value,"PRC")
	fmt.Println(parse) //2022-02-02 22:22:22 +0000 UTC
	fmt.Println(parse2) //2022-02-02 22:22:22 +0800 CST

	//使用Time.Unix()将时间转换成unix时间戳,注意时间戳是UTC标准下的时间戳
	fmt.Println(parse.Unix()) // 1643840542
	fmt.Println(parse2.Unix()) // 1643811742

	unix := time.Unix(parse.Unix(), 0)
	unix2 := time.Unix(parse2.Unix(), 0)

	//Time.Format是转换成当地时区的时间,所以要注意时间的时区,可能出现显示的时间和预期的不一样的结果
	fmt.Println(unix.Format(format)) //2022-02-03 06:22:22 (PRC比UTC早8个小时)
	fmt.Println(unix2.Format(format))  //2022-02-02 22:22:22
}

func parseAndTime(layout, value, location string) time.Time{
	//time.ParseInLocation 和 time.Parse 基本一致,只不过提供设置时区(第三参数)
	//time.Local 根据本地系统区获取对应的时区
	//time.LoadLocation(name string) (*Location, error) 根据name查找对应的时区
	var loadLocation *time.Location
	if location == "" {
		loadLocation = time.Local
	}else {
		l, err := time.LoadLocation(location)
		if err != nil {
			panic(err)
		}
		loadLocation = l
	}

	t, err := time.ParseInLocation(layout, value, loadLocation) //time.Local:根据系统找到对应时区
	if err != nil {
		panic(err)
	}
	return t
}
时间向前或向后

时间的前进和后退可以使用t.Add(time.Duration),正的往后推,负的往前推

package main

import (
	"fmt"
	"time"
)

func main() {
	format := "2006-01-02 15:04:05"
	value := "2022-02-02 22:22:22"
	t := parseAndTime(format,value,"PRC")

	//时间的前进和后退可以使用t.Add(time.Duration),正的往后推,负的往前推
	t2 := t.Add(time.Hour * 12)
	t3 := t.Add(time.Hour * -12)
	fmt.Println(t2) // 2022-02-03 10:22:22 +0800 CST
	fmt.Println(t3) // 2022-02-02 10:22:22 +0800 CST
}

func parseAndTime(layout, value, location string) time.Time{
	//time.ParseInLocation 和 time.Parse 基本一致,只不过提供设置时区(第三参数)
	//time.Local 根据本地系统区获取对应的时区
	//time.LoadLocation(name string) (*Location, error) 根据name查找对应的时区
	var loadLocation *time.Location
	if location == "" {
		loadLocation = time.Local
	}else {
		l, err := time.LoadLocation(location)
		if err != nil {
			panic(err)
		}
		loadLocation = l
	}

	t, err := time.ParseInLocation(layout, value, loadLocation) //time.Local:根据系统找到对应时区
	if err != nil {
		panic(err)
	}
	return t
}
获取两个时间的间隔(持续时间)

time.Sub(time2) 获取time - time2的间隔(持续时间:time.Duration)

package main

import (
	"fmt"
	"time"
)

func main() {
	format := "2006-01-02 15:04:05"
	value1 := "2022-02-02 22:22:22"
	value2 := "2022-02-02 12:10:10"
	t1 := parseAndTime(format,value1,"PRC")
	t2 := parseAndTime(format,value2,"PRC")
	//time.Sub(time2) 获取time - time2的间隔(持续时间:time.Duration)
	fmt.Println(t1.Sub(t2)) //10h12m12s
}

func parseAndTime(layout, value, location string) time.Time{
	//time.ParseInLocation 和 time.Parse 基本一致,只不过提供设置时区(第三参数)
	//time.Local 根据本地系统区获取对应的时区
	//time.LoadLocation(name string) (*Location, error) 根据name查找对应的时区
	var loadLocation *time.Location
	if location == "" {
		loadLocation = time.Local
	}else {
		l, err := time.LoadLocation(location)
		if err != nil {
			panic(err)
		}
		loadLocation = l
	}

	t, err := time.ParseInLocation(layout, value, loadLocation) //time.Local:根据系统找到对应时区
	if err != nil {
		panic(err)
	}
	return t
}
time.Duration

1.可以理解为时间中比较常用的“单位”,常用的有:时(1h0m0s),分(1m0s),秒(1s)
2.如果需要一些特别的持续时间,可以通过 time.ParseDuration(s string) (Duration, error) 将时间解析为time.Duration

package main

import (
	"fmt"
	"time"
)

func main() {
	//可以理解为时间中比较常用的“单位”,常用的有:时(h),分(m),秒(s)
	fmt.Println(time.Second) //1s
	fmt.Println(time.Hour) //1h0m0s
	fmt.Println(time.Minute) //1m0s

	//如果需要一些特别的持续时间,可以通过 time.ParseDuration(s string) (Duration, error) 将时间解析为time.Duration
	duration, err := time.ParseDuration("5h20m")
	if err != nil {
		panic(err)
	}
	fmt.Println(duration) //5h20m0s
}

func parseAndTime(layout, value, location string) time.Time{
	//time.ParseInLocation 和 time.Parse 基本一致,只不过提供设置时区(第三参数)
	//time.Local 根据本地系统区获取对应的时区
	//time.LoadLocation(name string) (*Location, error) 根据name查找对应的时区
	var loadLocation *time.Location
	if location == "" {
		loadLocation = time.Local
	}else {
		l, err := time.LoadLocation(location)
		if err != nil {
			panic(err)
		}
		loadLocation = l
	}

	t, err := time.ParseInLocation(layout, value, loadLocation) //time.Local:根据系统找到对应时区
	if err != nil {
		panic(err)
	}
	return t
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言中的时间时区是通过time包来处理的。time包提供了一些函数和方法来获取和操作时间以及时区信息。 在Go中,可以使用time.Now()函数来获取当前的本地时间。这个函数返回一个time.Time类型的值,其中包含了年、月、日、时、分、秒等信息。 要获取特定时区的时间,可以使用time.LoadLocation()函数来加载指定的时区。例如,要获取纽约的时间,可以使用以下代码: ``` loc, err := time.LoadLocation("America/New_York") if err != nil { fmt.Println("加载时区失败:", err) return } nyTime := time.Now().In(loc) fmt.Println("纽约时间:", nyTime) ``` 在上面的代码中,我们首先使用time.LoadLocation()函数加载了纽约的时区,然后使用time.Now().In()方法将当前时间转换为纽约时区的时间。 除了获取特定时区的时间,还可以使用time.FixedZone()函数创建一个固定偏移量的时区。例如,要创建一个偏移量为-8小时的时区,可以使用以下代码: ``` offset := -8 * 60 * 60 // 偏移量为-8小时 fixedZone := time.FixedZone("CustomZone", offset) customTime := time.Now().In(fixedZone) fmt.Println("自定义时区时间:", customTime) ``` 在上面的代码中,我们使用time.FixedZone()函数创建了一个偏移量为-8小时的自定义时区,并将当前时间转换为该时区的时间。 需要注意的是,Go语言中的时间时区信息是通过time.Time类型来保存的,而不是单独的时区类型。因此,在处理时间时,需要使用相应的时区信息来进行转换和计算。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值