Skip to content

Commit

Permalink
✨ feat: add RawTime and CountDays
Browse files Browse the repository at this point in the history
  • Loading branch information
jingyuexing committed Oct 9, 2024
1 parent e4b329f commit d0398ec
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ func (dt DateTime) SetTime(sec int64, ns int64) DateTime {
return d
}

func (dt DateTime) RawTime() *time.Time {
return &dt.time
}

func (dt DateTime) LocaleCallBack(t string, call func(t string) string) string {
return dt.Format(call(t))
}
Expand Down Expand Up @@ -260,6 +264,16 @@ func (d DateTime) WeekDay() int {
return int(d.time.Weekday())
}

func (d DateTime) CountDays() int {
start := time.Date(d.Year, time.January, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(d.Year+1, time.January, 1, 0, 0, 0, 0, time.UTC)
return int(end.Sub(start).Hours() / 24)
}

func (d DateTime) Progress() float32 {
return float32(d.time.YearDay()) / float32(d.CountDays())
}

func (d DateTime) WeekOfYear() int {
_, week := d.time.ISOWeek()
return week
Expand Down Expand Up @@ -414,6 +428,9 @@ func extractDateParts(date string, formatParts []string) map[string]string {

func (dt DateTime) Parse(date string, formatTemplate string) *DateTime {
datetime := NewDateTime()
if formatTemplate == "" {
formatTemplate = dt.DateFormat + "T" + dt.TimeFormat + "Z"
}
formatParts := parseFormatTemplate(formatTemplate)
dateParts := extractDateParts(date, formatParts)

Expand Down

0 comments on commit d0398ec

Please sign in to comment.