Skip to content

proposal: time: add API to retrieve next trigger time for Timer and Ticker #66680

@ktalg

Description

@ktalg

Proposal Details

The Go time package's Timer and Ticker are fundamental for scheduling future events. However, there's no straightforward way to query their next trigger time.

Currently, developers must resort to indirect methods to estimate the next trigger time. For example:

ticker := time.NewTicker(5 * time.Minute)
// Inaccurate assumption of next tick
nextTick := time.Now().Add(5 * time.Minute)

Estimating the next trigger time for Timer or Ticker is cumbersome and requires the context of their creation. For example, in a function that accepts only a Timer, the absence of creation context complicates accessing the next trigger time, hindering straightforward implementation and usage.

Internally, both Timer and Ticker leverage a structure similar to runtimeTimer, which already contains timing information such as when and nextwhen. This suggests that the necessary data for determining the next trigger time is already being tracked.

Proposal:
Introduce a method, NextTriggerTime(), (or any other aptly named method) be in Timer and Ticker, to expose the next trigger time. Given the internal tracking within runtimeTimer, this addition should be straightforward to implement and maintain.

Example usage:

ticker := time.NewTicker(5 * time.Minute)
nextTick := ticker.NextTriggerTime()

The suggestion to use a method to obtain this information aims to ensure the integrity of the Timer/Ticker's own structural encapsulation and to avoid triggering concurrency access conflicts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeProposalWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions