2022-03-24 18:30:59 +01:00
|
|
|
package txt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
// Empty checks whether a string represents an empty, unset, or undefined value.
|
2022-07-22 12:38:25 +02:00
|
|
|
func Empty(s string) bool {
|
2022-09-30 19:15:10 +02:00
|
|
|
if s == "" {
|
|
|
|
return true
|
2023-02-24 15:53:59 +01:00
|
|
|
} else if s = strings.Trim(s, "%* "); s == "" || s == "0" || s == "-1" || DateTimeDefault(s) {
|
2022-09-30 19:15:10 +02:00
|
|
|
return true
|
|
|
|
} else if s = strings.ToLower(s); s == "nil" || s == "null" || s == "nan" {
|
2022-03-24 18:30:59 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:15:10 +02:00
|
|
|
return false
|
2022-03-24 18:30:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotEmpty tests if a string does not represent an empty/invalid value.
|
|
|
|
func NotEmpty(s string) bool {
|
2022-07-22 12:38:25 +02:00
|
|
|
return !Empty(s)
|
|
|
|
}
|
|
|
|
|
2023-02-24 15:53:59 +01:00
|
|
|
// EmptyDateTime tests if the string is empty or matches an unknown time pattern.
|
|
|
|
func EmptyDateTime(s string) bool {
|
2022-07-22 12:38:25 +02:00
|
|
|
switch s {
|
2023-02-24 15:53:59 +01:00
|
|
|
case "", "-", ":", "z", "zz", "Z", "nil", "null", "none", "nan", "NaN":
|
|
|
|
return true
|
|
|
|
case "0", "00", "0000", "0000:00:00", "00:00:00", "0000-00-00", "00-00-00":
|
2022-07-22 12:38:25 +02:00
|
|
|
return true
|
|
|
|
case "0000:00:00 00:00:00", "0000-00-00 00-00-00", "0000-00-00 00:00:00":
|
|
|
|
return true
|
|
|
|
case "0001-01-01 00:00:00", "0001-01-01 00:00:00 +0000 UTC":
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2022-03-24 18:30:59 +01:00
|
|
|
}
|
2023-02-24 15:53:59 +01:00
|
|
|
|
|
|
|
// DateTimeDefault tests if the datetime string is not empty and not a default value.
|
|
|
|
func DateTimeDefault(s string) bool {
|
|
|
|
switch s {
|
2023-02-24 16:15:43 +01:00
|
|
|
case "1970-01-01", "1970-01-01 00:00:00", "1970:01:01 00:00:00":
|
2023-02-24 15:53:59 +01:00
|
|
|
// Unix epoch.
|
|
|
|
return true
|
2023-02-24 16:15:43 +01:00
|
|
|
case "1980-01-01", "1980-01-01 00:00:00", "1980:01:01 00:00:00":
|
|
|
|
// Windows default.
|
2023-02-24 15:53:59 +01:00
|
|
|
return true
|
2023-02-24 16:15:43 +01:00
|
|
|
case "2002-12-08 12:00:00", "2002:12:08 12:00:00":
|
2023-02-24 15:53:59 +01:00
|
|
|
// Android Bug: https://issuetracker.google.com/issues/36967504
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return EmptyDateTime(s)
|
|
|
|
}
|
|
|
|
}
|