Go语言教程之边写边学:基础练习:将特定的UTC日期时间转换为 PST、HST、MST 和 SGT

跨地区的开发中需要把一个时区的时间转化为其他时区的时间,以下是相关示例。

示例代码:

package main
 
import (
    "fmt"
    "time"
)
 
func main() {
    t, err := time.Parse("2006 01 02 15 04", "2015 11 11 16 50")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(t)
 
    loc, err := time.LoadLocation("America/Los_Angeles")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(loc)
     
    t = t.In(loc)
    fmt.Println(t.Format(time.RFC822))
     
    loc, err = time.LoadLocation("Singapore")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(loc)
 
    t = t.In(loc)
    fmt.Println(t.Format(time.RFC822))
     
    loc, err = time.LoadLocation("US/Hawaii")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(loc)
 
    t = t.In(loc)
    fmt.Println(t.Format(time.RFC822))
     
    loc, err = time.LoadLocation("EST")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(loc)
     
    t = t.In(loc)
    fmt.Println(t.Format(time.RFC822))
     
    loc, err = time.LoadLocation("MST")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(loc)
     
    t = t.In(loc)
    fmt.Println(t.Format(time.RFC822))
     
}

输出:

2015-11-11 16:50:00 +0000 UTC
America/Los_Angeles
11 Nov 15 08:50 PST
Singapore
12 Nov 15 00:50 SGT
US/Hawaii
11 Nov 15 06:50 HST
EST
11 Nov 15 11:50 EST
MST
11 Nov 15 09:50 MST
Go语言教程之边写边学:基础练习:获取 EST、UTC 和 MST 中的当前日期和时间

示例代码:

package main
 
import (
         "fmt"
         "time"
)
 
func main() {
    t := time.Now() 
    z, _ := t.Zone()
    fmt.Println("ZONE : ", z, " Time : ", t) // local time
      
    location, err := time.LoadLocation("EST")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("ZONE : ", location, " Time : ", t.In(location)) // EST
  
    loc, _ := time.LoadLocation("UTC")
    now := time.Now().In(loc)
    fmt.Println("ZONE : ", loc, " Time : ", now) // UTC 
     
    loc, _ = time.LoadLocation("MST")
    now = time.Now().In(loc)
    fmt.Println("ZONE : ", loc, " Time : ", now) // MST
}

输出:

ZONE :  IST  Time :  2017-08-26 22:12:31.3763932 +0530 IST
ZONE :  EST  Time :  2017-08-26 11:42:31.3763932 -0500 EST
ZONE :  UTC  Time :  2017-08-26 16:42:31.3773933 +0000 UTC
ZONE :  MST  Time :  2017-08-26 09:42:31.3783934 -0700 MST
laravel中修改reuqest数组的方法

以下两种方式都可以。

1. request→merge()

$request->merge(['user_id' => 1]);

2. request→offsetSet()

$request->offsetSet('user_id', 1);
  • 当前日期:
  • 北京时间:
  • 时间戳:
  • 今年的第:18周
  • 我的 IP:18.116.118.216
农历
五行
冲煞
彭祖
方位
吉神
凶神
极简任务管理 help
+ 0 0 0
Task Idea Collect