time.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package traffic
  5. import (
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. func parseDepartureTime(departureTime string) (uint, error) {
  12. hms := strings.Split(departureTime, ":")
  13. h, err := strconv.ParseInt(hms[0], 10, 0)
  14. if err != nil {
  15. return 0, fmt.Errorf("while parsing hour: %w", err)
  16. }
  17. m, err := strconv.ParseInt(hms[1], 10, 0)
  18. if err != nil {
  19. return 0, fmt.Errorf("while parsing minutes: %w", err)
  20. }
  21. s, err := strconv.ParseInt(hms[2], 10, 0)
  22. if err != nil {
  23. return 0, fmt.Errorf("while parsing seconds: %w", err)
  24. }
  25. return uint(s + m*60 + h*3600), nil
  26. }
  27. func formatDepartureTime(secondsAfterMidnight uint) string {
  28. h := secondsAfterMidnight / 3600
  29. ms := secondsAfterMidnight % 3600
  30. m := ms / 60
  31. s := ms % 60
  32. return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
  33. }
  34. func calculateGtfsTime(gtfsTime uint, delay int32, date time.Time,
  35. timezone *time.Location) time.Time {
  36. noon := time.Date(date.Year(), date.Month(), date.Day(), 12, 0, 0, 0,
  37. timezone)
  38. return noon.Add(time.Duration(-12) * time.Hour).Add(time.Duration(gtfsTime) * time.Second).Add(time.Duration(delay) * time.Second)
  39. }