-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
47 lines (40 loc) · 1.04 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func errorOut(c *gin.Context, err error) {
log.Println("error:", err)
c.JSON(400, gin.H{"error": "Invalid request."})
c.Abort()
}
func errorOutGraceful(c *gin.Context, err error) {
log.Println("error:", err)
c.Redirect(http.StatusSeeOther, "/")
c.Abort()
}
func errorOutAnnoying(c *gin.Context, err error) {
log.Println("error:", err)
c.Redirect(http.StatusSeeOther, "/forbidden")
c.Abort()
}
func parseTime(timeStr string) time.Time {
timeStr += " " + locString
parsedTime, err := time.Parse("01/02/06 3:04 MST", timeStr)
if err != nil {
log.Println("time parsing failed,", timeStr, "did not parse correctly:", err.Error())
}
return parsedTime
}
func formatTime(dur time.Duration) string {
durSeconds := dur.Microseconds() / 1000000
seconds := durSeconds % 60
durSeconds -= seconds
minutes := (durSeconds % (60 * 60)) / 60
durSeconds -= minutes * 60
hours := durSeconds / (60 * 60)
return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
}