-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathuser.go
84 lines (80 loc) · 2.33 KB
/
user.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
75
76
77
78
79
80
81
82
83
84
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/hebcal/greg"
"github.com/hebcal/hdate"
"github.com/hebcal/hebcal-go/hebcal"
)
func readUserFile(filename string) []hebcal.UserEvent {
f, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "could not open input file %s.\n", filename)
os.Exit(1)
}
scanner := bufio.NewScanner(f)
re := regexp.MustCompile(`^(\S+)\s+(\d+)\s+(.+)$`)
lineNumber := 0
entries := make([]hebcal.UserEvent, 0, 10)
for scanner.Scan() {
line0 := scanner.Text()
line := strings.TrimSpace(line0)
lineNumber++
fields := re.FindStringSubmatch(line)
if len(fields) != 4 {
fmt.Fprintf(os.Stderr, "error, invalid format: %s:%d\n", filename, lineNumber)
continue
}
month, err := hdate.MonthFromName(fields[1])
if err != nil {
fmt.Fprintf(os.Stderr, "error, invalid month: %s:%d\n", filename, lineNumber)
continue
}
day, _ := strconv.Atoi(fields[2])
if day < 1 || day > 30 {
fmt.Fprintf(os.Stderr, "error, invalid days: %s:%d\n", filename, lineNumber)
}
entries = append(entries, hebcal.UserEvent{Month: month, Day: day, Desc: fields[3]})
}
return entries
}
func readYahrzeitFile(filename string) []hebcal.UserYahrzeit {
f, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "could not open yahrtzeit input file %s.\n", filename)
os.Exit(1)
}
scanner := bufio.NewScanner(f)
re := regexp.MustCompile(`^(\d+)\s+(\d+)\s+(\d+)\s+(.+)$`)
lineNumber := 0
entries := make([]hebcal.UserYahrzeit, 0, 10)
for scanner.Scan() {
line0 := scanner.Text()
line := strings.TrimSpace(line0)
lineNumber++
fields := re.FindStringSubmatch(line)
if len(fields) != 5 {
fmt.Fprintf(os.Stderr, "error, invalid format: %s:%d\n", filename, lineNumber)
continue
}
month0, _ := strconv.Atoi(fields[1])
if month0 < 1 || month0 > 12 {
fmt.Fprintf(os.Stderr, "error, invalid month: %s:%d\n", filename, lineNumber)
continue
}
day, _ := strconv.Atoi(fields[2])
year, _ := strconv.Atoi(fields[3])
month := time.Month(month0)
if day < 1 || day > greg.DaysIn(month, year) {
fmt.Fprintf(os.Stderr, "error, invalid days: %s:%d\n", filename, lineNumber)
}
gregDate := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
entries = append(entries, hebcal.UserYahrzeit{Date: gregDate, Name: fields[4]})
}
return entries
}