Skip to content

Commit

Permalink
Merge pull request #155 from vshn/fix-tz-weekly
Browse files Browse the repository at this point in the history
Fix timezone in weekly report
  • Loading branch information
ccremer authored Dec 13, 2022
2 parents 14d3cd6 + a1f3fe0 commit c54e308
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions pkg/odoo/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func LocalizeTime(tm time.Time, loc *time.Location) time.Time {
return time.Date(tm.Year(), tm.Month(), tm.Day(), tm.Hour(), tm.Minute(), tm.Second(), tm.Nanosecond(), loc)
}

// Midnight returns a new time object in midnight (most recently past).
func Midnight(tm time.Time) time.Time {
return time.Date(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0, tm.Location())
}

// MustParseDateTime parses the given value in DateTimeFormat or panics if it fails.
func MustParseDateTime(value string) Date {
tm, err := ParseDateTime(value)
Expand Down
4 changes: 2 additions & 2 deletions pkg/timesheet/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (r *ReportBuilder) reduceLeavesToBlocks(leaves []model.Leave) []AbsenceBloc
from := leave.DateFrom
blocks = append(blocks, AbsenceBlock{
Reason: leave.Type.String(),
Date: time.Date(from.Year(), from.Month(), from.Day(), 0, 0, 0, 0, from.Location()),
Date: odoo.Midnight(from.Time),
})
}
}
Expand Down Expand Up @@ -275,7 +275,7 @@ func (r *ReportBuilder) filterLeavesInTimeRange() []model.Leave {
for _, split := range splits {
tz := r.getTimeZone()
from := split.DateFrom
date := time.Date(from.Year(), from.Month(), from.Day(), 0, 0, 0, 0, tz)
date := odoo.Midnight(from.In(tz))
if odoo.IsWithinTimeRange(date, r.from, r.to) && date.Weekday() != time.Sunday && date.Weekday() != time.Saturday {
split.DateFrom.Time = odoo.LocalizeTime(split.DateFrom.Time, tz)
split.DateTo.Time = odoo.LocalizeTime(split.DateTo.Time, tz)
Expand Down
6 changes: 3 additions & 3 deletions pkg/web/reportconfig/config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func (c *ConfigController) fetchUser(ctx context.Context) error {
}
tz := user.TimeZone.LocationOrDefault(timesheet.DefaultTimeZone)
c.User = user
c.StartOfWeek = c.StartOfWeek.In(tz)
c.EndOfWeek = c.EndOfWeek.In(tz)
c.StartOfWeek = odoo.Midnight(c.StartOfWeek.In(tz))
c.EndOfWeek = odoo.Midnight(c.EndOfWeek.In(tz))
return nil
}

Expand Down Expand Up @@ -163,7 +163,7 @@ func (c *ConfigController) displayWarning(_ context.Context, err error) error {
// getStartOfWeek returns the previously occurred Monday at midnight.
// If t is already a Monday, it will be truncated to midnight the same day.
func getStartOfWeek(t time.Time) time.Time {
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
t = odoo.Midnight(t)
if t.Weekday() == time.Sunday { // go treats Sunday as the first day of the week
return t.AddDate(0, 0, -6)
}
Expand Down

0 comments on commit c54e308

Please sign in to comment.