fill the first two digits for year based on the current year when importing a two-digit year
This commit is contained in:
@@ -65,6 +65,35 @@ func FormatUnixTimeToLongDateTime(unixTime int64, timezone *time.Location) strin
|
||||
return t.Format(longDateTimeFormat)
|
||||
}
|
||||
|
||||
func FormatYearMonthDayToLongDateTime(year string, month string, day string) (string, error) {
|
||||
if len(year) == 2 {
|
||||
yearLast2Digits, err := StringToInt(year)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
currentYear := time.Now().Year()
|
||||
currentYearLast2Digits := currentYear % 100
|
||||
|
||||
if yearLast2Digits <= currentYearLast2Digits {
|
||||
year = IntToString(currentYear/100) + year
|
||||
} else {
|
||||
year = IntToString(currentYear/100-1) + year
|
||||
}
|
||||
}
|
||||
|
||||
if len(month) < 2 {
|
||||
month = "0" + month
|
||||
}
|
||||
|
||||
if len(day) < 2 {
|
||||
day = "0" + day
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s-%s-%s 00:00:00", year, month, day), nil
|
||||
}
|
||||
|
||||
// FormatUnixTimeToLongDateTimeInServerTimezone returns a textual representation of the unix time formatted by long date time format
|
||||
func FormatUnixTimeToLongDateTimeInServerTimezone(unixTime int64) string {
|
||||
return parseFromUnixTime(unixTime).Format(longDateTimeFormat)
|
||||
|
||||
@@ -46,6 +46,23 @@ func TestFormatUnixTimeToLongDateTime(t *testing.T) {
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
|
||||
func TestFormatYearMonthDayToLongDateTime(t *testing.T) {
|
||||
expectedValue := "2025-06-01 00:00:00"
|
||||
actualValue, err := FormatYearMonthDayToLongDateTime("25", "06", "01")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
expectedValue = "2025-06-01 00:00:00"
|
||||
actualValue, err = FormatYearMonthDayToLongDateTime("25", "6", "1")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
expectedValue = "1990-06-01 00:00:00"
|
||||
actualValue, err = FormatYearMonthDayToLongDateTime("90", "06", "01")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
|
||||
func TestFormatUnixTimeToLongDateTimeWithoutSecond(t *testing.T) {
|
||||
unixTime := int64(1617228083)
|
||||
utcTimezone := time.FixedZone("Test Timezone", 0) // UTC
|
||||
|
||||
@@ -9,9 +9,9 @@ var (
|
||||
longDateTimePattern = regexp.MustCompile("^([1-9][0-9]{3})-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[01]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")
|
||||
longDateTimeWithoutSecondPattern = regexp.MustCompile("^([1-9][0-9]{3})-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[01]) ([0-1][0-9]|2[0-3]):([0-5][0-9])$")
|
||||
longDatePattern = regexp.MustCompile("^([1-9][0-9]{3})-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[01])$")
|
||||
longOrShortYearMonthDayDatePattern = regexp.MustCompile("^([1-9][0-9]{3})[-/.']([1-9]|0[1-9]|1[0-2])[-/.']([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])$")
|
||||
longOrShortMonthDayYearDatePattern = regexp.MustCompile("^([1-9]|0[1-9]|1[0-2])[-/.']([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])[-/.']([1-9][0-9]{3})$")
|
||||
longOrShortDayMonthYearDatePattern = regexp.MustCompile("^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])[-/.']([1-9]|0[1-9]|1[0-2])[-/.']([1-9][0-9]{3})$")
|
||||
longOrShortYearMonthDayDatePattern = regexp.MustCompile("^(([1-9][0-9])?[0-9]{2})[-/.']([1-9]|0[1-9]|1[0-2])[-/.']([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])$")
|
||||
longOrShortMonthDayYearDatePattern = regexp.MustCompile("^([1-9]|0[1-9]|1[0-2])[-/.']([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])[-/.'](([1-9][0-9])?[0-9]{2})$")
|
||||
longOrShortDayMonthYearDatePattern = regexp.MustCompile("^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])[-/.']([1-9]|0[1-9]|1[0-2])[-/.'](([1-9][0-9])?[0-9]{2})$")
|
||||
)
|
||||
|
||||
// IsValidUsername reports whether username is valid
|
||||
|
||||
@@ -236,6 +236,11 @@ func TestIsValidYearMonthDayLongOrShortDateFormat_ValidFormat(t *testing.T) {
|
||||
actualValue := IsValidYearMonthDayLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
datetime = "24-09-01"
|
||||
expectedValue = true
|
||||
actualValue = IsValidYearMonthDayLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
datetime = "2024-09-1"
|
||||
expectedValue = true
|
||||
actualValue = IsValidYearMonthDayLongOrShortDateFormat(datetime)
|
||||
@@ -278,6 +283,11 @@ func TestIsValidMonthDayYearLongOrShortDateFormat_ValidFormat(t *testing.T) {
|
||||
actualValue := IsValidMonthDayYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
datetime = "09-01-24"
|
||||
expectedValue = true
|
||||
actualValue = IsValidMonthDayYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
datetime = "09-1-2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidMonthDayYearLongOrShortDateFormat(datetime)
|
||||
@@ -320,6 +330,11 @@ func TestIsValidDayMonthYearLongDateFormat_ValidLongDateFormat(t *testing.T) {
|
||||
actualValue := IsValidDayMonthYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
datetime = "01-09-24"
|
||||
expectedValue = true
|
||||
actualValue = IsValidDayMonthYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
datetime = "1-09-2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidDayMonthYearLongOrShortDateFormat(datetime)
|
||||
|
||||
Reference in New Issue
Block a user