Basic timezone support.

Digests are generated for day boundaries in the given timezone, and timestamps
that are displayed are in that timezone too. No UI for actually specifying a
timezone.
This commit is contained in:
Mihai Parparita 2014-08-03 23:00:48 -07:00
parent b76c164d45
commit fe11e2a97a
4 changed files with 46 additions and 20 deletions

View file

@ -3,6 +3,7 @@ package githop
import (
"bytes"
"encoding/gob"
"time"
"appengine"
"appengine/datastore"
@ -15,7 +16,9 @@ type Account struct {
// The datastore API doesn't store maps, and the token contains one. We
// thefore store a gob-serialized version instead.
OAuthTokenSerialized []byte
OAuthToken oauth.Token `datastore:"-,"`
OAuthToken oauth.Token `datastore:"-,"`
TimezoneName string `datastore:",noindex"`
TimezoneLocation *time.Location `datastore:"-,"`
}
func getAccount(c appengine.Context, gitHubUserId int) (*Account, error) {
@ -25,9 +28,22 @@ func getAccount(c appengine.Context, gitHubUserId int) (*Account, error) {
if err != nil {
return nil, err
}
r := bytes.NewBuffer(account.OAuthTokenSerialized)
err = gob.NewDecoder(r).Decode(&account.OAuthToken)
return account, err
if err != nil {
return nil, err
}
if len(account.TimezoneName) == 0 {
account.TimezoneName = "America/Los_Angeles"
}
account.TimezoneLocation, err = time.LoadLocation(account.TimezoneName)
if err != nil {
return nil, err
}
return account, nil
}
func getAllAccounts(c appengine.Context, accounts *[]Account) error {

View file

@ -14,11 +14,11 @@ type DigestCommit struct {
URL string
Title string
Message string
Date *time.Time
Date time.Time
RepositoryCommit *github.RepositoryCommit
}
func newDigestCommit(commit *github.RepositoryCommit, repo *github.Repository) DigestCommit {
func newDigestCommit(commit *github.RepositoryCommit, repo *github.Repository, location *time.Location) DigestCommit {
messagePieces := strings.SplitN(*commit.Commit.Message, "\n", 2)
title := messagePieces[0]
message := ""
@ -30,11 +30,15 @@ func newDigestCommit(commit *github.RepositoryCommit, repo *github.Repository) D
URL: fmt.Sprintf("https://github.com/%s/commit/%s", *repo.FullName, *commit.SHA),
Title: title,
Message: message,
Date: commit.Commit.Author.Date,
Date: commit.Commit.Author.Date.In(location),
RepositoryCommit: commit,
}
}
func (commit DigestCommit) DisplayDate() string {
return commit.Date.Format("3:04pm")
}
type RepoDigest struct {
Repo *github.Repository
Commits []DigestCommit
@ -48,13 +52,14 @@ func (a ByRepoFullName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByRepoFullName) Less(i, j int) bool { return *a[i].Repo.FullName < *a[j].Repo.FullName }
type Digest struct {
User *github.User
StartTime time.Time
EndTime time.Time
RepoDigests []*RepoDigest
User *github.User
StartTime time.Time
EndTime time.Time
TimezoneLocation *time.Location
RepoDigests []*RepoDigest
}
func newDigest(githubClient *github.Client) (*Digest, error) {
func newDigest(githubClient *github.Client, account *Account) (*Digest, error) {
user, _, err := githubClient.Users.Get("")
if err != nil {
return nil, err
@ -82,7 +87,7 @@ func newDigest(githubClient *github.Client) (*Digest, error) {
repos = newRepos
}
now := time.Now()
now := time.Now().In(account.TimezoneLocation)
digestStartTime := time.Date(now.Year()-1, now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
digestEndTime := digestStartTime.AddDate(0, 0, 1)
@ -95,10 +100,11 @@ func newDigest(githubClient *github.Client) (*Digest, error) {
}
repos = digestRepos
digest := &Digest{
User: user,
RepoDigests: make([]*RepoDigest, 0, len(repos)),
StartTime: digestStartTime,
EndTime: digestEndTime,
User: user,
RepoDigests: make([]*RepoDigest, 0, len(repos)),
StartTime: digestStartTime,
EndTime: digestEndTime,
TimezoneLocation: account.TimezoneLocation,
}
err = digest.fetch(repos, githubClient)
return digest, err
@ -125,7 +131,7 @@ func (digest *Digest) fetch(repos []github.Repository, githubClient *github.Clie
} else {
digestCommits := make([]DigestCommit, 0, len(commits))
for i, _ := range commits {
digestCommits = append(digestCommits, newDigestCommit(&commits[i], &repo))
digestCommits = append(digestCommits, newDigestCommit(&commits[i], &repo, digest.TimezoneLocation))
}
ch <- &RepoDigestResponse{&RepoDigest{&repo, digestCommits}, nil}
}
@ -145,3 +151,7 @@ func (digest *Digest) fetch(repos []github.Repository, githubClient *github.Clie
sort.Sort(ByRepoFullName(digest.RepoDigests))
return nil
}
func (digest *Digest) DisplayDate() string {
return digest.StartTime.Format("January 2, 2006 was a Monday")
}

View file

@ -102,7 +102,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
oauthTransport.Token = &account.OAuthToken
githubClient := github.NewClient(oauthTransport.Client())
digest, err := newDigest(githubClient)
digest, err := newDigest(githubClient, account)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
@ -159,7 +159,7 @@ func sendDigestForAccount(account *Account, c appengine.Context) error {
oauthTransport.Token = &account.OAuthToken
githubClient := github.NewClient(oauthTransport.Client())
digest, err := newDigest(githubClient)
digest, err := newDigest(githubClient, account)
if err != nil {
return err
}

View file

@ -2,7 +2,7 @@
<dl>
<p>Activity a year ago for <a href="https://github.com/{{.User.Login}}" title={{.User.Name}}><img src={{.User.AvatarURL}} width="20" height="20" border="0">{{.User.Login}}</a>.</p>
<p>{{.DisplayDate}}. Here's the GitHub activity on that day for <a href="https://github.com/{{.User.Login}}" title={{.User.Name}}><img src={{.User.AvatarURL}} width="20" height="20" border="0">{{.User.Login}}</a>.</p>
{{range $index, $repoDigest := .RepoDigests}}
<h2>
@ -20,7 +20,7 @@
{{end}}
<div>
<a href="{{.URL}}">{{.DisplaySHA}}</a>
<i>{{.Date}}</i>
<i>{{.DisplayDate}}</i>
</div>
</div>
{{end}}