Skip to content
Permalink
Browse files

12-Dates-And-Times format post timestamp

  • Loading branch information...
bonfy committed Oct 16, 2018
1 parent 01611b3 commit b5cdcf1fec212a012486928d2bc0ad3052d37d51
Showing with 127 additions and 3 deletions.
  1. +5 −0 model/post.go
  2. +119 −0 model/utils.go
  3. +1 −1 templates/content/explore.html
  4. +1 −1 templates/content/index.html
  5. +1 −1 templates/content/profile.html
@@ -13,6 +13,11 @@ type Post struct {
Timestamp *time.Time `sql:"DEFAULT:current_timestamp"`
}

// FormattedTimeAgo func
func (p *Post) FormattedTimeAgo() string {
return FromTime(*p.Timestamp)
}

// GetPostsByUserID func
func GetPostsByUserID(id int) (*[]Post, error) {
var posts []Post
@@ -1,8 +1,12 @@
package model

import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"math"
"time"
)

// GeneratePasswordHash : Use MD5
@@ -16,3 +20,118 @@ func Md5(origin string) string {
hasher.Write([]byte(origin))
return hex.EncodeToString(hasher.Sum(nil))
}

const (
minute = 1
hour = minute * 60
day = hour * 24
month = day * 30
year = day * 365
quarter = year / 4
)

// FromDuration returns a friendly string representing an approximation of the
// given duration
func FromDuration(d time.Duration) string {
seconds := round(d.Seconds())

if seconds < 30 {
return "less than a minute"
}

if seconds < 90 {
return "1 minute"
}

minutes := div(seconds, 60)

if minutes < 45 {
return fmt.Sprintf("%0d minutes", minutes)
}

hours := div(minutes, 60)

if minutes < day {
return fmt.Sprintf("about %s", pluralize(hours, "hour"))
}

if minutes < (42 * hour) {
return "1 day"
}

days := div(hours, 24)

if minutes < (30 * day) {
return pluralize(days, "day")
}

months := div(days, 30)

if minutes < (45 * day) {
return "about 1 month"
}

if minutes < (60 * day) {
return "about 2 months"
}

if minutes < year {
return pluralize(months, "month")
}

rem := minutes % year
years := minutes / year

if rem < (3 * month) {
return fmt.Sprintf("about %s", pluralize(years, "year"))
}
if rem < (9 * month) {
return fmt.Sprintf("over %s", pluralize(years, "year"))
}

years++
return fmt.Sprintf("almost %s", pluralize(years, "year"))
}

// FromTime returns a friendly string representing the approximate difference
// from the given time and time.Now()
func FromTime(t time.Time) string {
now := time.Now()

var d time.Duration
var suffix string

if t.Before(now) {
d = now.Sub(t)
suffix = "ago"
} else {
d = t.Sub(now)
suffix = "from now"
}

return fmt.Sprintf("%s %s", FromDuration(d), suffix)
}

func pluralize(i int, s string) string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("%d %s", i, s))
if i != 1 {
buf.WriteString("s")
}
return buf.String()
}

func round(f float64) int {
return int(math.Floor(f + .50))
}

func div(numerator int, denominator int) int {
rem := numerator % denominator
result := numerator / denominator

if rem >= (denominator / 2) {
result++
}

return result
}
@@ -5,7 +5,7 @@ <h1>Hello, {{.CurrentUser}}!</h1>
<table class="table table-hover">
<tr valign="top">
<td width="36px"><img src="{{.User.Avatar}}&s=36"></td>
<td><a href="/user/{{.User.Username}}">{{ .User.Username }}</a> says:<br>{{ .Body }}</td>
<td><a href="/user/{{.User.Username}}">{{ .User.Username }}</a> said {{.FormattedTimeAgo}}:<br>{{ .Body }}</td>
</tr>
</table>
{{end}}
@@ -16,7 +16,7 @@ <h1>Hello, {{.CurrentUser}}!</h1>
<table class="table table-hover">
<tr valign="top">
<td width="36px"><img src="{{.User.Avatar}}&s=36"></td>
<td><a href="/user/{{.User.Username}}">{{ .User.Username }}</a> says:<br>{{ .Body }}</td>
<td><a href="/user/{{.User.Username}}">{{ .User.Username }}</a> said {{.FormattedTimeAgo}}:<br>{{ .Body }}</td>
</tr>
</table>
{{end}}
@@ -34,7 +34,7 @@ <h1>User: {{.ProfileUser.Username}}</h1>
<table class="table table-hover">
<tr valign="top">
<td width="36px"><img src="{{.User.Avatar}}&s=36"></td>
<td><a href="/user/{{.User.Username}}">{{ .User.Username }}</a> says:<br>{{ .Body }}</td>
<td><a href="/user/{{.User.Username}}">{{ .User.Username }}</a> said {{.FormattedTimeAgo}}:<br>{{ .Body }}</td>
</tr>
</table>
{{end}}

0 comments on commit b5cdcf1

Please sign in to comment.
You can’t perform that action at this time.