golang 中解决前端time 输出,后端mongodb中时间存储。

package mask

import (
"fmt"
"time" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
) // Timestamp extension time
type Timestamp struct {
Time time.Time
} const (
jsonLayout = "2006-01-02 15:04:05"
) // Now returns the current local time.
func Now() Timestamp {
return Timestamp{
Time: time.Now(),
}
} // UnmarshalBSON unmarshal bson
func (t *Timestamp) UnmarshalBSON(data []byte) (err error) {
var d bson.D
err = bson.Unmarshal(data, &d)
if err != nil {
return err
}
if v, ok := d.Map()["t"]; ok {
t.Time = time.Time{}
return t.Time.UnmarshalText([]byte(v.(string)))
}
return fmt.Errorf("key 't' missing")
} // MarshalBSON marshal bson
func (t Timestamp) MarshalBSON() ([]byte, error) {
txt, err := t.Time.MarshalText()
if err != nil {
return nil, err
}
b, err := bson.Marshal(map[string]string{"t": string(txt)})
return b, err
} // MarshalBSONValue marshal bson value
func (t *Timestamp) MarshalBSONValue() (bsontype.Type, []byte, error) {
fmt.Println(t)
b, err := bson.Marshal(t)
return bson.TypeEmbeddedDocument, b, err
} // UnmarshalJSON unmarshal json
func (t *Timestamp) UnmarshalJSON(data []byte) (err error) {
if len(data) == 0 || string(data) == "" || string(data) == `""` {
return
}
now, err := time.ParseInLocation(`"`+jsonLayout+`"`, string(data), time.Local)
*t = Timestamp{
Time: now,
}
return
} // MarshalJSON marshal json
func (t Timestamp) MarshalJSON() ([]byte, error) { b := make([]byte, 0, len(jsonLayout)+2)
b = append(b, '"')
b = time.Time(t.Time).AppendFormat(b, jsonLayout)
b = append(b, '"')
return b, nil
}

golang time json mongodb 时间处理的更多相关文章

  1. Java json设置时间格式,Jackson设置时间格式,json设置单引号

    Java json设置时间格式,Jackson设置时间格式,json设置单引号 >>>>>>>>>>>>>>> ...

  2. C#应用Newtonsoft.Json.dll,控制json的时间格式

    原文:C#应用Newtonsoft.Json.dll,控制json的时间格式 var aIsoDateTimeConverter = new IsoDateTimeConverter();aIsoDa ...

  3. .NET 自定义Json序列化时间格式

    .NET 自定义Json序列化时间格式 Intro 和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认序列化规 ...

  4. JSON.Net 自定义Json序列化时间格式

    JSON.Net 自定义Json序列化时间格式 Intro 和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认 ...

  5. 48 【golang】json的效率

    本文将主要做如下几方面的测试: 1,构造一个[100]struct的数组,然后来测试它的json编码后的字符串 或者([]byte),首先关心它的功能是否正常: 2,在很早之前,我们在使用golang ...

  6. Go_14:GoLang中 json、map、struct 之间的相互转化

    1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...

  7. Golang 处理 Json(二):解码

    golang 编码 json 还比较简单,而解析 json 则非常蛋疼.不像 PHP 一句 json_decode() 就能搞定.之前项目开发中,为了兼容不同客户端的需求,请求的 content-ty ...

  8. Golang 处理 Json(一):编码

    JSON 是一种数据格式描述语言.以 key 和 value 构成的哈系结构,类似 Javascript 中的对象,python 中的字典.通常 json 格式的 key 是字符串,其值可以是任意类型 ...

  9. GoLang中 json、map、struct 之间的相互转化

    1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...

随机推荐

  1. layui时间控件选择时间范围

    layui.use([ 'laydate'], function(){ var $ = layui.$; var laydate = layui.laydate; var max = ${nowYea ...

  2. Xcode真机报错clang: error: linker command failed with exit code 1 (use -v to see invocation)

    出现这种错误,如下图所示,搜索bitcode,置为NO即可.

  3. Servlet(3):Cookie

    概念 Cookie是存储在客户端计算机上的文本文件,并保留了各种跟踪信息.Java Servlet支持HTTP Cookie. 识别返回用户包括三个步骤: (1) 服务器脚本向浏览器发送一组Cooki ...

  4. configparser模块 subprocess 模块,xlrd 模块(表格处理)

    今日内容: 1.configparser模块 2.subprocess模块 3.xlrd(读),xlwt(写) 表格处理 configparser模块 import configparser # co ...

  5. Leetcode之动态规划(DP)专题-1025. 除数博弈(Divisor Game)

    Leetcode之动态规划(DP)专题-1025. 除数博弈(Divisor Game) 爱丽丝和鲍勃一起玩游戏,他们轮流行动.爱丽丝先手开局. 最初,黑板上有一个数字 N .在每个玩家的回合,玩家需 ...

  6. 一加手机2 进入recovery 模式无法挂载USB存储器通过命令窗口上传ROM镜像

    试过3.0.3-0和3.0.3-1的recovery都无法使用“挂载USB大容量存储器”模式,这肯定让很多清掉系统(就是system分区)的小伙伴无力吐槽,因为这样子rom就无法在rec里面从电脑拷到 ...

  7. 【Linux开发】linux设备驱动归纳总结(四):5.多处理器下的竞态和并发

    linux设备驱动归纳总结(四):5.多处理器下的竞态和并发 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  8. TOEFL | 听力题型

    通常是2个对话,4个讲座,但可能会有加试: 2~3 conversations  5 Questions/Each 4~6 lectures            6 Questions/Each 正 ...

  9. flask_migrate 的应用

    怎么查看的命令: python manage.py --help 使用flask_migrate的注意事项:

  10. AKKA文档2.2(java)——术语,概念

    原文:http://doc.akka.io/docs/akka/2.3.6/general/terminology.html 译者:吴京润 本章我们试图建立一个通用的术语列表,用来定义有关并发和分布式 ...