gocronexpr

package module
v1.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

Cron expression parsing in go

GoDoc Test Status Go Report Card

Introduction

Supports cron expressions with seconds field. A copy of CronSequenceGenerator from Spring Framework.

Installation

The import path for the package is github.com/dongfg/gocronexpr.

To install it, run:

 go get github.com/dongfg/gocronexpr
License

The package is licensed under the Apache License 2.0. Please see the LICENSE file for details.

Example
package main

import (
	"fmt"
	"github.com/dongfg/gocronexpr"
	"time"
)

func main() {
	cronExpr, err := gocronexpr.New("* * * * * *", time.Local)
	if err != nil {
		fmt.Println(err)
		return
	}

	nextTime, err := cronExpr.Next(time.Now())
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(nextTime)
}

This example will generate the following output:

2019-05-22 17:13:22 +0800 CST
Install as a command

Download from release page or install with go command:

GO111MODULE="off" go get github.com/dongfg/gocronexpr/cmd/gocronexpr

Usage:

dongfg at MacBook-Pro.local in [~]
10:42:08 $ gocronexpr
NAME:
  gocronexpr - Display the time of the next N runs base on cron expression
USAGE:
  gocronexpr <cron> [N]
OPTIONS:
  <cron>  6 fields cron expression
  [N]     next number of runs, default 5

dongfg at MacBook-Pro.local in [~]
10:42:10 $ gocronexpr "5 */5 * * * *"
1: 2021-07-14 10:45:05
2: 2021-07-14 10:50:05
3: 2021-07-14 10:55:05
4: 2021-07-14 11:00:05
5: 2021-07-14 11:05:05

dongfg at MacBook-Pro.local in [~]
10:42:15 $ gocronexpr "5 */5 * * * *" 3
1: 2021-07-14 10:45:05
2: 2021-07-14 10:50:05
3: 2021-07-14 10:55:05

Documentation

Overview

Package gocronexpr implements cron expression parser and calculate next execution time.

The pattern is a list of six single space-separated fields: representing second, minute, hour, day, month, weekday. Month and weekday names can be given as the first three letters of the English names.

Example patterns:

  • "0 0 * * * *" = the top of every hour of every day.
  • "*\/10 * * * * *" = every ten seconds.
  • "0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
  • "0 0 6,19 * * *" = 6:00 AM and 7:00 PM every day.
  • "0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day.
  • "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays.
  • "0 0 0 25 12 ?" = every Christmas Day at midnight.

Usage:

cronExpr, err := New("* * * * * *", time.Local)
if err != nil {
	fmt.Println(err)
	return
}

nextTime, err := cronExpr.Next(time.Now())
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(nextTime)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CronExpr

type CronExpr struct {
	// contains filtered or unexported fields
}

CronExpr is parse result with no exported fields

func New

func New(expression string, location *time.Location) (*CronExpr, error)

New cron expr, return error if parse fail

func (*CronExpr) Next

func (c *CronExpr) Next(t *time.Time) (time.Time, error)

Next time calculated based on the given time.

func (*CronExpr) Run added in v1.1.0

func (c *CronExpr) Run(fn func(), options *ScheduleOptions)

Run function periodically by cron expr

type ScheduleOptions added in v1.1.1

type ScheduleOptions struct {
	Start    *time.Time
	End      *time.Time
	Executed func()
	Finish   func()
}

ScheduleOptions by cron expr

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL