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