Skip to content

Commit f265de7

Browse files
committed
add time parsing error
1 parent ebe382e commit f265de7

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

api/connection.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,29 @@ import (
66
"io"
77
"net/http"
88
"os"
9+
"regexp"
910
"strconv"
1011

1112
"github.com/Kaya-Sem/commandtrein/internal/util"
1213
)
1314

1415
/*
1516
GetConnections fetches the connection data from the API and returns the response body as a byte slice.
16-
1717
https://docs.irail.be/#connections
1818
*/
19+
20+
func validateTimeRegex(timeStr string) bool {
21+
// Pattern explanation:
22+
// ^ - start of string
23+
// ([01][0-9] - 00-19 (first two digits)
24+
// |2[0-3]) - OR 20-23 (hours)
25+
// [0-5][0-9] - 00-59 (minutes)
26+
// $ - end of string
27+
pattern := `^([01][0-9]|2[0-3])[0-5][0-9]$`
28+
matched, _ := regexp.MatchString(pattern, timeStr)
29+
return matched
30+
}
31+
1932
func GetConnections(stationFrom string, stationTo string, time string, departure bool, date string) ([]byte, error) {
2033

2134
timesel := "arrival"
@@ -25,8 +38,14 @@ func GetConnections(stationFrom string, stationTo string, time string, departure
2538

2639
if time == "" {
2740
time = util.GetBelgiumTimeHHMM()
41+
} else {
42+
if !validateTimeRegex(time) {
43+
return nil, fmt.Errorf("Time does not follow hhmm format")
44+
}
2845
}
2946

47+
// TODO: do time checking
48+
3049
url := fmt.Sprintf("https://api.irail.be/connections/?from=%s&to=%s&time=%s&timesel=%s&format=json&lang=nl&typeOfTransport=automatic&alerts=false&results=10",
3150
stationFrom,
3251
stationTo,

cmd/connection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func getNextValidDay(dayName string) string {
8585
return fmt.Sprintf("%02d%02d%02d", nextValidDay.Day(), nextValidDay.Month(), nextValidDay.Year()%100)
8686
}
8787

88-
func handleConnection(stationFrom string, stationTo string, queryMode string, departure bool, date string) {
88+
func handleConnection(stationFrom string, stationTo string, timeQuery string, departure bool, date string) {
8989
s := util.NewSpinner("", " fetching connections", 1*time.Second)
9090
s.Start()
9191

@@ -100,7 +100,7 @@ func handleConnection(stationFrom string, stationTo string, queryMode string, de
100100
}
101101
}
102102

103-
connectionsJSON, err := api.GetConnections(stationFrom, stationTo, queryMode, departure, parsedDate)
103+
connectionsJSON, err := api.GetConnections(stationFrom, stationTo, timeQuery, departure, parsedDate)
104104
if err != nil {
105105
panic(err)
106106
}

cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9-
const Version = "2.3.13"
9+
const Version = "2.3.14"
1010

1111
func NewVersionCommand() *cobra.Command {
1212
return &cobra.Command{

0 commit comments

Comments
 (0)