Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os/gtime: The gtime.Time format returned by the interface is a standard format, and I hope you can customize the configuration #4205

Open
sunxiaolong321 opened this issue Mar 15, 2025 · 3 comments
Labels

Comments

@sunxiaolong321
Copy link

Is your feature request related to a problem?

Option Yes

Describe the solution you'd like

如题,希望可以自定义配置v2规范路由中gtime.Time返回数据的格式,以便支持更广泛的功能,比如时区国际化。

Describe alternatives you've considered

现在返回的格式是:2025-02-24 15:54:26,希望可以在设置中添加自定义配置项自定义配置返回的格式,比如可以返回毫秒,纳秒或者年月日等格式

Additional

No response

@Issues-translate-bot Issues-translate-bot changed the title os/gtime: 目前接口返回的gtime.Time格式是标准格式,希望可以自定义配置 os/gtime: The gtime.Time format returned by the interface is a standard format, and I hope you can customize the configuration Mar 15, 2025
@y1jiong
Copy link
Contributor

y1jiong commented Mar 15, 2025

Related #4130, Discussion #4173

It is recommended to wrap custom time type for now.

@wln32
Copy link
Member

wln32 commented Mar 19, 2025

@sunxiaolong321 @y1jiong @hailaz @gqcn
其实可以有一个方案,在gtime.Time结构体内部保存一个格式化函数,同时并为gtime.Time提供一个api用来设置格式化函数,比如

package gtime
// gtime.Time结构体
type Time struct{
    // ...
    // 原来的字段
   
    // 添加一个格式化函数字段
   f  func(*Time)([]byte,error)
}

func(t*Time)SetFormatFunc(f func(*Time)([]byte,error)){
    t.f= f
}

func(t *Time)MarshalJSON()([]byte,error){
   if t.f != nil {
      return t.f(t)
  }
  // 原逻辑
}

/////////////////////////////////////////////////////////////////
// 然后在ghttp包提供对gtime.Time类型设置格式化的能力
package ghttp

var gtimeTimeMarshalJSONFunc func(*gtime.Time)([]byte,error)

func SetGtimeTimeMarshalJSONFunc(f func(*gtime.Time)([]byte,error)){
   gtimeTimeMarshalJSONFunc = f
}

// 同时可以利用gconv中的converter特性,hook所有的gtime.Time类型,进行一些设置

func convertGtimeTime(src any,dst reflect.Value)error{
   dv:=dst.Addr().Interface().(*gtime.Time)
    switch x:=src.(type){
      case []byte: // 这里只做一个示例,不用太关心src类型
           *dv = *gtime.New(src)
           // 对gtime.Time类型设置格式化函数
           dv.SetFormatFunc(gtimeTimeMarshalJSONFunc )
      default:
         return fmt.Error("不支持从%T转换到gtime.Time",src)
  } 

}
// 注册转换函数
converter.Register(convertGtimeTime,reflect.TypeOf(gtime.Time{}))

// 如果进一步细分的话,可以对gtime.Time字段做tag标记,比如
// iso8601
// RFC3339
// RFC3339Nano

// 根据以上tag的值,设置不同的格式化函数,但我认为没什么必要,一个项目用一种就差不多了

其实上述ghttp包的逻辑,可以不用gf来提供,gf只需要提供gtime包的逻辑即可,即SetFormatFunc函数

@kuafuRace
Copy link
Contributor

kuafuRace commented Apr 2, 2025

carbon uses generics to customize the output format when encoding JSON , only need to implement SetLayout method of LayoutFactory interface, or implement SetFormat method of FormatFactory interface

type RFC3339Layout string
func (t CustomerLayout) SetLayout() string {
	return carbon.RFC3339Layout
}

type ISO8601Format string
func (t CustomerFormat) SetFormat() string {
	return carbon.ISO8601Format
}

type User struct {
	Customer1 carbon.LayoutType[RFC3339Layout] `json:"customer1"`
	Customer2 carbon.FormatType[ISO8601Format] `json:"customer2"`
}

var user User

c := carbon.Parse("2020-08-05 13:14:15")

user.Customer1 = carbon.NewLayoutType[RFC3339Layout](c)
user.Customer2 = carbon.NewFormatType[ISO8601Format](c)

data, err := json.Marshal(&user)
if err != nil {
	// 错误处理
	log.Fatal(err)
}
fmt.Printf("%s\n", data)
// 输出
{"customer1":"2020-08-05T13:14:15Z", "customer2":"2020-08-05T13:14:15+00:00"}

var person User
err := json.Unmarshal(data, &person)
if err != nil {
	// 错误处理
	log.Fatal(err)
}

fmt.Printf("person: %+v\n", person)
// 输出
person: {Customer1:2020-08-05T13:14:15Z Customer2:2020-08-05T13:14:15+00:00}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants