Fix time duration value
authorr <r@freesoftwareextremist.com>
Wed, 19 Feb 2020 16:33:21 +0000 (16:33 +0000)
committerr <r@freesoftwareextremist.com>
Wed, 19 Feb 2020 16:33:21 +0000 (16:33 +0000)
- Make sure that duration is not < 0
- Handle nil ExpiresAt time in poll

mastodon/poll.go
renderer/renderer.go
templates/status.tmpl

index 274b95e60a8974314cb7e66846867e81f53d9b31..cd874a7d4140cb0872d98f327e6129e434157df1 100644 (file)
@@ -10,7 +10,7 @@ import (
 
 type Poll struct {
        ID          string       `json:"id"`
-       ExpiresAt   time.Time    `json:"expires_at"`
+       ExpiresAt   *time.Time    `json:"expires_at"`
        Expired     bool         `json:"expired"`
        Multiple    bool         `json:"multiple"`
        VotesCount  int64        `json:"votes_count"`
index 293a6c63b191712edae82530a2ed85b3820d15ac..a4f749d0e075da994c639678d1fac677bce6dd58 100644 (file)
@@ -197,11 +197,19 @@ func DurToStr(dur time.Duration) string {
 }
 
 func TimeSince(t time.Time) string {
-       return DurToStr(time.Since(t))
+       d := time.Since(t)
+       if d < 0 {
+               d = 0
+       }
+       return DurToStr(d)
 }
 
 func TimeUntil(t time.Time) string {
-       return DurToStr(time.Until(t))
+       d := time.Until(t)
+       if d < 0 {
+               d = 0
+       }
+       return DurToStr(d)
 }
 
 func FormatTimeRFC3339(t time.Time) string {
index c4f2e5f41f6d4f706a1e7224fc40cd15da12845a..536f515318fda1b830ebf83b0c0cbec71aed8267 100644 (file)
                                        <span>{{.Poll.VotesCount}} votes</span>
                                        {{if .Poll.Expired}}
                                        <span> - poll expired </span>
-                                       {{else}}
+                                       {{else if .Poll.ExpiresAt}}
                                        <span>
                                                - poll ends in
                                                <time datetime="{{FormatTimeRFC3339 .Poll.ExpiresAt}}" title="{{FormatTimeRFC822 .Poll.ExpiresAt}}">