go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/cron/once_at.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package cron
     9  
    10  import (
    11  	"fmt"
    12  	"time"
    13  )
    14  
    15  // Interface assertions.
    16  var (
    17  	_ Schedule     = (*OnceAtSchedule)(nil)
    18  	_ fmt.Stringer = (*OnceAtSchedule)(nil)
    19  )
    20  
    21  // OnceAt returns a schedule that fires once at a given time.
    22  // It will never fire again unless reloaded.
    23  func OnceAt(t time.Time) OnceAtSchedule {
    24  	return OnceAtSchedule{Time: t}
    25  }
    26  
    27  // OnceAtSchedule is a schedule.
    28  type OnceAtSchedule struct {
    29  	Time time.Time
    30  }
    31  
    32  // String returns a string representation of the schedule.
    33  func (oa OnceAtSchedule) String() string {
    34  	return fmt.Sprintf("%s %s", StringScheduleOnceAt, oa.Time.Format(time.RFC3339))
    35  }
    36  
    37  // Next returns the next runtime.
    38  func (oa OnceAtSchedule) Next(after time.Time) time.Time {
    39  	if after.IsZero() {
    40  		return oa.Time
    41  	}
    42  	if oa.Time.After(after) {
    43  		return oa.Time
    44  	}
    45  	return Zero
    46  }