gocronexpr

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2021 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

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.

Directories

Path Synopsis
cmd
gocronexpr command

Jump to

Keyboard shortcuts

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