Browse Source

Display timestamps relative to the current date

This is not perfect yet (by far), but it's good enough for a demo.
Lucas Stadler 8 years ago
parent
commit
d834b6feca
1 changed files with 29 additions and 3 deletions
  1. 29 3
      quit.go

+ 29 - 3
quit.go

@ -2,6 +2,7 @@ package main
2 2
3 3
import (
4 4
	"bytes"
5
	"fmt"
5 6
	"html/template"
6 7
	"io"
7 8
	"log"
@ -182,8 +183,33 @@ func (fc *FancyCommit) Author() string {
182 183
	return fc.commit.Author().Name
183 184
}
184 185
185
func (fc *FancyCommit) Date() time.Time {
186
	return fc.commit.Author().When
186
func (fc *FancyCommit) Date() *FancyTime {
187
	return &FancyTime{fc.commit.Author().When}
188
}
189
190
type FancyTime struct {
191
	time.Time
192
}
193
194
func (fc FancyTime) PrettyString() string {
195
	now := time.Now()
196
	diff := time.Since(fc.Time)
197
	switch {
198
	case diff < time.Minute:
199
		return fmt.Sprintf("%d seconds ago", diff/time.Second)
200
	case diff < time.Hour:
201
		return fmt.Sprintf("%d minutes ago", diff/time.Minute)
202
	case now.AddDate(0, 0, -1).Before(fc.Time):
203
		return fmt.Sprintf("%d hours ago", diff/time.Hour)
204
	case now.AddDate(0, 0, -7).Before(fc.Time):
205
		return fmt.Sprintf("%d days ago", diff/(24*time.Hour))
206
	case now.AddDate(0, -1, 0).Before(fc.Time):
207
		return fmt.Sprintf("%d weeks ago", diff/(7*24*time.Hour))
208
	case now.AddDate(-1, 0, 0).Before(fc.Time):
209
		return fmt.Sprintf("%d months ago", diff/(30*7*24*time.Hour))
210
	default:
211
		return fmt.Sprintf("%d years ago", diff/(365*7*24*time.Hour))
212
	}
187 213
}
188 214
189 215
func (fc *FancyCommit) Id(n int) string {
@ -325,7 +351,7 @@ var repoTmpl = template.Must(template.New("").Parse(`<!doctype html>
325 351
			<div class="commit-author">{{ .Repo.Commit.Author }}</div>
326 352
			<div class="commit-summary" title="{{ .Repo.Commit.Description }}">{{ .Repo.Commit.Summary }}</div>
327 353
			<div class="commit-id" data-commit="{{ .Repo.Commit.Id 0 }}"><a href="#">{{ .Repo.Commit.Id 10 }}</a></div>
328
			<time class="commit-date">{{ .Repo.Commit.Date }}</time>
354
			<time class="commit-date" title="{{ .Repo.Commit.Date }}">{{ .Repo.Commit.Date.PrettyString }}</time>
329 355
		</section>
330 356
331 357
		<table id="files">