58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package tracking
|
|
|
|
import (
|
|
"math/bits"
|
|
"time"
|
|
|
|
"github.com/pocketbase/dbx"
|
|
"github.com/pocketbase/pocketbase/core"
|
|
)
|
|
|
|
func settingsAt(tx core.App, owner, dayKey string) (*core.Record, error) {
|
|
rows, err := tx.FindRecordsByFilter("settings_history",
|
|
liveFilter+" && effective_from <= {:day}",
|
|
"-effective_from,-id",
|
|
1, 0,
|
|
dbx.Params{"owner": owner, "day": dayKey},
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return nil, nil
|
|
}
|
|
return rows[0], nil
|
|
}
|
|
|
|
func location(tx core.App, owner, dayKey string) *time.Location {
|
|
set, err := settingsAt(tx, owner, dayKey)
|
|
if err != nil || set == nil {
|
|
return time.UTC
|
|
}
|
|
loc, err := time.LoadLocation(set.GetString("timezone"))
|
|
if err != nil {
|
|
return time.UTC
|
|
}
|
|
return loc
|
|
}
|
|
|
|
func weekExpectedMs(set *core.Record) int64 {
|
|
return int64(set.GetFloat("hours_per_week") * 3_600_000)
|
|
}
|
|
|
|
func dayExpectedMs(set *core.Record) int64 {
|
|
days := bits.OnesCount(uint(workdaysMask(set)))
|
|
if days == 0 {
|
|
return 0
|
|
}
|
|
return weekExpectedMs(set) / int64(days)
|
|
}
|
|
|
|
func isWorkday(set *core.Record, wd time.Weekday) bool {
|
|
return workdaysMask(set)&(1<<((int(wd)+6)%7)) != 0
|
|
}
|
|
|
|
func workdaysMask(set *core.Record) int {
|
|
return set.GetInt("workdays_mask")
|
|
}
|