cunix时间戳转换成时间_hpunix修改时间_unix时间

导入形式:

import "time"

time包提供了时间的显示和测量用的函数。日历的计算采用的是公历。

1》时间点Time

typeTime

type Time struct {
    wall uint64
    ext  int64
    loc *Location
}

Time代表一个纳秒精度的时间点。

Time零值代表时间点January 1, year 1, 00:00:00.000000000 UTC。因为本时间点一般不会出现在使用中,IsZero方法提供了检验时间是否显式初始化的一个简单途径。

每一个时间都具有一个地点信息(及对应地点的时区信息),当计算时间的表示格式时,如Format、Hour和Year等方法,都会考虑该信息。Local、UTC和In方法返回一个指定时区(但指向同一时间点)的Time。修改地点/时区信息只是会改变其表示;不会修改被表示的时间点,因此也不会影响其计算。

func (Time)String

func (t Time) String() string

String返回采用如下格式字符串的格式化时间。

"2006-01-02 15:04:05.999999999 -0700 MST"

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
    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
    RFC3339     = "2006-01-02T15:04:05Z07:00"
    RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
    Kitchen     = "3:04PM"
    // 方便的时间戳
    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"
)

这些预定义的版式用于time.Format和time.Parse函数。用在版式中的参考时间是:

Mon Jan 2 15:04:05 MST 2006

对应的Unix时间是1136239445

1)举例如何使用在time.Format函数中:

funcNow

func Now() Time

Now返回当前本地时间。

func (Time)Format

func (t Time) Format(layout string) string

Format根据layout指定的格式返回t代表的时间点的格式化文本表示,即将时间转换成想要的格式,layout定义了参考时间

package main 
import(
    "fmt"
    "time"
)
func main() {
    fmt.Println(time.Now().Format(time.UnixDate))         //Tue Feb 12 12:10:21 CST 2019
    fmt.Println(time.Now().Format(time.RFC3339Nano))      //2019-02-12T12:10:21.880328+08:00
    fmt.Println(time.Now().Format(time.StampMicro))       //Feb 12 12:10:21.880337
    //自定义格式
    fmt.Println(time.Now().Format("2006-01-02 15:04:05")) //2019-02-12 12:10:21
    fmt.Println(time.Now().Format("Jan 2, 2006 at 3:04pm (MST)"))  //Feb 12, 2019 at 12:10pm (CST)
    fmt.Println(time.Now().Format("2006-Jan-02"))  //2019-Feb-12 
}

2)如何使用在time.Parse函数中,上面例子的逆向:

funcParse

func Parse(layout, value string) (Time, error)

Parse解析一个格式化的时间字符串并返回它代表的时间。layout定义了参考的时间格式

package main 
import(
    "fmt"
    "time"
)
func main() {
    unix, _ := time.Parse(time.UnixDate, "Tue Feb 12 12:10:21 CST 2019")
    fmt.Println(unix) //2019-02-12 12:10:21 +0800 CST
    rfc, _ := time.Parse(time.RFC3339Nano, "2019-02-12T12:10:21.880328+08:00")
    fmt.Println(rfc) //2019-02-12 12:10:21.880328 +0800 CST
    stamp, _ := time.Parse(time.StampMicro, "Feb 12 12:10:21.880337")
    fmt.Println(stamp) //0000-02-12 12:10:21.880337 +0000 UTC
    custom1Form := "2006-01-02 15:04:05"
    custom1, _ := time.Parse(custom1Form, "2019-02-12 12:10:21")
    fmt.Println(custom1) //2019-02-12 12:10:21 +0000 UTC
    custom2Form := "Jan 2, 2006 at 3:04pm (MST)"
    custom2, _ := time.Parse(custom2Form, "Feb 12, 2019 at 12:10pm (CST)")
    fmt.Println(custom2) //2019-02-12 12:10:00 +0800 CST
    custom3Form := "2006-Jan-02"
    custom3, _ := time.Parse(custom3Form, "2019-Feb-12")
    fmt.Println(custom3) //2019-02-12 00:00:00 +0000 UTC
}

数字表示的时区格式如下:

-0700  ±hhmm
-07:00 ±hh:mm

将格式字符串中的负号替换为Z会触发ISO 8601行为(当时区是UTC时,输出Z而不是时区偏移量),这样:

Z0700  Z or ±hhmm
Z07:00 Z or ±hh:mm

其他的获取Time对象的方法:

首先先说明时区Location

typeLocation

type Location struct {
    name string
    zone []zone
    tx   []zoneTrans
    cacheStart int64
    cacheEnd   int64
    cacheZone  *zone
}

Location代表一个(关联到某个时间点的)地点,以及该地点所在的时区。

var Local *Location = &localLoc

Local代表系统本地,对应本地时区,通过time.Local来使用。

var UTC *Location = &utcLoc

UTC代表通用协调时间,对应零时区,通过time.UTC来使用。

3)

funcLoadLocation

func LoadLocation(name string) (*Location, error)

LoadLocation返回使用给定的名字创建的Location,即时区。

如果name是””或”UTC”,返回UTC;如果name是”Local”,返回Local;否则name应该是IANA时区数据库里有记录的地点名(该数据库记录了地点和对应的时区),如”America/New_York”。

LoadLocation函数需要的时区数据库可能不是所有系统都提供,特别是非Unix系统。此时LoadLocation会查找环境变量ZONEINFO指定目录或解压该变量指定的zip文件(如果有该环境变量);然后查找Unix系统的惯例时区数据安装位置,最后查找$GOROOT/lib/time/zoneinfo.zip。

func (Time)In

func (t Time) In(loc *Location) Time

In返回采用loc指定的地点和时区,但指向同一时间点的Time。如果loc为nil会panic。

package main 
import(
    "fmt"
    "time"
)
func main() {
    loc, _ := time.LoadLocation("America/Los_Angeles") //美国洛杉矶时区PST
    fmt.Println(loc) //America/Los_Angeles
    fmt.Println(time.Now()) //2019-02-12 15:03:12.649876 +0800 CST m=+0.000715568,相对于UTC时区正向差8小时
    fmt.Println(time.Now().In(loc)) //2019-02-11 23:03:12.649952 -0800 PST,相对于UTC时区正向差8小时,因此loc时区和本地时区相差16小时
}

4)

funcParseInLocation

func ParseInLocation(layout, value string, loc *Location) (Time, error)

ParseInLocation类似Parse但有两个重要的不同之处。第一,当缺少时区信息时,Parse将时间解释为UTC时间,而ParseInLocation将返回值的Location设置为loc;第二,当时间字符串提供了时区偏移量信息时,Parse会尝试去匹配本地时区,而ParseInLocation会去匹配loc。

举例:

package main 
import(
    "fmt"
    "time"
)
func main() {
    loc, _ := time.LoadLocation("America/Los_Angeles")
    custom2Form := "2006-01-02 15:04:05"
    custom2, _ := time.ParseInLocation(custom2Form, "2019-02-12 12:10:21", loc)
    fmt.Println(custom2) //2019-02-12 12:10:21 -0800 PST,可见将时区设置为了指定的loc时区
    custom2Parse, _ := time.Parse(custom2Form, "2019-02-12 12:10:21")
    fmt.Println(custom2Parse) //2019-02-12 12:10:21 +0000 UTC,可见如果没有设置时区则默认为UTC时区
}

5)

funcUnix

func Unix(sec int64, nsec int64) Time

Unix创建一个本地时间,对应sec和nsec表示的Unix时间(从January 1, 1970 UTC至该时间的秒数和纳秒数)。

nsec的值在[0, 999999999]范围外是合法的。

获得时间的时间戳

func (Time)Unix

func (t Time) Unix() int64

Unix将t表示为Unix时间,即从时间点January 1, 1970 UTC到时间点t所经过的时间(单位秒)。

func (Time)UnixNano

func (t Time) UnixNano() int64

UnixNano将t表示为Unix时间,即从时间点January 1, 1970 UTC到时间点t所经过的时间(单位纳秒)。如果纳秒为单位的unix时间超出了int64能表示的范围,结果是未定义的。注意这就意味着Time零值调用UnixNano方法的话,结果是未定义的。

举例:

package main 
import(
    "fmt"
    "time"
)
func main() {
    fmt.Println(time.Now().Unix())     //1549956211
    fmt.Println(time.Now().UnixNano()) //1549956211529784000
   fmt.Println(time.Unix(1549956211, 529784000)) //2019-02-12 15:23:31.529784 +0800 CST
}

然后使用上面例子得到的秒数或纳秒数来调用Unix得到Time对象:

package main 
import(
    "fmt"
    "time"
)
func main() {
    fmt.Println(time.Unix(1549956211, 0)) //2019-02-12 15:23:31 +0800 CST
    fmt.Println(time.Unix(0, 1549956211529784000)) //2019-02-12 15:23:31.529784 +0800 CST
}

6)

funcDate

func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time

Date返回一个时区为loc、当地时间为:

year-month-day hour:min:sec + nsec nanoseconds

的时间点。

month、day、hour、min、sec和nsec的值可能会超出它们的正常范围,在转换前函数会自动将之规范化。如October 32被修正为November 1。

举例:

package main 
import(
    "fmt"
    "time"
)
func main() {
    fmt.Println(time.Unix(1549956211, 529784000)) //2019-02-12 15:23:31.529784 +0800 CST
    //等价于上面的Unix方法
    t := time.Date(2019, time.February, 12, 15, 23, 31, 529784000, time.Local)
    fmt.Println(t)////2019-02-12 15:23:31.529784 +0800 CST
}

当你得到了Time对象后,你就能够调用相应的函数来获得相应的信息,选择其中几个说明,其他省略:

func (Time)Round

func (t Time) Round(d Duration) Time

返回距离t最近的时间点,得到的是晚于t的时间,该时间点应该满足从Time零值到该时间点的时间段能整除d;如果有两个满足要求的时间点,距离t相同,会向上舍入;如果d

限 时 特 惠: 本站每日持续更新海量各大内部创业教程,一年会员只需98元,全站资源免费下载 点击查看详情
站 长 微 信: lzxmw777

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注