go标准库:time
原文链接 http://ironxu.com/747
介绍Go 标准库 time
常用导出函数,结构体及其方法。
import "time"
- time包提供显示和计算时间用的函数。
1. 时间处理类型
Go 中时间处理依赖的数据类型: time.Time
, time.Month
, time.Weekday
, time.Duration
, time.Location
。
1.1 time.Time 时间点
time.Time
代表一个纳秒精度的时间点.
/usr/local/go/src/time/time.go
定义如下:
type Time struct {sec int64 // 从1年1月1日 00:00:00 UTC 至今过去的秒数nsec int32 // 最近一秒到下一秒过去的纳秒数loc *Location // 时区
}
时间变量标识一个具体的时间点,演示如下:
var t time.Time // 定义 time.Time 类型变量
t = time.Now() // 获取当前时间
fmt.Printf("时间: %v, 时区: %v, 时间类型: %T\n", t, t.Location(), t)
// 时间: 2017-02-22 09:06:05.816187261 +0800 CST, 时区: Local, 时间类型: time.Time// time.UTC() time 返回UTC 时区的时间
fmt.Printf("时间: %v, 时区: %v, 时间类型: %T\n", t.UTC(), t.UTC().Location(), t)
// 时间: 2017-02-22 01:07:15.179280004 +0000 UTC, 时区: UTC, 时间类型: time.Time
代码中应使用time.Time
类型值来保存和传递时间,而不能用指针。表示时间的变量和字段,应为time.Time
类型,而不是*time.Time
类型。
每一个时间点都具有时区信息,当计算时间的表示格式时,如Format
、Hour
和Year
等方法,都会考虑该信息。Local
、UTC
和In
方法返回一个指定时区(但指向同一时间点)的Time
。修改时区信息只是会改变其表示,不会修改被表示的时间点。
1.2 time.Month 月份
time.Month
代表一年中的某个月
/usr/local/go/src/time/time.go
定义如下:
type Month int
和月份相关的常量如下:
const (January Month = 1 + iotaFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
)
1.3 time.Weekday 星期
time.Weekday
代表一周的周几。
/usr/local/go/src/time/time.go
定义如下:
type Weekday int
和星期相关的常量:
const (Sunday Weekday = iotaMondayTuesdayWednesdayThursdayFridaySaturday
)
1.4 time.Duration 时间段
time.Duration
类型代表两个时间点之间经过的纳秒数,可表示的最长时间段约为290年。
/usr/local/go/src/time/time.go
定义如下:
type Duration int64
涉及常量如下:
const (Nanosecond Duration = 1Microsecond = 1000 * NanosecondMillisecond = 1000 * MicrosecondSecond = 1000 * MillisecondMinute = 60 * SecondHour = 60 * Minute
)
1.5 time.Location 时区
Location代表一个地点,以及该地点所在的时区信息。北京时间可以使用 Asia/Shanghai
/usr/local/go/src/time/zoneinfo.go
中定义:
type Location struct {name stringzone []zonetx []zoneTranscacheStart int64cacheEnd int64cacheZone *zone
}
预定义时区变量:
var UTC *Location = &utcLoc
var Local *Location = &localLoc
2. time.Time 方法
介绍 time.Time
接受者的方法:获取时间点,获取时间相关信息,时间比较,计算和序列化操作。
示例代码文件: $GOPATH/src/github.com/ironxu/go_note/library/time/time.go
2.1 获取一个时间的方法
func Now() Time {}
// 当前本地时间func Unix(sec int64, nsec int64) Time {}
// 根据时间戳返回本地时间func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time {}
// 返回指定时间
// 当前本地时间
t = time.Now()
fmt.Println("'time.Now': ", t)// 根据时间戳返回本地时间
t_by_unix := time.Unix(1487780010, 0)
fmt.Println("'time.Unix': ", t_by_unix)// 返回指定时间
t_by_date := time.Date(2017, time.Month(2), 23, 1, 30, 30, 0, l)
fmt.Println("'time.Date': ", t_by_date)
2.2 时间显示
func (t Time) UTC() Time {}
// 获取指定时间在UTC 时区的时间表示func (t Time) Local() Time {}
// 以本地时区表示func (t Time) In(loc *Location) Time {}
// 时间在指定时区的表示func (t Time) Format(layout string) string {}
// 按指定格式显示时间
// 获取指定时间在UTC 时区的时间表示
t_by_utc := t.UTC()
fmt.Println("'t.UTC': ", t_by_utc)// 获取本地时间表示
t_by_local := t.Local()
fmt.Println("'t.Local': ", t_by_local)// 时间在指定时区的表示
t_in := t.In(time.UTC)
fmt.Println("'t.In': ", t_in)// Format
fmt.Println("t.Format", t.Format(time.RFC3339))
2.3 获取日期信息
func (t Time) Date() (year int, month Month, day int) {}
// 返回时间的日期信息func (t Time) Year() int {}
// 返回年func (t Time) Month() Month {}
// 月func (t Time) Day() int {}
// 日func (t Time) Weekday() Weekday {}
// 星期func (t Time) ISOWeek() (year, week int) {}
// 返回年,星期范围编号func (t Time) Clock() (hour, min, sec 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) Location() *Location {}
// 时间的时区func (t Time) Zone() (name string, offset int) {}
// 时间所在时区的规范名和想对UTC 时间偏移量func (t Time) Unix() int64 {}
// 时间转为时间戳func (t Time) UnixNano() int64 {}
// 时间转为时间戳(纳秒)
// 返回时间的日期信息
year, month, day := t.Date()
fmt.Println("'t.Date': ", year, month, day)// 星期
week := t.Weekday()
fmt.Println("'t.Weekday': ", week)// 返回年,星期范围编号
year, week_int := t.ISOWeek()
fmt.Println("'t.ISOWeek': ", year, week_int)// 返回时间的时分秒
hour, min, sec := t.Clock()
fmt.Println("'t.Clock': ", hour, min, sec)
2.4 时间比较与计算
func (t Time) IsZero() bool {}
// 是否是零时时间func (t Time) After(u Time) bool {}
// 时间在u 之前func (t Time) Before(u Time) bool {}
// 时间在u 之后func (t Time) Equal(u Time) bool {}
// 时间与u 相同func (t Time) Add(d Duration) Time {}
// 返回t +d 的时间点func (t Time) Sub(u Time) Duration {}
// 返回 t-ufunc (t Time) AddDate(years int, months int, days int) Time {}
返回增加了给出的年份、月份和天数的时间点Time
// 返回增加了给出的年份、月份和天数的时间点Time
t_new := t.AddDate(0, 1, 1)
fmt.Println("'t.AddDate': ", t_new)// 时间在u 之前
is_after := t.After(t_new)
fmt.Println("'t.After': ", is_after)
2.6 时间序列化
func (t Time) MarshalBinary() ([]byte, error) {}
// 时间序列化func (t Time) UnmarshalBinary(data []byte) error {}
// 反序列化func (t Time) MarshalJSON() ([]byte, error) {}
// 时间序列化func (t Time) MarshalText() ([]byte, error) {}
// 时间序列化func (t Time) GobEncode() ([]byte, error) {}
// 时间序列化func (t Time) GobDecode() ([]byte, error) {}
// 时间序列化
// 时间序列化
t_byte, err := t.MarshalJSON()
fmt.Println("'t.MarshalJSON': ", t_byte, err)// 时间数据反序列化
var t_un time.Time
err = t_un.UnmarshalJSON(t_byte)
fmt.Println("'t_un.UnmarshalJSON': ", t_un, err)
3. time.Duration 方法
介绍 time.Duration
时间段接受者的方法:输出和改变时间段单位。
func (d Duration) String() string
// 格式化输出 Durationfunc (d Duration) Nanoseconds() int64
// 将时间段表示为纳秒func (d Duration) Seconds() float64
// 将时间段表示为秒func (d Duration) Minutes() float64
// 将时间段表示为分钟func (d Duration) Hours() float64
// 将时间段表示为小时
// time.Duration 时间段
fmt.Println("time.Duration 时间段")
d = time.Duration(10000000000000)fmt.Printf("'String: %v', 'Nanoseconds: %v', 'Seconds: %v', 'Minutes: %v', 'Hours: %v'\n",
d.String(), d.Nanoseconds(), d.Seconds(), d.Minutes(), d.Hours())
// 'String: 2h46m40s', 'Nanoseconds: 10000000000000', 'Seconds: 10000', 'Minutes: 166.66666666666666', 'Hours: 2.7777777777777777'
4. time.Location 方法
time.Location
时区的导出函数
func (l *Location) String() string
// 输出时区名func FixedZone(name string, offset int) *Location
// FixedZone 使用给定的地点名name和时间偏移量offset(单位秒)创建并返回一个Locationfunc LoadLocation(name string) (*Location, error)
// LoadLocation 使用给定的名字创建Location
var local *time.Location
local, ok := time.LoadLocation("Asia/Shanghai")
fmt.Printf("%v, %T, %v\n", local, local, ok)
5. 其他方法
func Sleep(d Duration)
// Sleep阻塞当前go程至少d代表的时间段。d<=0时,Sleep会立刻返回
d_second := time.Second
time.Sleep(d_second)
参考资料
- pkg/time 中文
- pkg/time
go标准库:time相关推荐
- Go 学习笔记(78)— Go 标准库 net/http 创建服务端(接收 GET、POST 请求)
使用 net/http 标准库创建一个 http 的 restful api 的服务端,用来处理 GET.POST 等请求. 源代码如下: package mainimport ("enco ...
- GCC 连接器、链接标准库 gcc -l、链接手动创建库(指定目录的库 gcc -L)
1. 链接器 链接器把多个二进制的目标文件(object file)链接成一个单独的可执行文件. 在链接过程中,它必须把符号(变量名.函数名等一些列标识符)用对应的数据的内存地址(变量地址.函数地址等 ...
- Go 学习笔记(55)— Go 标准库 sql (初始化数据库、插入、更新、删除数据库表、单行查询、多行查询、事务处理)
1. 标准库说明 Go 的标准库中是没有数据库驱动,只提供了驱动接口,有很多第三方实现了驱动,我们这里选择 go-sql-driver 这个实现是目前使用最多的.github 地址是:https:// ...
- Python 标准库之 subprocesss
Python 目前已经废弃了 os.system.os.spawn*.os.popen*.popen2.*.commands.* 来执行其他语言的命令,取而代之的是 subprocess 模块. 运行 ...
- Python 标准库之 xml.etree.ElementTree xml解析
Python 标准库之 xml.etree.ElementTree Python中有多种xml处理API,常用的有xml.dom.*模块.xml.sax.*模块.xml.parser.expat模块和 ...
- Go 学习笔记(21)— 标准库 os 操作文件(新建、打开、写入、读取、删除、关闭文件)
Go 操作文本文件时,与其它语言一样也有新建文件.打开文件.写文件.读文件.删除文件等操作.主要有两个标准库来提供这些操作,分别为 os 和 ioutil .在该文中我们介绍 os 模块. 1. 新建 ...
- bitset类型, 标准库类型
C++ primer 17.2 bitset类型, 标准库类型 1 使得位运算更容易实现, 并且能够处理超过最长整型大小的位集合. bitset定义在bitset中 定义和初始化bitset 1 bi ...
- c++标准库 及 命名空间std
1.命名空间std C++标准中引入命名空间的概念,是为了解决不同模块或者函数库中相同标识符冲突的问题.有了命名空间的概念,标识符就被限制在特定的范围(函数)内,不会引起命名冲突.最典型的例子就是st ...
- C++标准库简介(转)
C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能. <cname>形式的标准头文件[ <complex>例外]其 ...
- pythonurllib标准_Python标准库urllib2的一些使用细节总结
Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节描述的并不清楚,比如 urllib2 这个 HTTP 客户端库.这里总结了一些 urllib2 的使用细节. 1.Pr ...
最新文章
- 盘点热门的目标检测开源方案(附论文+代码下载)
- 【Linux】——常见的rc的含义
- 启明云端分享| ESP32-S3 + 480*480分辨率的2.1寸圆屏旋钮方案
- Python 生成账号密码算法
- linux6.7能升级6.8吗,CentOS 六、7升级gcc至4.八、4.九、5.二、6.三、7.3等高版本
- python socket server accpet 时间_Python socket.accept非阻塞?
- jq处理返回来json_(转)JQuery处理json与ajax返回JSON实例
- java环境怎么搭,如何搭建一个完整的Java开发环境
- 4--RESTful应用程序
- 金融项目app业务及测试策略
- 英雄联盟LOL静态HTML网页制作模板DⅣ+CSS学生网页作品代码游戏题材大学生网页设计作业下载
- 面试官:如何理解TCP/IP协议?
- 生物化学-第二章-氨基酸
- chatter投稿&メール通知
- 我理解的Java栈与堆,String类
- NLP中面向文本表示的模型梳理
- openbmc-web3:添加语言
- POJO和PO的概念,区别
- 调用讯雷下载文件(转)
- 简单的合成图片实现‘盖章’
热门文章
- office移动端_阿里云 Teambition 网盘产品快讯:移动端正式版即将上线,体验将大幅提升...
- nginx修改默认端口
- Linux 不小心删除了 root,root目录不小心删除了怎么办……
- 乾坤 微前端_拥抱云时代的前端开发架构——微前端
- VGG16与SSD算法Tensorflow代码实现对比
- sourcetree删除文件夹、重新指向并重定义主分支、
- javascript 性能 · 平稳退化、渐进增强
- 快微音频课程小程序v3.8.4+前端
- 七彩影视双端新版本源码
- 搏天短网址生成网站源码v3.1