pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/cron/examples_test.go (about)

     1  package cron
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"fmt"
    12  	"time"
    13  )
    14  
    15  // ////////////////////////////////////////////////////////////////////////////////// //
    16  
    17  func ExampleParse() {
    18  	expr, err := Parse("0,15,30,45 0,6,12,18 1-10,15,31 * Mon-Fri")
    19  
    20  	if err != nil {
    21  		return
    22  	}
    23  
    24  	m1 := time.Date(2020, 1, 1, 18, 15, 0, 0, time.Local)
    25  	m2 := time.Date(2020, 1, 1, 18, 20, 0, 0, time.Local)
    26  
    27  	fmt.Printf("Execute1: %t\n", expr.IsDue(m1))
    28  	fmt.Printf("Execute2: %t\n", expr.IsDue(m2))
    29  
    30  	// Output:
    31  	// Execute1: true
    32  	// Execute2: false
    33  }
    34  
    35  func ExampleExpr_Next() {
    36  	expr, err := Parse("0,15,30,45 0,6,12,18 1-10,15,31 * Mon-Fri")
    37  
    38  	if err != nil {
    39  		return
    40  	}
    41  
    42  	m := time.Date(2020, 1, 1, 18, 15, 0, 0, time.UTC)
    43  
    44  	fmt.Printf("%v\n", expr.Next(m))
    45  
    46  	// Output:
    47  	// 2020-01-01 18:30:00 +0000 UTC
    48  }
    49  
    50  func ExampleExpr_Prev() {
    51  	expr, err := Parse("0,15,30,45 0,6,12,18 1-10,15,31 * Mon-Fri")
    52  
    53  	if err != nil {
    54  		return
    55  	}
    56  
    57  	m := time.Date(2020, 1, 1, 18, 15, 0, 0, time.UTC)
    58  
    59  	fmt.Printf("%v\n", expr.Prev(m))
    60  
    61  	// Output:
    62  	// 2020-01-01 18:00:00 +0000 UTC
    63  }
    64  
    65  func ExampleExpr_String() {
    66  	expr, err := Parse("0,15,30,45 0,6,12,18 1-10,15,31 * Mon-Fri")
    67  
    68  	if err != nil {
    69  		return
    70  	}
    71  
    72  	fmt.Printf("%s\n", expr.String())
    73  
    74  	// Output:
    75  	// 0,15,30,45 0,6,12,18 1-10,15,31 * Mon-Fri
    76  }