File: time_tools.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (26 lines) | stat: -rw-r--r-- 719 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package tools

import (
	"time"
)

// IsExpiredAtOrIn returns whether or not the result of calling TimeAtOrIn is
// "expired" within "until" units of time from now.
func IsExpiredAtOrIn(from time.Time, until time.Duration, at time.Time, in time.Duration) (time.Time, bool) {
	expiration := TimeAtOrIn(from, at, in)
	if expiration.IsZero() {
		return expiration, false
	}

	return expiration, expiration.Before(time.Now().Add(until))
}

// TimeAtOrIn returns either "at", or the "in" duration added to the current
// time. TimeAtOrIn prefers to add a duration rather than return the "at"
// parameter.
func TimeAtOrIn(from, at time.Time, in time.Duration) time.Time {
	if in == 0 {
		return at
	}
	return from.Add(in)
}