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

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

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

问题

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

  1. type ImageData struct {
  2. Id primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
  3. Time time.Time `json:"time,omitempty" bson:"time,omitempty"`
  4. Path string `json:"path,omitempty" bson:"path,omitempty"`
  5. SizeBefore string `json:"sizebefore,omitempty" bson:"sizebefore,omitempty"`
  6. SizeAfter string `json:"sizeafter,omitempty" bson:"sizeafter,omitempty"`
  7. IsSuccess bool `json:"issuccess,omitempty" bson:"issuccess,omitempty"`
  8. }

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

  1. func CreateImageData(w http.ResponseWriter, r *http.Request) {
  2. w.Header().Set("Content-Type", "application/json")
  3. var image models.ImageData
  4. json.NewDecoder(r.Body).Decode(&image)
  5. image.Time = time.Now()
  6. collection := database.ImageData()
  7. ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
  8. result, _ := collection.InsertOne(ctx, image)
  9. json.NewEncoder(w).Encode(result)
  10. }

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

  1. [
  2. {
  3. "id": "6453e3a9b680e192e2fb82aa",
  4. "time": "2023-05-04T16:56:09.67Z",
  5. "path": "/result/test1.png",
  6. "sizebefore": "785KB",
  7. "sizeafter": "785KB"
  8. }
  9. ]

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

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

对不起,我的英语不好。

英文:

this is my struct located in different go file

  1. type ImageData struct {
  2. Id primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
  3. Time time.Time `json:"time,omitempty" bson:"time,omitempty"`
  4. Path string `json:"path,omitempty" bson:"path,omitempty"`
  5. SizeBefore string `json:"sizebefore,omitempty" bson:"sizebefore,omitempty"`
  6. SizeAfter string `json:"sizeafter,omitempty" bson:"sizeafter,omitempty"`
  7. IsSuccess bool `json:"issuccess,omitempty" bson:"issuccess,omitempty"`
  8. }

this is create function located in different go file

  1. func CreateImageData(w http.ResponseWriter, r *http.Request) {
  2. w.Header().Set("Content-Type", "application/json")
  3. var image models.ImageData
  4. json.NewDecoder(r.Body).Decode(&image)
  5. image.Time = time.Now()
  6. collection := database.ImageData()
  7. ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
  8. result, _ := collection.InsertOne(ctx, image)
  9. json.NewEncoder(w).Encode(result)
  10. }

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

  1. [
  2. {
  3. "id": "6453e3a9b680e192e2fb82aa",
  4. "time": "2023-05-04T16:56:09.67Z",
  5. "path": "/result/test1.png",
  6. "sizebefore": "785KB",
  7. "sizeafter": "785KB"
  8. }
  9. ]

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格式。

以下是一个示例:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. now := time.Now().UTC()
  8. formatted := now.Format(time.RFC3339Nano)
  9. fmt.Println(formatted)
  10. }
英文:

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:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. now := time.Now().UTC()
  8. formatted := now.Format(time.RFC3339Nano)
  9. fmt.Println(formatted)
  10. }

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:

确定