miio il y a 8 ans
Parent
commit
1519a1d4a4
1 fichiers modifiés avec 91 ajouts et 0 suppressions
  1. 91 0
      main.go

+ 91 - 0
main.go

@@ -0,0 +1,91 @@
+package main
+
+import (
+    //"fmt"
+    "net/http"
+    "bufio"
+    "os"
+    "github.com/zenazn/goji"
+    "github.com/zenazn/goji/web"
+    "time"
+    "strings"
+    "strconv"
+    "github.com/flosch/pongo2"
+    )
+
+type TimeData struct {
+
+  Campany  string
+    Type     int
+    Hour     int
+    Minute   int
+    Start    string
+    End      string
+}
+
+func hello(c web.C, w http.ResponseWriter, r *http.Request) {
+
+  var fp *os.File
+    var err error
+    fp, err = os.Open("bus.csv")
+    if err != nil {
+      panic(err)
+    }
+
+  var time_datas []TimeData
+  var times []TimeData
+
+  defer fp.Close()
+
+    scanner := bufio.NewScanner(fp)
+    for scanner.Scan() {
+      var row []string
+        row = strings.Split(scanner.Text(), ",")
+        time_data := TimeData{}
+        time_data.Campany   = row[0]
+        time_data.Type, _   = strconv.Atoi(row[1])
+        time_data.Hour, _   = strconv.Atoi(row[2])
+        time_data.Minute, _ = strconv.Atoi(row[3])
+        time_data.Start     = row[4]
+        time_data.End       = row[5]
+
+        time_datas = append(time_datas, time_data)
+
+    }
+
+     //var timeformat = "2006-01-02 15:04:06"
+     //t,err := time.Parse(timeformat, "2016-02-10 7:30:00")
+     t := time.Now()
+     var now_hour = t.Hour()
+     var now_min  = t.Minute()
+     var weekday  = t.Weekday()
+     for _, value := range time_datas {
+       if ((now_hour < value.Hour) || (now_hour == value.Hour && now_min <= value.Minute)) {
+         if ((value.Type == 1 && weekday != time.Saturday && weekday != time.Sunday) ||
+           (value.Type == 2 && (weekday == time.Saturday || weekday == time.Sunday))) {
+           //fmt.Fprintf(w, "Hello, %2d %2d! <br />", value.Hour, value.Minute)
+
+            times = append(times, value)
+         }
+       }
+     }
+
+   if (0 == len(times)) {
+     times = time_datas
+   }
+   if err := scanner.Err(); err != nil {
+     panic(err)
+   }
+   tpl, err := pongo2.DefaultSet.FromFile("view/page.tpl")
+    if err != nil {
+        http.Error(w, err.Error(), http.StatusInternalServerError)
+        return
+    }
+    tpl.ExecuteWriter(pongo2.Context{"times": times}, w)
+}
+
+func main() {
+  goji.Get("/", hello)
+    goji.Serve()
+}
+