github.com/blend/go-sdk@v1.20220411.3/cron/on_the_hour_at.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package cron
     9  
    10  import (
    11  	"fmt"
    12  	"time"
    13  )
    14  
    15  var (
    16  	_ Schedule     = (*OnTheHourAtUTCSchedule)(nil)
    17  	_ fmt.Stringer = (*OnTheHourAtUTCSchedule)(nil)
    18  )
    19  
    20  // EveryHourOnTheHour returns a schedule that fires every 60 minutes on the 00th minute.
    21  func EveryHourOnTheHour() Schedule {
    22  	return OnTheHourAtUTCSchedule{}
    23  }
    24  
    25  // EveryHourAtUTC returns a schedule that fires every hour at a given minute.
    26  func EveryHourAtUTC(minute, second int) Schedule {
    27  	return OnTheHourAtUTCSchedule{Minute: minute, Second: second}
    28  }
    29  
    30  // OnTheHourAtUTCSchedule is a schedule that fires every hour on the given minute.
    31  type OnTheHourAtUTCSchedule struct {
    32  	Minute int
    33  	Second int
    34  }
    35  
    36  // String returns a string representation of the schedule.
    37  func (o OnTheHourAtUTCSchedule) String() string {
    38  	return fmt.Sprintf("on the hour at %v:%v", o.Minute, o.Second)
    39  }
    40  
    41  // Next implements the chronometer Schedule api.
    42  func (o OnTheHourAtUTCSchedule) Next(after time.Time) time.Time {
    43  	var returnValue time.Time
    44  	now := Now()
    45  	if after.IsZero() {
    46  		returnValue = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), o.Minute, o.Second, 0, time.UTC)
    47  		if returnValue.Before(now) {
    48  			returnValue = returnValue.Add(time.Hour)
    49  		}
    50  	} else {
    51  		returnValue = time.Date(after.Year(), after.Month(), after.Day(), after.Hour(), o.Minute, o.Second, 0, time.UTC)
    52  		if returnValue.Before(after) {
    53  			returnValue = returnValue.Add(time.Hour)
    54  		}
    55  	}
    56  	return returnValue
    57  }