Skip to content

Commit

Permalink
feature: ceph time format regexp support
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiu Jian committed May 8, 2024
1 parent ff6b95d commit 58a4812
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions util/regutils/regutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var CLICKHOUSE_TIME_REG *regexp.Regexp
var NORMAL_TIME_REG *regexp.Regexp
var FULLNORMAL_TIME_REG *regexp.Regexp
var RFC2882_TIME_REG *regexp.Regexp
var CEPH_TIME_REG *regexp.Regexp
var EMAIL_REG *regexp.Regexp
var CHINA_MOBILE_REG *regexp.Regexp
var FS_FORMAT_REG *regexp.Regexp
Expand Down Expand Up @@ -86,6 +87,8 @@ func init() {
NORMAL_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$`)
FULLNORMAL_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}$`)
RFC2882_TIME_REG = regexp.MustCompile(`[A-Z][a-z]{2}, [0-9]{1,2} [A-Z][a-z]{2} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{3}`)
// Tue May 7 15:46:33 2024
CEPH_TIME_REG = regexp.MustCompile(`[A-Z][a-z]{2} [A-Z][a-z]{2} [ 123][0-9] [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}`)
EMAIL_REG = regexp.MustCompile(`^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$`)
CHINA_MOBILE_REG = regexp.MustCompile(`^1[0-9-]{10}$`)
FS_FORMAT_REG = regexp.MustCompile(`^(ext|fat|hfs|xfs|swap|ntfs|reiserfs|ufs|btrfs)`)
Expand Down Expand Up @@ -266,6 +269,10 @@ func MatchRFC2882Time(str string) bool {
return RFC2882_TIME_REG.MatchString(str)
}

func MatchCephTime(str string) bool {
return CEPH_TIME_REG.MatchString(str)
}

func MatchEmail(str string) bool {
return EMAIL_REG.MatchString(str)
}
Expand Down
2 changes: 2 additions & 0 deletions util/regutils/regutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func TestRegexp(t *testing.T) {
{"2022-06-01 23:59:59 +0000 UTC", "MatchClickhouseTime", MatchClickhouseTime, true},
{"2023-03-23 12:02:19.206", "MatchFullISOTime2", MatchFullISOTime2, false},
{"2023-03-23 12:02:19.206", "MatchFullISOTime3", MatchFullISOTime3, true},
{"Tue Feb 14 14:52:50 2023", "MatchCephTime", MatchCephTime, true},
{"Tue May 7 15:46:33 2024", "MatchCephTime", MatchCephTime, true},
}
for _, c := range cases {
got := c.regfunc(c.in)
Expand Down
12 changes: 12 additions & 0 deletions util/timeutils/timeutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const (
FullIsoNanoTimeFormat3 = "2006-01-02 15:04:05.000000000"

RFC2882Format = time.RFC1123

CephTimeFormat = "Mon Jan 2 15:04:05 2006"
)

func IsoTime(now time.Time) string {
Expand Down Expand Up @@ -100,6 +102,10 @@ func RFC2882Time(now time.Time) string {
return Utcify(now).Format(RFC2882Format)
}

func CephTime(now time.Time) string {
return Utcify(now).Format(CephTimeFormat)
}

func DateStr(now time.Time) string {
return Utcify(now).Format(DateFormat)
}
Expand Down Expand Up @@ -198,6 +204,10 @@ func ParseRFC2882Time(str string) (time.Time, error) {
return time.Parse(RFC2882Format, str)
}

func ParseCephTime(str string) (time.Time, error) {
return time.Parse(CephTimeFormat, str)
}

func ParseDate(str string) (time.Time, error) {
return time.Parse(DateFormat, str)
}
Expand Down Expand Up @@ -240,6 +250,8 @@ func ParseTimeStr(str string) (time.Time, error) {
return ParseFullNormalTime(str)
} else if regutils.MatchRFC2882Time(str) {
return ParseRFC2882Time(str)
} else if regutils.MatchCephTime(str) {
return ParseCephTime(str)
} else if regutils.MatchCompactTime(str) {
return ParseCompactTime(str)
} else if regutils.MatchDate(str) {
Expand Down
6 changes: 6 additions & 0 deletions util/timeutils/timeutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestTimeUtils(t *testing.T) {
CompactTime,
ZStackTime,
FullIsoNanoTime,
CephTime,
} {
tm2, err := ParseTimeStr(tmf(tm))
if err != nil {
Expand Down Expand Up @@ -150,6 +151,11 @@ func TestParseTimeStrInTimeZone(t *testing.T) {
timezone: "Asia/Shanghai",
want: "2020-05-31T16:00:00Z",
},
{
in: "Tue May 7 15:46:31 2024",
timezone: "Asia/Shanghai",
want: "2024-05-07T07:46:31Z",
},
}
for _, c := range cases {
localTm, _ := ParseTimeStrInTimeZone(c.in, c.timezone)
Expand Down

0 comments on commit 58a4812

Please sign in to comment.