2020-09-11 10:26:41 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2020-09-11 11:03:55 +00:00
|
|
|
// GetTimestamp extracts unix timestamp in milliseconds from `timestamp` query arg.
|
2020-09-11 10:26:41 +00:00
|
|
|
//
|
|
|
|
// It returns 0 if there is no `timestamp` query arg.
|
|
|
|
func GetTimestamp(req *http.Request) (int64, error) {
|
2020-09-11 11:42:28 +00:00
|
|
|
ts := req.URL.Query().Get("timestamp")
|
2020-09-11 10:26:41 +00:00
|
|
|
if len(ts) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2020-09-11 11:03:55 +00:00
|
|
|
timestamp, err := strconv.ParseInt(ts, 10, 64)
|
2020-09-11 10:26:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("cannot parse `timestamp=%s` query arg: %w", ts, err)
|
|
|
|
}
|
2020-09-11 11:03:55 +00:00
|
|
|
return timestamp, nil
|
2020-09-11 10:26:41 +00:00
|
|
|
}
|