relativetime.go 591 B

12345678910111213141516171819202122
  1. package timex
  2. import "time"
  3. // Use the long enough past time as start time, in case timex.Now() - lastTime equals 0.
  4. var initTime = time.Now().AddDate(-1, -1, -1)
  5. // Now returns a relative time duration since initTime, which is not important.
  6. // The caller only needs to care about the relative value.
  7. func Now() time.Duration {
  8. return time.Since(initTime)
  9. }
  10. // Since returns a diff since given d.
  11. func Since(d time.Duration) time.Duration {
  12. return time.Since(initTime) - d
  13. }
  14. // Time returns current time, the same as time.Now().
  15. func Time() time.Time {
  16. return initTime.Add(Now())
  17. }