code refactor

This commit is contained in:
MaysWind
2025-07-06 15:50:53 +08:00
parent a54275d307
commit 82b98eca95
9 changed files with 239 additions and 191 deletions
+9 -4
View File
@@ -13,7 +13,7 @@ const (
longDateTimeFormat = "2006-01-02 15:04:05"
longDateTimeWithTimezoneFormat = "2006-01-02 15:04:05Z07:00"
longDateTimeWithTimezoneFormat2 = "2006-01-02 15:04:05 Z0700"
longDateTimeWithTimezoneRFC3389Format = "2006-01-02T15:04:05Z07:00"
longDateTimeWithTimezoneRFC3339Format = "2006-01-02T15:04:05Z07:00"
longDateTimeWithoutSecondFormat = "2006-01-02 15:04"
shortDateTimeFormat = "2006-1-2 15:4:5"
yearMonthDateTimeFormat = "2006-01"
@@ -77,15 +77,15 @@ func FormatUnixTimeToLongDateTimeWithTimezone(unixTime int64, timezone *time.Loc
return t.Format(longDateTimeWithTimezoneFormat)
}
// FormatUnixTimeToLongDateTimeWithTimezoneRFC3389Format returns a textual representation of the unix time formatted by long date time with timezone RFC 3389 format
func FormatUnixTimeToLongDateTimeWithTimezoneRFC3389Format(unixTime int64, timezone *time.Location) string {
// FormatUnixTimeToLongDateTimeWithTimezoneRFC3339Format returns a textual representation of the unix time formatted by long date time with timezone RFC 3339 format
func FormatUnixTimeToLongDateTimeWithTimezoneRFC3339Format(unixTime int64, timezone *time.Location) string {
t := parseFromUnixTime(unixTime)
if timezone != nil {
t = t.In(timezone)
}
return t.Format(longDateTimeWithTimezoneRFC3389Format)
return t.Format(longDateTimeWithTimezoneRFC3339Format)
}
func FormatYearMonthDayToLongDateTime(year string, month string, day string) (string, error) {
@@ -229,6 +229,11 @@ func ParseFromLongDateTimeWithTimezone2(t string) (time.Time, error) {
return time.Parse(longDateTimeWithTimezoneFormat2, t)
}
// ParseFromLongDateTimeWithTimezoneRFC3339Format parses a formatted string in long date time RFC 3378 format
func ParseFromLongDateTimeWithTimezoneRFC3339Format(t string) (time.Time, error) {
return time.Parse(longDateTimeWithTimezoneRFC3339Format, t)
}
// ParseFromLongDateTimeWithoutSecond parses a formatted string in long date time format (no second)
func ParseFromLongDateTimeWithoutSecond(t string, utcOffset int16) (time.Time, error) {
timezone := time.FixedZone("Timezone", int(utcOffset)*60)
+12 -3
View File
@@ -46,17 +46,17 @@ func TestFormatUnixTimeToLongDateTimeWithTimezone(t *testing.T) {
assert.Equal(t, expectedValue, actualValue)
}
func TestFormatUnixTimeToLongDateTimeWithTimezoneRFC3389Format(t *testing.T) {
func TestFormatUnixTimeToLongDateTimeWithTimezoneRFC3339Format(t *testing.T) {
unixTime := int64(1617228083)
utcTimezone := time.FixedZone("Test Timezone", 0) // UTC
utc8Timezone := time.FixedZone("Test Timezone", 28800) // UTC+8
expectedValue := "2021-03-31T22:01:23Z"
actualValue := FormatUnixTimeToLongDateTimeWithTimezoneRFC3389Format(unixTime, utcTimezone)
actualValue := FormatUnixTimeToLongDateTimeWithTimezoneRFC3339Format(unixTime, utcTimezone)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "2021-04-01T06:01:23+08:00"
actualValue = FormatUnixTimeToLongDateTimeWithTimezoneRFC3389Format(unixTime, utc8Timezone)
actualValue = FormatUnixTimeToLongDateTimeWithTimezoneRFC3339Format(unixTime, utc8Timezone)
assert.Equal(t, expectedValue, actualValue)
}
@@ -228,6 +228,15 @@ func TestParseFromLongDateTimeWithTimezone2(t *testing.T) {
assert.Equal(t, expectedValue, actualValue)
}
func TestParseFromLongDateTimeWithTimezoneRFC3339Format(t *testing.T) {
expectedValue := int64(1617238883)
actualTime, err := ParseFromLongDateTimeWithTimezoneRFC3339Format("2021-04-01T06:01:23+05:00")
assert.Equal(t, nil, err)
actualValue := actualTime.Unix()
assert.Equal(t, expectedValue, actualValue)
}
func TestParseFromLongDateTimeWithoutSecond(t *testing.T) {
expectedValue := int64(1691947440)
actualTime, err := ParseFromLongDateTimeWithoutSecond("2023-08-13 17:24", 0)