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. ConstraintLayout的简单介绍和使用

    ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在去年的I/O大会上重点宣传的一个功能.我们都知道,在传统的Android开发当中,界面基本 ...

  2. MySQL 将 字符串 转为 整数

    MySQL 将 字符串 转为 整数 1.CAST(epr AS type) 1)type 为 SIGNED " AS SIGNED); 效果如下: 2)type 为 UNSIGNED &qu ...

  3. django基础窗口类的使用

    django基础窗口form表单的运用 具体效果图如下: 首先确定表单中的数据集,先自己创建一个forms.py或者在原来的models.py中添加: 1代码如下 class ContactForm( ...

  4. H5本地存储技术

    H5 Web存储技术 前言 web存储技术在初期的时候被定义为HTML5的一部分作为其API.后来被独立出来作为一份独立的标准. web存储标准包含localStorage对象和sessionStor ...

  5. PJzhang:python基础入门的7个疗程-four

    猫宁!!! 参考链接:易灵微课-21天轻松掌握零基础python入门必修课-售价29元人民币 https://www.liaoxuefeng.com/wiki/1016959663602400 第十天 ...

  6. 大数据技术之kettle

    大数据技术之kettle 第1章            kettle概述 1.1    什么是kettle kettle是一款开源的ETL工具,纯java编写,可以在Windows.Linux.Uni ...

  7. 解决Iframe跨域高度自适应,利用window.postMessage()实现跨域消息传递页面高度(JavaScript)

    在iframe跨域引用高度自适应这块写的js方式都试了不管用,最终使用的是window.postMessage() 跨域获取高度 传递信息 1.首先,在主页面上使用iframe引入子页面:也就是A.h ...

  8. aws和ufile挂载数据盘EBS

    aws的话挂载的ebs需要格式化,参考:https://docs.aws.amazon.com/zh_cn/AWSEC2/latest/UserGuide/ebs-using-volumes.html ...

  9. Linux 概念与快捷方式

    概念 何为shell Shell 是指"提供给使用者使用界面"的软件(命令解析器),类似于 DOS 下的 command(命令行)和后来的 cmd.exe .普通意义上的 Shel ...

  10. [转帖]IntelliJ IDEA 2018.3.3破解方法

    IntelliJ IDEA 2018.3.3破解方法 https://blog.csdn.net/qq_42862882/article/details/86477495 验证了下 也可以激活.   ...