Giter VIP home page Giter VIP logo

kronika's Introduction

Kronika - The keeper of time

Kronika adds some extra utility around the standard time package. It does not have any other external dependencies.

WaitUntil

kronika.WaitUntil(ctx context.Context, t time.Time)

WaitUntil will block until the given time. Unlike using time.After or time.Timer, it can be cancelled by cancelling the context.

WaitFor

kronika.WaitFor(ctx context.Context, diff time.Duration)

WaitFor will block for the specified duration. Unlike using time.After or time.Timer, it can be cancelled by cancelling the context.

Every

kronika.Every(ctx context.Context, start time.Time, interval time.Duration) <-chan time.Time

kronika.Every() works like a cron job. It is supplied a start time, and an interval, and it will send the current time to the returned channel at the specified intervals. Again, it can always be cancelled using the context.

Here are some examples of using Every

Run on Tuesdays by 2pm

ctx := context.Background()

start, err := time.Parse(
    "2006-01-02 15:04:05",
    "2019-09-17 14:00:00",
) // is a tuesday
if err != nil {
    panic(err)
}

interval := time.Hour * 24 * 7 // 1 week

for t := range kronika.Every(ctx, start, interval) {
    // Perform action here
    log.Println(t.Format("2006-01-02 15:04:05"))
}

Run every hour, on the hour

ctx := context.Background()

start, err := time.Parse(
    "2006-01-02 15:04:05",
    "2019-09-17 14:00:00",
) // any time in the past works but it should be on the hour
if err != nil {
    panic(err)
}

interval := time.Hour // 1 hour

for t := range kronika.Every(ctx, start, interval) {
    // Perform action here
    log.Println(t.Format("2006-01-02 15:04:05"))
}

Run every 10 minutes, starting in a week

ctx := context.Background()

// see https://golang.org/pkg/time/#Time.AddDate
start, err := time.Now().AddDate(0, 0, 7) 
if err != nil {
    panic(err)
}

interval := time.Minute * 10 // 10 minutes

for t := range kronika.Every(ctx, start, interval) {
    // Perform action here
    log.Println(t.Format("2006-01-02 15:04:05"))
}

kronika's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.