1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add GenerateDatetimesBetween in datetime package

This commit is contained in:
dudaodong
2024-09-09 11:35:17 +08:00
parent 48244d6711
commit da84d95aa3
5 changed files with 175 additions and 0 deletions

View File

@@ -420,3 +420,26 @@ func DaysBetween(start, end time.Time) int {
return days
}
// GenerateDatetimesBetween returns a slice of strings between two times.
// layout: the format of the datetime string
// interval: the interval between two datetimes
// Play: todo
func GenerateDatetimesBetween(start, end time.Time, layout string, interval string) ([]string, error) {
var result []string
if start.After(end) {
start, end = end, start
}
duration, err := time.ParseDuration(interval)
if err != nil {
return nil, err
}
for current := start; !current.After(end); current = current.Add(duration) {
result = append(result, current.Format(layout))
}
return result, nil
}