mongo分解日期对象设置时区

func main() {
    str := "2018-12-19 09"
    t, err := time.ParseInLocation("2006-01-02 03", str, time.Local)
    if err != nil {
        log.Fatal(err)
    }
    log.Println(t)
    log.Println(time.Unix(t.Unix(), 0))
    if err := testDBC.Insert(M{"data": t}); err != nil {
        log.Fatal(err)
    }
    res := map[string]interface{}{}
    if err := testDBC.Pipe([]M{
        {"$group": M{
            "_id": M{
                "year":  M{"$year": M{"date": "$data", "timezone": "+0800"}},
                "month": M{"$month": M{"date": "$data", "timezone": "+0800"}},
                "day":   M{"$dayOfMonth": M{"date": "$data", "timezone": "+0800"}},
                "hour":  M{"$hour": M{"date": "$data", "timezone": "+0800"}},
            },
            "date": M{"$last": "$data"}},
        },
    }).One(&res); err != nil {
        log.Println(err)
    }
    log.Println(res)
}
  2018-12-19 09:00:00 +0800 CST
  2018-12-19 09:00:00 +0800 CST
  map[date:2018-12-19 09:00:00 +0800 CST _id:map[month:12 day:19 hour:9 year:2018]]

你可能感兴趣的:(mongo分解日期对象设置时区)