Allow specifying a time to count down to
The input for this is dumb AF, but it "works". Added flags are: -target-year -target-month -target-day -target-hour -target-minute -target-second These flags all take a number, and default to the current time if omitted.
This commit is contained in:
parent
5c31d22c11
commit
9e8008f090
74
main.go
74
main.go
|
@ -28,29 +28,71 @@ func main() {
|
||||||
var format string
|
var format string
|
||||||
|
|
||||||
flag.StringVar(&filename, "o", "/tmp/countdown.txt", "Output file to write the countdown to.")
|
flag.StringVar(&filename, "o", "/tmp/countdown.txt", "Output file to write the countdown to.")
|
||||||
flag.StringVar(&untilStr, "u", "10m", "Countdown duration")
|
flag.StringVar(&untilStr, "u", "", "Countdown duration")
|
||||||
flag.StringVar(&targetTimeStr, "t", "", "Countdown to time")
|
flag.StringVar(&targetTimeStr, "t", "", "Countdown to time")
|
||||||
flag.StringVar(&doneStr, "d", "00:00:00", "String to display when done")
|
flag.StringVar(&doneStr, "d", "00:00:00", "String to display when done")
|
||||||
//flag.StringVar(&format, "f", "%H:%M:%S", "Format for the countdown")
|
//flag.StringVar(&format, "f", "%H:%M:%S", "Format for the countdown")
|
||||||
flag.StringVar(&format, "f", "{{if .Hours}}{{.Hours}}h {{end}}{{if or .Minutes .Hours}}{{.Minutes}}m {{end}}{{.Seconds}}s", "Format for the countdown")
|
flag.StringVar(&format, "f", "{{if .Hours}}{{.Hours}}h {{end}}{{if or .Minutes .Hours}}{{.Minutes}}m {{end}}{{.Seconds}}s", "Format for the countdown")
|
||||||
|
|
||||||
|
var targetYear int
|
||||||
|
var targetDay int
|
||||||
|
var targetMonth int
|
||||||
|
|
||||||
|
flag.IntVar(&targetYear, "target-year", -1, "")
|
||||||
|
flag.IntVar(&targetDay, "target-day", -1, "")
|
||||||
|
flag.IntVar(&targetMonth, "target-month", -1, "")
|
||||||
|
|
||||||
|
var targetHour int
|
||||||
|
var targetMinute int
|
||||||
|
var targetSecond int
|
||||||
|
|
||||||
|
flag.IntVar(&targetHour, "target-hour", -1, "")
|
||||||
|
flag.IntVar(&targetMinute, "target-minute", -1, "")
|
||||||
|
flag.IntVar(&targetSecond, "target-second", -1, "")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
untilDuration, err := time.ParseDuration(untilStr)
|
var until time.Time
|
||||||
if err != nil {
|
now := time.Now()
|
||||||
fmt.Println(err)
|
|
||||||
os.Exit(1)
|
if untilStr != "" {
|
||||||
|
untilDuration, err := time.ParseDuration(untilStr)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
until = time.Now().Add(untilDuration)
|
||||||
|
} else {
|
||||||
|
if targetYear == -1 {
|
||||||
|
targetYear = now.Year()
|
||||||
|
}
|
||||||
|
if targetMonth == -1 {
|
||||||
|
targetMonth = int(now.Month())
|
||||||
|
}
|
||||||
|
if targetDay == -1 {
|
||||||
|
targetDay = now.Day()
|
||||||
|
}
|
||||||
|
|
||||||
|
if targetHour == -1 {
|
||||||
|
targetHour = now.Hour()
|
||||||
|
}
|
||||||
|
if targetMinute == -1 {
|
||||||
|
targetMinute = now.Minute()
|
||||||
|
}
|
||||||
|
if targetSecond == -1 {
|
||||||
|
targetSecond = now.Second()
|
||||||
|
}
|
||||||
|
|
||||||
|
until = time.Date(
|
||||||
|
targetYear, time.Month(targetMonth), targetDay,
|
||||||
|
targetHour, targetMinute, targetSecond,
|
||||||
|
0, time.Local,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
var until time.Time = time.Now().Add(untilDuration)
|
fmt.Printf("Output file: %s\nCountdown to: %s\n", filename, untiluntil)
|
||||||
|
|
||||||
d, err := time.ParseDuration("1s")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Output file: %s\nCountdown duration: %s\n", filename, untilDuration)
|
|
||||||
|
|
||||||
|
var err error
|
||||||
tp := template.New("time")
|
tp := template.New("time")
|
||||||
tp, err = tp.Parse(format)
|
tp, err = tp.Parse(format)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -60,9 +102,11 @@ func main() {
|
||||||
|
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
ticker := time.NewTicker(d)
|
ticker := time.NewTicker(time.Second)
|
||||||
|
|
||||||
go func(c <-chan time.Time) {
|
go func(c <-chan time.Time) {
|
||||||
count := time.Until(until)
|
count := time.Until(until)
|
||||||
|
fmt.Println(count)
|
||||||
for ; count >= 0; count = time.Until(until) {
|
for ; count >= 0; count = time.Until(until) {
|
||||||
h := int(count.Hours())
|
h := int(count.Hours())
|
||||||
m := int(int(count.Minutes()) % 60)
|
m := int(int(count.Minutes()) % 60)
|
||||||
|
|
Loading…
Reference in New Issue