美文网首页Go
Go 日期和时间格式 与nginx服务部署

Go 日期和时间格式 与nginx服务部署

作者: Super淳语 | 来源:发表于2021-09-10 15:35 被阅读0次

    t := time.Now()         // 2021-09-09 17:38:12.246295 +0800 CST m=+0.000161927 在 Go 中,可以使用time.Now()由time包提供的来确定当前时间。
    t := time.Now().UTC()   // 2021-09-09 09:38:38.031648 +0000 UTC 系统本地时间
    t := time.Now().Unix()  // 1631180346 时间戳
    t := time.Now().Format("2006-01-02 15:04:05") // 2021-09-09 17:42:51
解释如下:Go 的时间格式是独一无二的,与您在其他语言中所做的不同。Go 没有使用传统格式来打印日期,20060102150405而是使用参考日期,这看起来毫无意义但实际上是有原因的,因为它1 2 3 4 5 6在 Posixdate命令中:
Mon Jan 2 15:04:05 -0700 MST 2006
0   1   2  3  4  5              6
中间的时区本来是7Mon Jan 2 03:04:05 -0600 MST 2007顺便说一下,不确定他们为什么不选择)

有趣的历史参考:https : //github.com/golang/go/issues/444

使用格式常量

Go 在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"
)
像这样使用它们
t := time.Now()
fmt.Println(t.Format(time.ANSIC))
反向代理主要负责静态文件处理和负载均衡,直接复制下面的配置。
proxy_pass 为 Gin 服务器的监听端口

server {
    server_name www.domain.com;
    listen 80; 
    root /data/mix/public;

    location / {
        proxy_http_version 1.1;
        proxy_set_header Connection "keep-alive";
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        if (!-f $request_filename) {
             proxy_pass http://127.0.0.1:8080;
        }
    }
}

在代码中通过读取 c.Request.Header.Get("X-Real-IP") 或者 c.Request.Header.Get("X-Forwarded-For") 来获取客户端的真实 IP

年月日 转时间戳
        //获取当前时区
        loc, _ := time.LoadLocation("Local")

        //日期当天0点时间戳(拼接字符串)
        startDate := date + "_00:00:00"
        startTime, _ := time.ParseInLocation("2006-01-02_15:04:05", startDate, loc)

        //日期当天23时59分时间戳
        endDate := date + "_23:59:59"
        end, _ := time.ParseInLocation("2006-01-02_15:04:05", endDate, loc)

        //返回当天0点和23点59分的时间戳
        return startTime.Unix(), end.Unix()
时间戳 转年月日期
func Time2DateTime(unixTime int64) (curDateTime string) {
    _, _ = time.LoadLocation("Asia/Shanghai")
    tm := time.Unix(unixTime, 0)
    dateTime := tm.Format("2006-01-02 15:04:05")

    return dateTime
}

相关文章

网友评论

    本文标题:Go 日期和时间格式 与nginx服务部署

    本文链接:https://www.haomeiwen.com/subject/drqwwltx.html