Coder Social home page Coder Social logo

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 about carbon HOT 11 CLOSED

liaohongxing avatar liaohongxing commented on June 12, 2024
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

from carbon.

Comments (11)

Issues-translate-bot avatar Issues-translate-bot commented on June 12, 2024

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?

from carbon.

gouguoyin avatar gouguoyin commented on June 12, 2024

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

from carbon.

gouguoyin avatar gouguoyin commented on June 12, 2024

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

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

from carbon.

Issues-translate-bot avatar Issues-translate-bot commented on June 12, 2024

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",
})

from carbon.

liaohongxing avatar liaohongxing commented on June 12, 2024

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

from carbon.

Issues-translate-bot avatar Issues-translate-bot commented on June 12, 2024

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.

from carbon.

gouguoyin avatar gouguoyin commented on June 12, 2024

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

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

carbon.SetDefault(carbon.Default{
  Layout: "2006/1/2",
})

from carbon.

Issues-translate-bot avatar Issues-translate-bot commented on June 12, 2024

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

from carbon.

gouguoyin avatar gouguoyin commented on June 12, 2024

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

from carbon.

gouguoyin avatar gouguoyin commented on June 12, 2024

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

from carbon.

Issues-translate-bot avatar Issues-translate-bot commented on June 12, 2024

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

from carbon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.