mirror of https://github.com/jlelse/GoBlog
Strict use of local dates
This commit is contained in:
parent
6256b2fdd9
commit
1025a726ef
12
database.go
12
database.go
|
@ -4,7 +4,7 @@ import (
|
|||
"database/sql"
|
||||
"sync"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
sqlite "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -15,7 +15,15 @@ var (
|
|||
)
|
||||
|
||||
func initDatabase() (err error) {
|
||||
appDb, err = sql.Open("sqlite3", appConfig.Db.File+"?cache=shared&mode=rwc&_journal_mode=WAL")
|
||||
sql.Register("goblog_db", &sqlite.SQLiteDriver{
|
||||
ConnectHook: func(c *sqlite.SQLiteConn) error {
|
||||
if err := c.RegisterFunc("tolocal", toLocalSafe, true); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
})
|
||||
appDb, err = sql.Open("goblog_db", appConfig.Db.File+"?cache=shared&mode=rwc&_journal_mode=WAL")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -87,6 +87,16 @@ func migrateDb() error {
|
|||
return err
|
||||
},
|
||||
},
|
||||
&migrator.Migration{
|
||||
Name: "00007",
|
||||
Func: func(tx *sql.Tx) error {
|
||||
// Change all dates to local
|
||||
_, err := tx.Exec(`
|
||||
update posts set published = tolocal(published), updated = tolocal(updated);
|
||||
`)
|
||||
return err
|
||||
},
|
||||
},
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
|
|
|
@ -398,7 +398,7 @@ func (p *post) computeExtraPostParameters() error {
|
|||
}
|
||||
if p.Published == "" && p.Section != "" {
|
||||
// Has no published date, but section -> published now
|
||||
p.Published = time.Now().String()
|
||||
p.Published = time.Now().Local().String()
|
||||
}
|
||||
// Add images not in content
|
||||
images := p.Parameters[appConfig.Micropub.PhotoParam]
|
||||
|
|
12
postsDb.go
12
postsDb.go
|
@ -12,7 +12,7 @@ import (
|
|||
"github.com/araddon/dateparse"
|
||||
)
|
||||
|
||||
func (p *post) checkPost() error {
|
||||
func (p *post) checkPost() (err error) {
|
||||
if p == nil {
|
||||
return errors.New("no post")
|
||||
}
|
||||
|
@ -21,18 +21,16 @@ func (p *post) checkPost() error {
|
|||
p.Content = strings.TrimSuffix(strings.TrimPrefix(p.Content, "\n"), "\n")
|
||||
// Fix date strings
|
||||
if p.Published != "" {
|
||||
d, err := dateparse.ParseLocal(p.Published)
|
||||
p.Published, err = toLocal(p.Published)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.Published = d.String()
|
||||
}
|
||||
if p.Updated != "" {
|
||||
d, err := dateparse.ParseLocal(p.Updated)
|
||||
p.Updated, err = toLocal(p.Updated)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.Updated = d.String()
|
||||
}
|
||||
// Cleanup params
|
||||
for key, value := range p.Parameters {
|
||||
|
@ -315,6 +313,10 @@ func getPosts(config *postsRequestConfig) (posts []*post, err error) {
|
|||
index := len(posts)
|
||||
paths[p.Path] = index + 1
|
||||
p.Parameters = map[string][]string{}
|
||||
// Fix dates
|
||||
p.Published = toLocalSafe(p.Published)
|
||||
p.Updated = toLocalSafe(p.Updated)
|
||||
// Append
|
||||
posts = append(posts, p)
|
||||
}
|
||||
if parameterName != "" && posts != nil {
|
||||
|
|
10
render.go
10
render.go
|
@ -91,7 +91,7 @@ func initRendering() error {
|
|||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return d.Format(format)
|
||||
return d.Local().Format(format)
|
||||
},
|
||||
"longdate": func(date string, localeString string) string {
|
||||
d, err := dateparse.ParseLocal(date)
|
||||
|
@ -99,20 +99,20 @@ func initRendering() error {
|
|||
return ""
|
||||
}
|
||||
ml := monday.Locale(localeString)
|
||||
return monday.Format(d, monday.LongFormatsByLocale[ml], ml)
|
||||
return monday.Format(d.Local(), monday.LongFormatsByLocale[ml], ml)
|
||||
},
|
||||
"unixtodate": func(unix int64) string {
|
||||
return time.Unix(unix, 0).String()
|
||||
return time.Unix(unix, 0).Local().String()
|
||||
},
|
||||
"now": func() string {
|
||||
return time.Now().String()
|
||||
return time.Now().Local().String()
|
||||
},
|
||||
"dateadd": func(date string, years, months, days int) string {
|
||||
d, err := dateparse.ParseLocal(date)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return d.AddDate(years, months, days).String()
|
||||
return d.AddDate(years, months, days).Local().String()
|
||||
},
|
||||
"datebefore": func(date string, before string) bool {
|
||||
d, err := dateparse.ParseLocal(date)
|
||||
|
|
17
utils.go
17
utils.go
|
@ -10,6 +10,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/araddon/dateparse"
|
||||
)
|
||||
|
||||
func urlize(str string) string {
|
||||
|
@ -139,3 +140,19 @@ func groupStrings(toGroup []string) []stringGroup {
|
|||
})
|
||||
return stringGroups
|
||||
}
|
||||
|
||||
func toLocalSafe(s string) string {
|
||||
d, _ := toLocal(s)
|
||||
return d
|
||||
}
|
||||
|
||||
func toLocal(s string) (string, error) {
|
||||
if s == "" {
|
||||
return "", nil
|
||||
}
|
||||
d, err := dateparse.ParseLocal(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return d.Local().String(), nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue