时间

标准包Time

time
type Time struct {
    // wall and ext encode the wall time seconds, wall time nanoseconds,
    // and optional monotonic clock reading in nanoseconds.
    //
    // From high to low bit position, wall encodes a 1-bit flag (hasMonotonic),
    // a 33-bit seconds field, and a 30-bit wall time nanoseconds field.
    // The nanoseconds field is in the range \[0, 999999999\].
    // If the hasMonotonic bit is 0, then the 33-bit field must be zero
    // and the full signed 64-bit wall seconds since Jan 1 year 1 is stored in ext.
    // If the hasMonotonic bit is 1, then the 33-bit field holds a 33-bit
    // unsigned wall seconds since Jan 1 year 1885, and ext holds a
    // signed 64-bit monotonic clock reading, nanoseconds since process start.
    wall uint64
    ext  int64

    // loc specifies the Location that should be used to
    // determine the minute, hour, month, day, and year
    // that correspond to this Time.
    // The nil location means UTC.
    // All UTC times are represented with loc==nil, never loc==&utcLoc.
    loc \*Location
}
Time
TimeTimetime.Timetime.Time

可以看到三个属性

package main

import "time"

const UINT64_MIN uint64 = 0
const UINT64_MAX uint64 = ^uint64(0)
const form = "2006-01-02 15:04:05"

func main() {

    t, _ := time.Parse(form, "9999-12-31 23:59:59")
    println("unint64 的最大值为:", UINT64_MAX)
    println("9999-12-31 23:59:59 的时间戳为:", t.Unix())
}

结果:

unint64 的最大值为: 18446744073709551615
9999-12-31 23:59:59 的时间戳为: 253402300799
wall
// wall and ext encode the wall time seconds, wall time nanoseconds,
// and optional monotonic clock reading in nanoseconds.
//
// From high to low bit position, wall encodes a 1-bit flag (hasMonotonic),
// a 33-bit seconds field, and a 30-bit wall time nanoseconds field.
// The nanoseconds field is in the range [0, 999999999].
// If the hasMonotonic bit is 0, then the 33-bit field must be zero
// and the full signed 64-bit wall seconds since Jan 1 year 1 is stored in ext.
// If the hasMonotonic bit is 1, then the 33-bit field holds a 33-bit
// unsigned wall seconds since Jan 1 year 1885, and ext holds a
// signed 64-bit monotonic clock reading, nanoseconds since process start.
wallexthasMonotonic
hasMonotonichasMonotonic = 1 << 63 
0extwall1ext
locloc
locUTCloc == nil loc == &utcLoc

时间戳转换


package main

import (
	"log"
	"time"
)

func main() {
	t := int64(1546926630)      //外部传入的时间戳(秒为单位),必须为int64类型
	t1 := "2019-01-08 13:50:30" //t1的时间字符串

	//时间转换的模板,golang里面只能是 "2006-01-02 15:04:05" (go的诞生时间)
	timeTemplate1 := "2006-01-02 15:04:05" //常规类型
	timeTemplate2 := "2006/01/02 15:04:05" //其他类型
	timeTemplate3 := "2006-01-02"          //其他类型
	timeTemplate4 := "15:04:05"            //其他类型
	timeTemplate5 := "20060102150405"      //其他类型

	// ======= 将时间戳格式化为日期字符串 =======
	log.Println(time.Unix(t, 0).Format(timeTemplate1)) //输出:2019-01-08 13:50:30
	log.Println(time.Unix(t, 0).Format(timeTemplate2)) //输出:2019/01/08 13:50:30
	log.Println(time.Unix(t, 0).Format(timeTemplate3)) //输出:2019-01-08
	log.Println(time.Unix(t, 0).Format(timeTemplate4)) //输出:13:50:30
	log.Println(time.Unix(t, 0).Format(timeTemplate5)) //输出:20190108135030

	// ======= 将时间字符串转换为时间戳 =======
	stamp, _ := time.ParseInLocation(timeTemplate1, t1, time.Local) //使用parseInLocation将字符串格式化返回本地时区时间
	log.Println(stamp.Unix())                                       //输出:1546926630
}

获取想要的时间格式——string

// time对象
  timeStr := time.Now().Format("2006-01-02")
	println(timeStr) // 2022-09-18
// 时间戳
  t := int64(1546926630)
  println(time.Unix(t, 0).Format("2006-01-02")) //2019-01-08

获取想要的时间戳——int64

  timeStr := time.Now().Format("2006-01-02")
  t5, _ := time.ParseInLocation("2006-01-02", timeStr, time.Local)
	timeUnix := t5.Unix()
	println(timeUnix) // 1663430400

处理时间差

time常用方法

After(u Time) bool 
时间类型比较,是否在Time之后

Before(u Time) bool 
时间类型比较,是否在Time之前

Equal(u Time) bool 
比较两个时间是否相等

IsZero() bool 
判断时间是否为零值,如果sec和nsec两个属性都是0的话,则该时间类型为0

Date() (year int, month Month, day int) 
返回年月日,三个参数

Year() int 
返回年份

Month() Month 
返回月份.是Month类型

Day() int 
返回多少号

Weekday() Weekday 
返回星期几,是Weekday类型

ISOWeek() (year, week int) 
返回年份,和该填是在这年的第几周.

Clock() (hour, min, sec int) 
返回小时,分钟,秒

Hour() int 
返回小时

Minute() int 
返回分钟

Second() int 
返回秒数

Nanosecond() int 
返回纳秒
// Add 时间相加
	now := time.Now()
	// ParseDuration parses a duration string.
	// A duration string is a possibly signed sequence of decimal numbers,
	// each with optional fraction and a unit suffix,
	// such as "300ms", "-1.5h" or "2h45m".
	//  Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
	
	// 10分钟前
	m, _ := time.ParseDuration("-1m")
	m1 := now.Add(m)
	fmt.Println(m1)
	// 一天后
	dd, _ := time.ParseDuration("24h")
	dd1 := now.Add(dd)
	fmt.Println(dd1)
	
	
	// Sub 计算两个时间差
	subM := now.Sub(m1)
	fmt.Println(subM.Minutes(), "分钟")

	sumD := now.Sub(dd1)
	fmt.Printf("%v 天\n", sumD.Hours()/24)