Browse Source

Allow the time formatting function return errors

master
Elis Hirwing 2 years ago
parent
commit
02ac39af65
Signed by: etu <elis@hirwing.se> GPG Key ID: D57EFA625C9A925F
1 changed files with 16 additions and 5 deletions
  1. +16
    -5
      src/statusscreen/SL.go

+ 16
- 5
src/statusscreen/SL.go View File

@@ -3,6 +3,7 @@ package statusscreen
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/olekukonko/tablewriter"
"io/ioutil"
@@ -71,10 +72,12 @@ func (sl *SL) GetOutput() string {
}

for _, v := range sl.lastResponse.ResponseData.Metros {
formattedTime, _ := sl.formatDisplayTime(v.ExpectedDateTime)

table.Append([]string{
v.LineNumber,
v.Destination,
sl.formatDisplayTime(v.ExpectedDateTime),
formattedTime,
v.TimeTabledDateTime[11:], // Remove YYYY-MM-DDT from the beginning of the string
v.ExpectedDateTime[11:], // Remove YYYY-MM-DDT from the beginning of the string
})
@@ -117,7 +120,7 @@ func (sl *SL) getTimeTable() (SLResponse, error) {
return parsedResponse, nil
}

func (sl *SL) formatDisplayTime(expected string) string {
func (sl *SL) formatDisplayTime(expected string) (string, error) {
myTimeZone, _ := time.Now().Zone()

then, err := time.Parse("2006-01-02T15:04:05 MST", expected+" "+myTimeZone)
@@ -127,13 +130,21 @@ func (sl *SL) formatDisplayTime(expected string) string {

timeLeft := time.Since(then).Seconds() * -1

// If time is less than zero
if timeLeft < 0 {
return "undefined", errors.New("Train is gone already")
}

// If time is less than one minute, show seconds
if timeLeft <= 60 && timeLeft > 0 {
return fmt.Sprintf("%d sec", int(timeLeft))
return fmt.Sprintf("%d sec", int(timeLeft)), nil
}

// If time is less than one hour, show minutes
if timeLeft <= 3600 && timeLeft > 0 {
return fmt.Sprintf("%d min", int(timeLeft)/60)
return fmt.Sprintf("%d min", int(timeLeft)/60), nil
}

return fmt.Sprintf("%02d:%02d:%02d", then.Hour(), then.Minute(), then.Second())
// Otherwise, just return a timestamp: HH:MM:SS
return fmt.Sprintf("%02d:%02d:%02d", then.Hour(), then.Minute(), then.Second()), nil
}

Loading…
Cancel
Save