Skip to content

Commit

Permalink
✨ feat: template parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jingyuexing committed Aug 3, 2024
1 parent 17bb7da commit 3790f8e
Showing 1 changed file with 100 additions and 121 deletions.
221 changes: 100 additions & 121 deletions datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,29 +198,6 @@ func (dt DateTime) Today() string {
return dt.Format(dt.DateFormat)
}

// func (dt DateTime) Parse(format string) (DateTime, error) {
// d := DateTime{}
// parsedTime, err := time.Parse(time.RFC3339, format)

// if err != nil {
// return d, err
// }

// d.time = parsedTime
// d.DateFormat = dt.DateFormat
// d.TimeFormat = dt.TimeFormat
// d.monthFormat = dt.monthFormat
// d.weekFormat = dt.weekFormat
// d.Year = d.time.Year()
// d.Month = int(d.time.Month())
// d.Day = d.time.Day()
// d.Hour = d.time.Hour()
// d.Minute = d.time.Minute()
// d.Second = d.time.Second()
// d.Nanosecond = d.time.Nanosecond()
// return d, nil
// }

func (dt DateTime) SetTime(sec int64, ns int64) DateTime {
d := DateTime{
time: time.Unix(sec, ns),
Expand Down Expand Up @@ -363,108 +340,110 @@ func (dt DateTime) IsAfter(d DateTime) bool {
}

func parseFormatTemplate(formatTemplate string) []string {
formatParts := make([]string, 0)
i := 0
for i < len(formatTemplate) {
switch {
case strings.HasPrefix(formatTemplate[i:], "YYYY"):
formatParts = append(formatParts, "YYYY")
i += 4
case strings.HasPrefix(formatTemplate[i:], "YY"):
formatParts = append(formatParts, "YY")
i += 2
case strings.HasPrefix(formatTemplate[i:], "MM"):
formatParts = append(formatParts, "MM")
i += 2
case strings.HasPrefix(formatTemplate[i:], "M"):
formatParts = append(formatParts, "M")
i++
case strings.HasPrefix(formatTemplate[i:], "dd") || strings.HasPrefix(formatTemplate[i:], "DD"):
formatParts = append(formatParts, "dd")
i += 2
case strings.HasPrefix(formatTemplate[i:], "d"):
formatParts = append(formatParts, "d")
i++
case strings.HasPrefix(formatTemplate[i:], "HH"):
formatParts = append(formatParts, "HH")
i += 2
case strings.HasPrefix(formatTemplate[i:], "H"):
formatParts = append(formatParts, "H")
i++
case strings.HasPrefix(formatTemplate[i:], "mm"):
formatParts = append(formatParts, "mm")
i += 2
case strings.HasPrefix(formatTemplate[i:], "m"):
formatParts = append(formatParts, "m")
i++
case strings.HasPrefix(formatTemplate[i:], "ss"):
formatParts = append(formatParts, "ss")
i += 2
case strings.HasPrefix(formatTemplate[i:], "s"):
formatParts = append(formatParts, "s")
i++
case strings.HasPrefix(formatTemplate[i:], "ms"):
formatParts = append(formatParts, "ms")
i += 2
case strings.HasPrefix(formatTemplate[i:], "W"):
formatParts = append(formatParts, "W")
i++
case strings.HasPrefix(formatTemplate[i:], "WW"):
formatParts = append(formatParts, "WW")
i += 2
default:
i++
}
}
return formatParts
formatParts := make([]string, 0)
i := 0
for i < len(formatTemplate) {
switch {
case strings.HasPrefix(formatTemplate[i:], string(FormatYear)):
formatParts = append(formatParts, string(FormatYear))
i += 4
case strings.HasPrefix(formatTemplate[i:], string(FormatShortDay)):
formatParts = append(formatParts, string(FormatShortYear))
i += 2
case strings.HasPrefix(formatTemplate[i:], string(FormatMonth)):
formatParts = append(formatParts, string(FormatMonth))
i += 2
case strings.HasPrefix(formatTemplate[i:], string(FormatShortMonth)):
formatParts = append(formatParts, string(FormatShortMonth))
i++
case strings.HasPrefix(formatTemplate[i:], string(FormatDay)) || strings.HasPrefix(formatTemplate[i:], string(FormatUpperDay)):
formatParts = append(formatParts, string(FormatDay))
i += 2
case strings.HasPrefix(formatTemplate[i:], string(FormatShortDay)):
formatParts = append(formatParts, string(FormatShortDay))
i++
case strings.HasPrefix(formatTemplate[i:], string(FormatHour)):
formatParts = append(formatParts, string(FormatHour))
i += 2
case strings.HasPrefix(formatTemplate[i:], string(FormatShortHour)):
formatParts = append(formatParts, string(FormatShortHour))
i++
case strings.HasPrefix(formatTemplate[i:], string(FormatMinute)):
formatParts = append(formatParts, string(FormatMinute))
i += 2
case strings.HasPrefix(formatTemplate[i:], string(FormatShortHour)):
formatParts = append(formatParts, string(FormatShortHour))
i++
case strings.HasPrefix(formatTemplate[i:], string(FormatSecond)):
formatParts = append(formatParts, string(FormatSecond))
i += 2
case strings.HasPrefix(formatTemplate[i:], string(FormatShortSecond)):
formatParts = append(formatParts, string(FormatShortSecond))
i++
case strings.HasPrefix(formatTemplate[i:], string(FormatMillisecond)):
formatParts = append(formatParts, string(FormatMillisecond))
i += 2
case strings.HasPrefix(formatTemplate[i:], string(FormatWeek)):
formatParts = append(formatParts, string(FormatWeek))
i++
case strings.HasPrefix(formatTemplate[i:], string(FormatShortWeek)):
formatParts = append(formatParts, string(FormatShortWeek))
i += 2
default:
i++
}
}
return formatParts
}
func extractDateParts(date string, formatParts []string) map[string]string {
dateParts := make(map[string]string)
current := 0

for _, part := range formatParts {
val := ""
for current < len(date) && (unicode.IsDigit(rune(date[current])) || rune(date[current]) == '.') {
val += string(date[current])
current++
}
dateParts[part] = val
current++
}
dateParts := make(map[string]string)
current := 0

for _, part := range formatParts {
val := ""
for current < len(date) && (unicode.IsDigit(rune(date[current])) || rune(date[current]) == '.') {
val += string(date[current])
current++
}
dateParts[part] = val
current++
}

return dateParts
return dateParts
}

func (dt DateTime) Parse(date string, formatTemplate string) *DateTime {
datetime := NewDateTime()
formatParts := parseFormatTemplate(formatTemplate)
dateParts := extractDateParts(date, formatParts)

if len(dateParts) == 0 {
return nil
}

for key, value := range dateParts {
val, _ := strconv.Atoi(value)
switch key {
case "YYYY":
datetime.Year = val
case "YY":
datetime.Year = val
case "MM", "M":
datetime.Month = val
case "dd", "DD", "d":
datetime.Day = val
case "HH", "H":
datetime.Hour = val
case "mm", "m":
datetime.Minute = val
case "ss", "s":
datetime.Second = val
case "ms":
datetime.Milliseconds = val
}
}

return &datetime
datetime := NewDateTime()
formatParts := parseFormatTemplate(formatTemplate)
dateParts := extractDateParts(date, formatParts)

if len(dateParts) == 0 {
return nil
}

for key, value := range dateParts {
val, _ := strconv.Atoi(value)
switch key {
case "YYYY":
datetime.Year = val
case "YY":
datetime.Year = val
case "MM", "M":
datetime.Month = val
case "dd", "DD", "d":
datetime.Day = val
case "HH", "H":
datetime.Hour = val
case "W", "WW":
datetime.Week = val
case "mm", "m":
datetime.Minute = val
case "ss", "s":
datetime.Second = val
case "ms":
datetime.Milliseconds = val
}
}

return &datetime
}

0 comments on commit 3790f8e

Please sign in to comment.