api.go
changeset 239 ca5639b4768e
parent 231 741291bb4772
child 240 80c81e9b77b4
--- a/api.go	Thu Sep 06 01:07:40 2018 +0200
+++ b/api.go	Thu Sep 06 11:28:42 2018 +0200
@@ -16,6 +16,7 @@
 	"regexp"
 	"strconv"
 	"strings"
+	"time"
 
 	"github.com/pkg/errors"
 	"github.com/sendgrid/rest"
@@ -229,3 +230,29 @@
 	}
 	return nil
 }
+
+/* Mastodon timestamp handling */
+
+// MastodonDate is a custom type for the timestamps returned by some API calls
+// It is used, for example, by 'v1/instance/activity' and 'v2/search'.
+// The date returned by those Mastodon API calls is a string containing a
+// timestamp in seconds...
+
+// UnmarshalJSON handles deserialization for custom MastodonDate type
+func (act *MastodonDate) UnmarshalJSON(b []byte) error {
+	s, err := strconv.ParseInt(strings.Trim(string(b), "\""), 10, 64)
+	if err != nil {
+		return err
+	}
+	if s == 0 {
+		act.Time = time.Time{}
+		return nil
+	}
+	act.Time = time.Unix(s, 0)
+	return nil
+}
+
+// MarshalJSON handles serialization for custom MastodonDate type
+func (act *MastodonDate) MarshalJSON() ([]byte, error) {
+	return []byte(fmt.Sprintf("\"%d\"", act.Unix())), nil
+}