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

feat(os/gtime): SetStringLayout #4130

Closed
wants to merge 5 commits into from

Conversation

yzy613
Copy link
Contributor

@yzy613 yzy613 commented Jan 21, 2025

func ExampleSetStringLayout() {
	gtime.SetStringLayout(time.RFC3339)
	fmt.Println(gtime.New("2025-01-21 14:08:05").String())

	// Output:
	// 2025-01-21T14:08:05+08:00
}

func ExampleSetStringISO8601() {
	gtime.SetStringISO8601()
	fmt.Println(gtime.New("2025-01-21 14:10:12").String())

	// Output:
	// 2025-01-21T14:10:12+08:00
}

func ExampleSetStringRFC822() {
	gtime.SetStringRFC822()
	fmt.Println(gtime.New("2025-01-21 14:11:32").String())

	// Output:
	// Tue, 21 Jan 25 14:11 CST
}

@cyjaysong
Copy link
Contributor

合理使用Format, Layout, 你的这种方式可能会导致其他依赖gtime.String() 的组件或工具得到的预期值不一致。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Use Format and Layout properly. Your approach may cause other components or tools that rely on gtime.String() to get inconsistent expected values.

@yzy613
Copy link
Contributor Author

yzy613 commented Jan 22, 2025

合理使用Format, Layout, 你的这种方式可能会导致其他依赖gtime.String() 的组件或工具得到的预期值不一致。

有没有可能我就是需要让依赖gtime.Time.String()的值改变,例如gtime.Time.MarshalJSON()。

并且如果你不使用这个全局函数,他的默认行为和以往版本保持一致。

@cyjaysong
Copy link
Contributor

cyjaysong commented Jan 22, 2025

@yzy613 实在是想要你可以封装成StringForRFC3339(),StringForISO8601(),StringForRFC822()

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@yzy613 I really want you to encapsulate it into StringForRFC3339(), StringISO8601(), StringRFC822()

@yzy613
Copy link
Contributor Author

yzy613 commented Jan 22, 2025

@yzy613 实在是想要你可以封装成StringForRFC3339(),StringForISO8601(),StringForRFC822()

但他并不会影响MarshalJSON()的行为

@cyjaysong
Copy link
Contributor

那你可以SetStringLayout声明在gtime.Time上,而不是全局函数。或者基于gtime.Time定义一个自定义结构体,复写String(),比如

type Ctime gtime.Time

func (c Ctime) String() string {
	return time.Time(c).Layout(time.RFC3339)
}

@cyjaysong
Copy link
Contributor

全局设置时间格式会导致其他依赖原时间格式的代码出现不可预知的错误。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Setting the time format globally will cause unpredictable errors in other code that relies on the original time format.

@yzy613
Copy link
Contributor Author

yzy613 commented Jan 22, 2025

那你可以SetStringLayout声明在gtime.Time上,而不是全局函数。或者基于gtime.Time定义一个自定义结构体,复写String(),比如

type Ctime gtime.Time

func (c Ctime) String() string {
	return time.Time(c).Layout(time.RFC3339)
}

还有一个问题就是,gtime.Time.String()没有时区信息,在跨时区的数据交换上,不好用。

@cyjaysong
Copy link
Contributor

这种建议保留在你自己的分支,定期同步源分支就好

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


It is recommended to keep this in your own branch, and just synchronize the source branch regularly.

@yzy613
Copy link
Contributor Author

yzy613 commented Jan 22, 2025

这种建议保留在你自己的分支,定期同步源分支就好

那我们为什么不试试将依赖原格式的代码找到并解耦呢?

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


It is recommended to keep this in your own branch, and just synchronize the source branch regularly

So why don't we try to find and decouple the code that relies on the original format?

@gqcn
Copy link
Member

gqcn commented Jan 22, 2025

@yzy613 感谢参与咱们的开源项目共建!其实在时间这块,不建议全局改动gtime这种格式化的逻辑,而是不同的场景根据自己的需要自行格式化时间字符串。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@yzy613 Thank you for participating in our open source project! In fact, when it comes to time, it is not recommended to globally change the formatting logic of gtime. Instead, different scenarios format the time string according to their own needs.

@yzy613
Copy link
Contributor Author

yzy613 commented Jan 22, 2025

@yzy613 感谢参与咱们的开源项目共建!其实在时间这块,不建议全局改动gtime这种格式化的逻辑,而是不同的场景根据自己的需要自行格式化时间字符串。

那时区输出就只能再wrap一层做String()和MarshalJSON()咯(好不优雅XP

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@yzy613 Thank you for participating in our open source project! In fact, when it comes to time, it is not recommended to globally change the formatting logic of gtime. Instead, different scenarios format the time string according to their own needs.

At that time, the zone output can only be done by wrapping one layer to String() and MarshalJSON() (so inelegant XP

@yzy613
Copy link
Contributor Author

yzy613 commented Jan 23, 2025

懂了,是因为MySQL不支持其他时间格式

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Got it, it’s because MySQL does not support other time formats

@yzy613 yzy613 closed this Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants