added natual time

pull/24/head
Akhil Gupta 4 years ago
parent b39e879325
commit a069a5ed99

@ -52,11 +52,11 @@ hr{
<div class="columns ten">
<div class="row">
<div class="columns nine">
<div class="columns eight">
<h4>{{.Title}} {{if .Podcast.Title }} // {{ .Podcast.Title}} {{end}}</h4>
</div>
<div class="columns two">
<small>{{ formatDate .PubDate }}</small>
<div class="columns three">
<small title="{{ formatDate .PubDate }}">{{ naturalDate .PubDate }}</small>
</div>
<div class="columns one">
<small> {{ formatDuration .Duration}}</small>
@ -84,10 +84,10 @@ hr{
<div class="row"><div class="columns twelve" style="text-align: center;">
{{if .previousPage }}
<a href="?page={{.previousPage}}" class="button button-primary">Previous Page</a>
<a href="?page={{.previousPage}}" class="button button-primary">Newer</a>
{{end}}
{{if .nextPage }}
<a href="?page={{.nextPage}}" class="button button-primary">Next Page</a>
<a href="?page={{.nextPage}}" class="button button-primary">Older</a>
{{end}}
</div></div>

@ -37,6 +37,10 @@ func main() {
"formatDate": func(raw time.Time) string {
return raw.Format("Jan 2 2006")
},
"naturalDate": func(raw time.Time) string {
return service.NatualTime(time.Now(), raw)
//return raw.Format("Jan 2 2006")
},
"latestEpisodeDate": func(podcastItems []db.PodcastItem) string {
var latest time.Time
for _, item := range podcastItems {

@ -0,0 +1,102 @@
package service
import (
"fmt"
"math"
"time"
)
func NatualTime(base, value time.Time) string {
if value.Before(base) {
return pastNaturalTime(base, value)
} else {
return futureNaturalTime(base, value)
}
}
func futureNaturalTime(base, value time.Time) string {
dur := value.Sub(base)
if dur.Seconds() <= 60 {
return "in a few seconds"
}
if dur.Minutes() < 5 {
return "in a few minutes"
}
if dur.Minutes() < 60 {
return fmt.Sprintf("in %.0f minutes", dur.Minutes())
}
if dur.Hours() < 24 {
return fmt.Sprintf("in %.0f hours", dur.Hours())
}
days := math.Floor(dur.Hours() / 24)
if days == 1 {
return "tomorrow"
}
if days == 2 {
return "day after tomorrow"
}
if days < 30 {
return fmt.Sprintf("in %.0f days", days)
}
months := math.Floor(days / 30)
if months == 1 {
return "next month"
}
if months < 12 {
return fmt.Sprintf("in %.0f months", months)
}
years := math.Floor(months / 12)
if years == 1 {
return "next year"
}
return fmt.Sprintf("in %.0f years", years)
}
func pastNaturalTime(base, value time.Time) string {
dur := base.Sub(value)
if dur.Seconds() <= 60 {
return "a few seconds ago"
}
if dur.Minutes() < 5 {
return "a few minutes ago"
}
if dur.Minutes() < 60 {
return fmt.Sprintf("%.0f minutes ago", dur.Minutes())
}
days := math.Floor(dur.Hours() / 24)
startBase := time.Date(base.Year(), base.Month(), base.Day(), 0, 0, 0, 0, time.UTC)
yesterday := startBase.Add(-24 * time.Hour)
dayBeforeYesterday := yesterday.Add(-24 * time.Hour)
//fmt.Println(value, days, startBase, yesterday, dayBeforeYesterday)
if value.After(startBase) {
return fmt.Sprintf("%.0f hours ago", dur.Hours())
}
if value.After(yesterday) {
return "yesterday"
}
if value.After(dayBeforeYesterday) {
return "day before yesterday"
}
if days < 30 {
return fmt.Sprintf("%.0f days ago", days)
}
months := math.Floor(days / 30)
if months == 1 {
return "last month"
}
if months < 12 {
return fmt.Sprintf("%.0f months ago", months)
}
years := math.Floor(months / 12)
if years == 1 {
return "last year"
}
return fmt.Sprintf("%.0f years ago", years)
}
Loading…
Cancel
Save