-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
180 lines (147 loc) · 3.85 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
"time"
)
const (
IN Action = iota
OUT
)
type Action int8
type Stamp struct {
Action Action
Country string
Date time.Time
}
type Stay struct {
Country string
FromDate time.Time
ToDate time.Time
}
func (a Action) String() string {
if a == IN {
return "IN"
}
return "OUT"
}
func parseDate(dateStr string) (time.Time, error) {
layout := "02.01.06"
return time.Parse(layout, dateStr)
}
// reads the stamps from the file and returns the list of stamps.
func readStamps(filename string) ([]Stamp, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var stamps []Stamp
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) != 3 {
continue
}
timeStamp, err := parseDate(fields[2])
if err != nil {
continue
}
if fields[0] != "IN" && fields[0] != "OUT" {
continue
}
action := IN
if fields[0] == "OUT" {
action = OUT
}
stamps = append(stamps, Stamp{Action: action, Country: fields[1], Date: timeStamp})
}
sort.Slice(stamps, func(i, j int) bool {
return stamps[i].Date.Before(stamps[j].Date)
})
return stamps, scanner.Err()
}
// calculateStayPeriods calculates the stay periods based on the stamps and fromDate.
// It returns the stay periods that are after fromDate.
func calculateStayPeriods(stamps []Stamp, fromDate time.Time) []Stay {
var stays []Stay
var selectedSrays []Stay
// Remove doubles of stamps
for i := 0; i < len(stamps)-1; i++ {
if stamps[i].Action == stamps[i+1].Action && stamps[i].Country == stamps[i+1].Country && stamps[i].Date.Equal(stamps[i+1].Date) {
stamps = append(stamps[:i], stamps[i+1:]...)
}
}
for _, stamp := range stamps {
if stamp.Action == IN {
stays = append(stays, Stay{Country: stamp.Country, FromDate: stamp.Date})
}
}
for i := range stays {
nextDate := time.Now()
for _, stamp := range stamps {
if stamp.Date.After(stays[i].FromDate) {
d := stamp.Date
if d.Before(nextDate) {
nextDate = d
}
}
}
stays[i].ToDate = nextDate
}
for _, stay := range stays {
if stay.ToDate.After(fromDate) {
if stay.FromDate.Before(fromDate) {
stay.FromDate = fromDate
}
selectedSrays = append(selectedSrays, stay)
}
}
return selectedSrays
}
func formatDuration(days int) string {
years := days / 365
months := (days % 365) / 30
days = (days % 365) % 30
return fmt.Sprintf("%d years, %d months, %d days", years, months, days)
}
func calcTotalDays(in time.Time, out time.Time) int {
return int(out.Sub(in).Hours()/24) + 1
}
func main() {
if len(os.Args) != 4 {
fmt.Println("Usage: program <date> <country> <file>")
os.Exit(1)
}
fromDate, err := parseDate(os.Args[1])
if err != nil {
fmt.Println("Invalid from date format. Use DD.MM.YYYY")
os.Exit(1)
}
country := os.Args[2]
filename := os.Args[3]
stamps, err := readStamps(filename)
if err != nil {
fmt.Println("Error reading file:", err)
os.Exit(1)
}
stays := calculateStayPeriods(stamps, fromDate)
totalDays := 0
fmt.Println("Stay periods:")
for i, stay := range stays {
if i > 0 && !stay.FromDate.Equal(stays[i-1].ToDate) {
errorDays := calcTotalDays(stays[i-1].ToDate, stay.FromDate)
if errorDays > 2 {
fmt.Printf("Error: %d days between %s and %s\n", errorDays, stays[i-1].ToDate.Format("02.01.2006"), stay.FromDate.Format("02.01.2006"))
}
}
if stay.Country == country {
totalDays += calcTotalDays(stay.FromDate, stay.ToDate)
}
fmt.Printf("%s from %02d.%02d.%d to %02d.%02d.%d, total days: %d\n", stay.Country, stay.FromDate.Day(), stay.FromDate.Month(), stay.FromDate.Year(), stay.ToDate.Day(), stay.ToDate.Month(), stay.ToDate.Year(), calcTotalDays(stay.FromDate, stay.ToDate))
}
fmt.Printf("Total time in %s since %s: %s\n", country, os.Args[1], formatDuration(totalDays))
}