package playevents import "time" // clientClockFutureSkew is how far into the future a client's clock may run // before its timestamp is treated as bogus and replaced with now. const clientClockFutureSkew = 5 * time.Minute // ClampClientTime bounds a play time a client supplied, on any surface (the // native apps' events and a Subsonic app's scrobble): no earlier than the // account's creation, since offline replays can be days old but no play // predates the listener, and no later than now plus a small allowance for // clock drift. An unbounded client clock would date plays anywhere, landing // them on the wrong day in History and poisoning Rediscover's "not played in // six months" ordering. func ClampClientTime(at, userCreatedAt, now time.Time) time.Time { if at.Before(userCreatedAt) { return userCreatedAt } if at.After(now.Add(clientClockFutureSkew)) { return now } return at }