dromara / carbon

A simple, semantic and developer-friendly golang package for time
https://pkg.go.dev/github.com/golang-module/carbon/v2
MIT License
4.77k stars 238 forks source link

Starting from v2.3.0, after removing the series of methods such as DateTime, it is extremely inconvenient to insert the birthday (Y-m-d) field with gorm now. What is the best practice #217

Closed liaohongxing closed 9 months ago

liaohongxing commented 9 months ago

从v2.3.0开始 , 移除DateTime等系列方法后 ,全部改用 Carbon后,现在用gorm插入包含生日(Y-m-d)字段,前端传Y-m-d , 后台一直报 layout "2006-01-02 15:04:05" 错误 ,设置了carbon:"type:date" 依旧不行,同样的问题存在于查询一个列表的情况 ,似乎json序列化,反序列化,不会识别设置的Tag参数 ,极为不便,最佳实践是什么

Issues-translate-bot commented 9 months ago

The issue body's language is not English, translate it automatically, please use English next time. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Starting from v2.3.0, after removing the DateTime and other series of methods, now use gorm to insert the field containing birthday (Y-m-d). The front-end passes Y-m-d, and the background keeps reporting the layout "2006-01-02 15:04:05" error. Carbon is set. :"type:date" still doesn't work. The same problem exists when querying a list. It seems that json serialization and deserialization will not recognize the set Tag parameters, which is extremely inconvenient. What is the best practice? @@=== =Starting from v2.3.0, after removing DateTime and other series of methods, now use gorm to insert the birthday (Y-m-d) field. It is extremely inconvenient. What is the best practice?

gouguoyin commented 9 months ago

https://github.com/golang-module/carbon/blob/master/README.cn.md#json

image
gouguoyin commented 9 months ago

建议在项目入口文件(如 main.go)里设置全局模板格式、时区等

carbon.SetDefault(carbon.Default{
  Layout: carbon.RFC3339Layout,
  Timezone: carbon.PRC,
  WeekStartsAt: carbon.Sunday,
  Locale: "zh-CN",
})
Issues-translate-bot commented 9 months ago

The issue body's language is not English, translate it automatically, please use English next time. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


It is recommended to set the global template format, time zone, etc. in the project entry file (such as main.go)

carbon.SetDefault(carbon.Default{
Layout: carbon.RFC3339Layout,
Timezone: carbon.PRC,
WeekStartsAt: carbon.Sunday,
Locale: "zh-CN",
})
liaohongxing commented 9 months ago

这个SetDefault 是很好的,但是略显鸡肋,部分非标准layout还是没有之前用起来顺畅,要是能解决json序列化,反序列能读取设置的非标准tag就好了

Issues-translate-bot commented 9 months ago

The issue body's language is not English, translate it automatically, please use English next time. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


This SetDefault is very good, but it is a bit useless. Some non-standard layouts are still not as smooth to use as before. It would be great if json serialization can be solved and the non-standard tags set can be read by deserialization.

gouguoyin commented 9 months ago

这个SetDefault 是很好的,但是略显鸡肋,部分非标准layout还是没有之前用起来顺畅,要是能解决json序列化,反序列能读取设置的非标准tag就好了

之前是预置的固定类型,没有预置的就没办法处理,现在使用 SetDefault 把控制权交给了用户,SetDefault 可以设置任何标准或非标准的 layout,如

carbon.SetDefault(carbon.Default{
  Layout: "2006/1/2",
})
Issues-translate-bot commented 9 months ago

The issue body's language is not English, translate it automatically, please use English next time. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


This SetDefault is very good, but it is a bit useless. Some non-standard layouts are still not as smooth to use as before. It would be great if json serialization could be solved and the non-standard tags set could be read by deserialization.

It used to be a preset fixed type. If there is no preset, there is no way to process it. Now use SetDefault to give control to the user. SetDefault can set any standard or non-standard layout

gouguoyin commented 9 months ago

如果要使用 tag 也可以,不过使用了泛型,性能会受到影响。使用 tag 序列化或者反序列化前必须调用 carbon.loadTag 方法载入 tag

image
gouguoyin commented 9 months ago

总结起来 1、如果结构体里时间字段输出格式都一样,使用 carbon.SetDefault 方法(当然也可以用 tag ,不过不推荐) 2、如果结构体里时间字段输出格式不一样,只能使用 tag 配合 carbon.LoadTag 方法

Issues-translate-bot commented 9 months ago

The issue body's language is not English, translate it automatically, please use English next time. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


In short

  1. If the output format of the time fields in the structure is the same, use carbon.SetDefault
  2. If the output format of the time field in the structure is different, use tag with carbon.LoadTag