golang time包的用法详解
作者:飘ing 发布时间:2024-04-25 13:19:19
在我们编程过程中,经常会用到与时间相关的各种务需求,下面来介绍 golang 中有关时间的一些基本用法,我们从 time 的几种 type 来开始介绍。
时间可分为时间点与时间段,golang 也不例外,提供了以下两种基础类型
- 时间点(Time)
- 时间段(Duration)
除此之外 golang 也提供了以下类型,做一些特定的业务
- 时区(Location)
- Ticker
- Timer(定时器)
我们将按以上顺序来介绍 time 包的使用。
时间点(Time)
我们使用的所有与时间相关的业务都是基于点而延伸的,两点组成一个时间段,大多数应用也都是围绕这些点与面去做逻辑处理。
初始化
go 针对不同的参数类型提供了以下初始化的方式
// func Now() Time
fmt.Println(time.Now())
// func Parse(layout, value string) (Time, error)
time.Parse("2016-01-02 15:04:05", "2018-04-23 12:24:51")
// func ParseInLocation(layout, value string, loc *Location) (Time, error) (layout已带时区时可直接用Parse)
time.ParseInLocation("2006-01-02 15:04:05", "2017-05-11 14:06:06", time.Local)
// func Unix(sec int64, nsec int64) Time
time.Unix(1e9, 0)
// func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
time.Date(2018, 1, 2, 15, 30, 10, 0, time.Local)
// func (t Time) In(loc *Location) Time 当前时间对应指定时区的时间
loc, _ := time.LoadLocation("America/Los_Angeles")
fmt.Println(time.Now().In(loc))
// func (t Time) Local() Time
获取到时间点之后为了满足业务和设计,需要转换成我们需要的格式,也就是所谓的时间格式化。
格式化
to string
格式化为字符串我们需要使用 time.Format 方法来转换成我们想要的格式
fmt.Println(time.Now().Format("2006-01-02 15:04:05")) // 2018-04-24 10:11:20
fmt.Println(time.Now().Format(time.UnixDate)) // Tue Apr 24 09:59:02 CST 2018
Format 函数中可以指定你想使用的格式,同时 time 包中也给了一些我们常用的格式
const (
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
)
注意: galang 中指定的特定时间格式为 "2006-01-02 15:04:05 -0700 MST", 为了记忆方便,按照美式时间格式 月日时分秒年 外加时区 排列起来依次是 01/02 03:04:05PM ‘06 -0700,刚开始使用时需要注意。
to time stamp
func (t Time) Unix() int64
func (t Time) UnixNano() int64
fmt.Println(time.Now().Unix())
// 获取指定日期的时间戳
dt, _ := time.Parse("2016-01-02 15:04:05", "2018-04-23 12:24:51")
fmt.Println(dt.Unix())
fmt.Println(time.Date(2018, 1,2,15,30,10,0, time.Local).Unix())
其他
time 包还提供了一些常用的方法,基本覆盖了大多数业务,从方法名就能知道代表的含义就不一一说明了。
func (t Time) Date() (year int, month Month, day int)
func (t Time) Clock() (hour, min, sec int)
func (t Time) Year() int
func (t Time) Month() Month
func (t Time) Day() int
func (t Time) Hour() int
func (t Time) Minute() int
func (t Time) Second() int
func (t Time) Nanosecond() int
func (t Time) YearDay() int
func (t Time) Weekday() Weekday
func (t Time) ISOWeek() (year, week int)
func (t Time) IsZero() bool
func (t Time) Local() Time
func (t Time) Location() *Location
func (t Time) Zone() (name string, offset int)
func (t Time) Unix() int64
时间段(Duartion)
介绍完了时间点,我们再来介绍时间段,即 Duartion 类型, 我们业务也是很常用的类型。
// func ParseDuration(s string) (Duration, error)
tp, _ := time.ParseDuration("1.5s")
fmt.Println(tp.Truncate(1000), tp.Seconds(), tp.Nanoseconds())
func (d Duration) Hours() float64
func (d Duration) Minutes() float64
func (d Duration) Seconds() float64
func (d Duration) Nanoseconds() int64
func (d Duration) Round(m Duration) Duration // 四舍五入
func (d Duration) Truncate(m Duration) Duration // 向下取整
时区(Location)
我们在来介绍一下时区的相关的函数
// 默认UTC
loc, err := time.LoadLocation("")
// 服务器设定的时区,一般为CST
loc, err := time.LoadLocation("Local")
// 美国洛杉矶PDT
loc, err := time.LoadLocation("America/Los_Angeles")
// 获取指定时区的时间点
local, _ := time.LoadLocation("America/Los_Angeles")
fmt.Println(time.Date(2018,1,1,12,0,0,0, local))
可以在 $GOROOT/lib/time/zoneinfo.zip 文件下看到所有时区。
时间运算
来源:https://blog.csdn.net/wschq/article/details/80114036


猜你喜欢
- 写爬虫有一个绕不过去的问题就是验证码,现在验证码分类大概有4种:图像类滑动类点击类语音类今天先来看看图像类,这类验证码大多是数字、字母的组合
- 获取nc数据的相关信息from netCDF4 import Datasetimport numpy as npimport pandas
- 导语无论家用电脑还是公司的电脑,定时开关机都是一个非常实用的功能,只是一般都不太受关注。定时关机不仅能延长电脑的使用寿命,还能节约超多的电费
- 以前看到 andy的关于“Quiet Structure”觉的很不错,于是今天到她的个人站点上逛逛,发现不少好的文章,今天介绍的是
- 测试字符串:<style>v\:* { BEHAVIOR: url(#default#VML) } o\:* { BEHAVIO
- 如何利用网页弹出各种形式的窗口,我想大家大多都是知道些的,但那种多种多样的弹出式窗口是怎么搞出来的,我们今天就来学习一下:推荐:网页弹出窗口
- Bytes和Str的区别在Python3中,字符序列有两种类型:bytes和str。bytes类型是无符号的8位值(通常以ASCII码显式)
- 偶然从pytorch讨论论坛中看到的一个问题,KL divergence different results from tf,kl dive
- 关于SQL Server 2014中的基数估计,官方文档Optimizing Your Query Plans with the SQL S
- CONVERT函数用于将值转换为指定的数据类型或字符集1.转换指定字符集CONVERT函数用于将字符串expr的字符集变成transcodi
- 本文实例讲述了Python实现批量读取word中表格信息的方法。分享给大家供大家参考。具体如下:单位收集了很多word格式的调查表,领导需要
- 小编今天写下关于后台管理员权限的分配自己的思路想法<?php /**reader * 小编的思想比较简单实现的功能
- 效果图如下所示: 前言嗨,说起探探想必各位程序汪都不陌生(毕竟妹子很多),能在上面丝滑的翻牌子,探探的的堆叠滑动组件起到了关键的作
- 一、半同步简介MASTER节点在执行完客户端提交的事务后不是立刻返回结果给客户端,而是等待至少一个SLAVE节点接收并写到relay log
- 当发现目录时出错如下:\windows\tensorflow\core\framework\op_kernel.cc:993] Not fo
- 在讲样式表开发管理之前,我想插播一个小知识。前几天看web标准设计组里,看到龍佑康同学问到关于 block 和 inline 的区别。记得以
- 数据结构Redis有五种基础数据结构,分别为:1、string(字符串)2、list(列表)3、hash(字典)4、set(集合)5、zse
- 一、相关模块jieba:中文分词wordcloud :Python词云库imageio:读取图形数据安装:pip install&
- python 中提供一种用于对函数固定属性的函数(与数学上的偏函数不一样)# 通常会返回10进制int('12345') &
- 一、模块&包简介模块:所谓模块就是一个.py文件,用来存放变量,方法的文件,便于在其他python文件中导入(通过import或fr