ParseTSParam takes a req (typically from an application service) and parses a Time object from the req if it exists in the query parameters. If it doesn't exist, the current time is returned.
(req *http.Request)
| 23 | // from the req if it exists in the query parameters. If it doesn't exist, the |
| 24 | // current time is returned. |
| 25 | func ParseTSParam(req *http.Request) (time.Time, error) { |
| 26 | // Use the ts parameter's value for event time if present |
| 27 | tsStr := req.URL.Query().Get("ts") |
| 28 | if tsStr == "" { |
| 29 | return time.Now(), nil |
| 30 | } |
| 31 | |
| 32 | // The parameter exists, parse into a Time object |
| 33 | ts, err := strconv.ParseInt(tsStr, 10, 64) |
| 34 | if err != nil { |
| 35 | return time.Time{}, fmt.Errorf("param 'ts' is no valid int (%s)", err.Error()) |
| 36 | } |
| 37 | |
| 38 | return time.Unix(ts/1000, 0), nil |
| 39 | } |