~anjan/mnc

ref: 3c887e23f9c659a57b588f09efb1d1ca1ea79324 mnc/mnc.go -rw-r--r-- 884 bytes
3c887e23 — Martin Vahlensieck Remove goroutine 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main

import (
	"bufio"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
	"time"

	"github.com/influxdata/cron"
)

func init() {
	verbose := flag.Bool("v", false, "display log output")
	flag.Parse()

	if *verbose {
	} else {
		log.SetOutput(ioutil.Discard)
		log.SetFlags(0)
	}
}

func main() {
	// seed with a large number that rtcwake can still handle
	m := 268435455
	s := bufio.NewScanner(os.Stdin)

	n := time.Now()
	for s.Scan() {
		//we need to only get the first 5 fields otherwise
		//the cron library will try to parse the rest too
		fields := strings.Split(strings.TrimSpace(s.Text()), " ")
		l := strings.Join(fields[:5], " ")
		p, e := cron.ParseUTC(l)
		if e == nil {
			t, e := p.Next(n)
			if e != nil {
				log.Fatal(e)
			}
			i := int(t.Sub(n).Seconds())
			if i < m {
				m = i
			}
			log.Print(i)
		} else {
			log.Print(e)
		}
	}

	fmt.Println(m)

}