func GetBackoffForNextScheduleNonNegative(cronSchedule string, scheduledTime time.Time, now time.Time) *time.Duration {
	backoffDuration := GetBackoffForNextSchedule(cronSchedule, scheduledTime, now)
	if backoffDuration == NoBackoff || backoffDuration < 0 {
		backoffDuration = 0
	}
	return &backoffDuration
}
GetBackoffForNextScheduleNonNegative calculates the backoff time and ensures a non-negative duration.
package backoff
import (
	"time"
	"github.com/robfig/cron"
	"go.temporal.io/api/serviceerror"
	"go.temporal.io/server/common/convert"
)
const NoBackoff = time.Duration(-1)
func ValidateSchedule(cronSchedule string) error {
	if cronSchedule == "" {
		return nil
	}
	if _, err := cron.ParseStandard(cronSchedule); err != nil {
		return serviceerror.NewInvalidArgument("Invalid CronSchedule.")
	}
	return nil
}
func GetBackoffForNextSchedule(cronSchedule string, scheduledTime time.Time, now time.Time) time.Duration {
	if len(cronSchedule) == 0 {
		return NoBackoff
	}
	schedule, err := cron.ParseStandard(cronSchedule)
	if err != nil {
		return NoBackoff
	}
	scheduledUTCTime := scheduledTime.UTC()
	nowUTC := now.UTC()
	var nextScheduleTime time.Time
	if nowUTC.Before(scheduledUTCTime) {
		nextScheduleTime = scheduledUTCTime
	} else {
		nextScheduleTime = schedule.Next(scheduledUTCTime)
		for nextScheduleTime.Before(nowUTC) {
			nextScheduleTime = schedule.Next(nextScheduleTime)
		}
	}
	backoffInterval := nextScheduleTime.Sub(nowUTC)
	roundedInterval := time.Second * time.Duration(convert.Int64Ceil(backoffInterval.Seconds()))
	return roundedInterval
}