使用Golang和API将时间保存到MongoDB中,但时间不匹配。

huangapple go评论72阅读模式
英文:

saving time to mongodb with golang with api, but the time is not matched

问题

这是我的结构体,位于不同的Go文件中:

type ImageData struct {
	Id         primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
	Time       time.Time          `json:"time,omitempty" bson:"time,omitempty"`
	Path       string             `json:"path,omitempty" bson:"path,omitempty"`
	SizeBefore string             `json:"sizebefore,omitempty" bson:"sizebefore,omitempty"`
	SizeAfter  string             `json:"sizeafter,omitempty" bson:"sizeafter,omitempty"`
	IsSuccess  bool               `json:"issuccess,omitempty" bson:"issuccess,omitempty"`
}

这是位于不同Go文件中的创建函数:

func CreateImageData(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	var image models.ImageData

	json.NewDecoder(r.Body).Decode(&image)
	image.Time = time.Now()

	collection := database.ImageData()
	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
	result, _ := collection.InsertOne(ctx, image)

	json.NewEncoder(w).Encode(result)
}

当我创建一个ImageData结构体并保存到数据库时,时间不匹配。这是我MongoDB中的数据:

[
    {
        "id": "6453e3a9b680e192e2fb82aa",
        "time": "2023-05-04T16:56:09.67Z",
        "path": "/result/test1.png",
        "sizebefore": "785KB",
        "sizeafter": "785KB"
    }
]

但是我的真实时间是2023-05-04T23:57:00。

如何修复这个问题以使时间准确?

对不起,我的英语不好。

英文:

this is my struct located in different go file

type ImageData struct {
	Id   primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
	Time time.Time	`json:"time,omitempty" bson:"time,omitempty"`
	Path	string	`json:"path,omitempty" bson:"path,omitempty"`
	SizeBefore	string	`json:"sizebefore,omitempty" bson:"sizebefore,omitempty"`
	SizeAfter	string	`json:"sizeafter,omitempty" bson:"sizeafter,omitempty"`
	IsSuccess	bool	`json:"issuccess,omitempty" bson:"issuccess,omitempty"`
}

this is create function located in different go file

func CreateImageData(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	var image models.ImageData

	json.NewDecoder(r.Body).Decode(&image)
	image.Time = time.Now()

	collection := database.ImageData()
	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
	result, _ := collection.InsertOne(ctx, image)

	json.NewEncoder(w).Encode(result)
}

when i create a ImageData struct and save to database the time isnt match
this is data in my mongodb

[
    {
        "id": "6453e3a9b680e192e2fb82aa",
        "time": "2023-05-04T16:56:09.67Z",
        "path": "/result/test1.png",
        "sizebefore": "785KB",
        "sizeafter": "785KB"
    }
]

but my real time is 2023-05-04T23:57:00

how to fix this to make the time accurate

sorry for bad english

答案1

得分: 1

你可以使用"time"包和"Format"函数将日期时间转换为有效的格式。

请注意,你需要更改日期时间的格式,因为MongoDB使用UTC格式。

以下是一个示例:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now().UTC()
	formatted := now.Format(time.RFC3339Nano)
	fmt.Println(formatted)
}
英文:

You can use the 'time' package and the 'Format' function to convert the datetime into a valid format.

Please note that you need to change the format of the datetime as MongoDB uses UTC format.

Here's an example:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now().UTC()
	formatted := now.Format(time.RFC3339Nano)
	fmt.Println(formatted)
}

huangapple
  • 本文由 发表于 2023年5月5日 01:00:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76175670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定