main.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package main
  2. import (
  3. //"fmt"
  4. "net/http"
  5. "bufio"
  6. "os"
  7. "github.com/zenazn/goji"
  8. "github.com/zenazn/goji/web"
  9. "time"
  10. "strings"
  11. "strconv"
  12. "github.com/flosch/pongo2"
  13. )
  14. type TimeData struct {
  15. Campany string
  16. Type int
  17. Hour int
  18. Minute int
  19. Start string
  20. End string
  21. }
  22. func hello(c web.C, w http.ResponseWriter, r *http.Request) {
  23. var fp *os.File
  24. var err error
  25. fp, err = os.Open("bus.csv")
  26. if err != nil {
  27. panic(err)
  28. }
  29. var time_datas []TimeData
  30. var times []TimeData
  31. defer fp.Close()
  32. scanner := bufio.NewScanner(fp)
  33. for scanner.Scan() {
  34. var row []string
  35. row = strings.Split(scanner.Text(), ",")
  36. time_data := TimeData{}
  37. time_data.Campany = row[0]
  38. time_data.Type, _ = strconv.Atoi(row[1])
  39. time_data.Hour, _ = strconv.Atoi(row[2])
  40. time_data.Minute, _ = strconv.Atoi(row[3])
  41. time_data.Start = row[4]
  42. time_data.End = row[5]
  43. time_datas = append(time_datas, time_data)
  44. }
  45. //var timeformat = "2006-01-02 15:04:06"
  46. //t,err := time.Parse(timeformat, "2016-02-10 7:30:00")
  47. t := time.Now()
  48. var now_hour = t.Hour()
  49. var now_min = t.Minute()
  50. var weekday = t.Weekday()
  51. for _, value := range time_datas {
  52. if ((now_hour < value.Hour) || (now_hour == value.Hour && now_min <= value.Minute)) {
  53. if ((value.Type == 1 && weekday != time.Saturday && weekday != time.Sunday) ||
  54. (value.Type == 2 && (weekday == time.Saturday || weekday == time.Sunday))) {
  55. //fmt.Fprintf(w, "Hello, %2d %2d! <br />", value.Hour, value.Minute)
  56. times = append(times, value)
  57. }
  58. }
  59. }
  60. if (0 == len(times)) {
  61. times = time_datas
  62. }
  63. if err := scanner.Err(); err != nil {
  64. panic(err)
  65. }
  66. tpl, err := pongo2.DefaultSet.FromFile("view/page.tpl")
  67. if err != nil {
  68. http.Error(w, err.Error(), http.StatusInternalServerError)
  69. return
  70. }
  71. tpl.ExecuteWriter(pongo2.Context{"times": times}, w)
  72. }
  73. func main() {
  74. goji.Get("/", hello)
  75. goji.Serve()
  76. }