-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
74 lines (58 loc) · 1.52 KB
/
utils.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"log"
"os/exec"
"time"
"github.com/robfig/cron/v3"
)
func Notification(title string) {
err := exec.Command("notify-send", "Todo Reminder", fmt.Sprintf("Reminder: '%s' is due in 30 minutes!", title)).Run()
if err != nil {
log.Println("Error sending notification:", err)
}
}
func RunNotifications(todos *Todos) {
log.Println("Notification system started...")
now := time.Now()
c := cron.New(cron.WithSeconds())
fmt.Println("Notification system started...")
for _, t := range *todos {
if t.Deadline != nil {
notifyTime := t.Deadline.Add(-45 * time.Minute)
if notifyTime.After(now) {
log.Printf("Scheduling notification for '%s' at %v\n", t.Title, notifyTime)
_, err := c.AddFunc(fmt.Sprintf("%d %d %d %d %d *",
notifyTime.Second(),
notifyTime.Minute(),
notifyTime.Hour(),
notifyTime.Day(),
int(notifyTime.Month())),
func(title string) func() {
return func() {
Notification(title)
}
}(t.Title))
if err != nil {
log.Printf("Error scheduling notification for '%s': %v", t.Title, err)
}
}
}
}
c.Start()
log.Println("Notification system is running in the background.")
}
func ParseLocalTime(time_string time.Time) time.Time {
localLocation := time.Now().Location()
localDeadline := time.Date(
time_string.Year(),
time_string.Month(),
time_string.Day(),
time_string.Hour(),
time_string.Minute(),
time_string.Second(),
time_string.Nanosecond(),
localLocation,
)
return localDeadline
}