Commit 0d7e821f authored by Sander van Harmelen's avatar Sander van Harmelen

Implement a generic ISOTime type

parent 22272f90
......@@ -20,6 +20,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
......@@ -28,6 +29,7 @@ import (
"sort"
"strconv"
"strings"
"time"
"github.com/google/go-querystring/query"
)
......@@ -67,6 +69,40 @@ const (
OwnerPermission AccessLevelValue = 50
)
// ISOTime represents an ISO 8601 formatted date
type ISOTime time.Time
// ISO 8901 date format
const iso8901 = "2006-01-02"
// MarshalJSON implements the json.Marshaler interface
func (t ISOTime) MarshalJSON() ([]byte, error) {
if y := time.Time(t).Year(); y < 0 || y >= 10000 {
// ISO 8901 uses 4 digits for the years
return nil, errors.New("ISOTime.MarshalJSON: year outside of range [0,9999]")
}
b := make([]byte, 0, len(iso8901)+2)
b = append(b, '"')
b = time.Time(t).AppendFormat(b, iso8901)
b = append(b, '"')
return b, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (t *ISOTime) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package
if string(data) == "null" {
return nil
}
isotime, err := time.Parse(`"`+iso8901+`"`, string(data))
*t = ISOTime(isotime)
return err
}
// NotificationLevelValue represents a notification level.
type NotificationLevelValue int
......
......@@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"time"
"strings"
)
// UsersService handles communication with the user related methods of
......@@ -698,52 +697,16 @@ func (s *UsersService) RevokeImpersonationToken(user, token int, options ...Opti
return s.client.Do(req, nil)
}
// UserActivityTime represents a custom time format
// used by Gitlab in the user/activities response (YYYY-MM-DD)
type UserActivityTime struct {
time.Time
}
// User Activity represents an entry in the user/activities response
// LastActivityAt is deprecated and only available for downward compatibility
// UserActivity represents an entry in the user/activities response
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/users.html#get-user-activities-admin-only
type UserActivity struct {
Username string `json:"username"`
LastActivityOn *UserActivityTime `json:"last_activity_on"`
LastActivityAt *UserActivityTime `json:"last_activity_at"` // deprecated!
}
// layout of UserActivityTime for parsing time string
const uatLayout = "2006-01-02"
// custom unmarshaller for for UserActivityTime
func (uat *UserActivityTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
uat.Time = time.Time{}
return
}
uat.Time, err = time.Parse(uatLayout, s)
return
}
// custom marshaller for UserActivityTime
func (uat *UserActivityTime) MarshalJSON() ([]byte, error) {
if uat.Time.UnixNano() == nilTime {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("\"%s\"", uat.Time.Format(uatLayout))), nil
}
// helper method to check whether UserActivityTime is set
var nilTime = (time.Time{}).UnixNano()
func (uat *UserActivityTime) IsSet() bool {
return uat.UnixNano() != nilTime
LastActivityOn *ISOTime `json:"last_activity_on"`
}
// Get user activities (admin only)
// GetUserActivities retrieves user activities (admin only)
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/users.html#get-user-activities-admin-only
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment