summaryrefslogtreecommitdiffstats
path: root/modules/base
diff options
context:
space:
mode:
authorslene <vslene@gmail.com>2014-03-23 01:45:21 +0800
committerslene <vslene@gmail.com>2014-03-23 01:45:21 +0800
commit5edd57e4822804aef2af64b1ede99cd6e977b143 (patch)
tree2c7bcd573d072771f64685d05a95ddcdcb9754d5 /modules/base
parent076fc98d981aea3533eea363ca1c7e43f77b9802 (diff)
parentcd070b56d2f5b34236174ff04ccc459d15c4cd17 (diff)
downloadgitea-5edd57e4822804aef2af64b1ede99cd6e977b143.tar.gz
gitea-5edd57e4822804aef2af64b1ede99cd6e977b143.zip
Merge branch 'master' of github.com:gogits/gogs
Diffstat (limited to 'modules/base')
-rw-r--r--modules/base/conf.go16
-rw-r--r--modules/base/tool.go84
2 files changed, 92 insertions, 8 deletions
diff --git a/modules/base/conf.go b/modules/base/conf.go
index d5e27d043b..7c8ed93654 100644
--- a/modules/base/conf.go
+++ b/modules/base/conf.go
@@ -41,19 +41,19 @@ var (
Cfg *goconfig.ConfigFile
MailService *Mailer
+ LogMode string
+ LogConfig string
+
Cache cache.Cache
CacheAdapter string
CacheConfig string
- PictureService string
- PictureRootPath string
-
- LogMode string
- LogConfig string
-
SessionProvider string
SessionConfig *session.Config
SessionManager *session.Manager
+
+ PictureService string
+ PictureRootPath string
)
var Service struct {
@@ -182,6 +182,10 @@ func newSessionService() {
SessionConfig.SessionIDHashFunc = Cfg.MustValue("session", "SESSION_ID_HASHFUNC", "sha1")
SessionConfig.SessionIDHashKey = Cfg.MustValue("session", "SESSION_ID_HASHKEY")
+ if SessionProvider == "file" {
+ os.MkdirAll(path.Dir(SessionConfig.ProviderConfig), os.ModePerm)
+ }
+
var err error
SessionManager, err = session.NewManager(SessionProvider, *SessionConfig)
if err != nil {
diff --git a/modules/base/tool.go b/modules/base/tool.go
index a2aeebf1b8..c7ee2ee857 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -115,6 +115,85 @@ const (
Year = 12 * Month
)
+func computeTimeDiff(diff int64) (int64, string) {
+ diffStr := ""
+ switch {
+ case diff <= 0:
+ diff = 0
+ diffStr = "now"
+ case diff < 2:
+ diff = 0
+ diffStr = "1 second"
+ case diff < 1*Minute:
+ diffStr = fmt.Sprintf("%d seconds", diff)
+ diff = 0
+
+ case diff < 2*Minute:
+ diff -= 1 * Minute
+ diffStr = "1 minute"
+ case diff < 1*Hour:
+ diffStr = fmt.Sprintf("%d minutes", diff/Minute)
+ diff -= diff / Minute * Minute
+
+ case diff < 2*Hour:
+ diff -= 1 * Hour
+ diffStr = "1 hour"
+ case diff < 1*Day:
+ diffStr = fmt.Sprintf("%d hours", diff/Hour)
+ diff -= diff / Hour * Hour
+
+ case diff < 2*Day:
+ diff -= 1 * Day
+ diffStr = "1 day"
+ case diff < 1*Week:
+ diffStr = fmt.Sprintf("%d days", diff/Day)
+ diff -= diff / Day * Day
+
+ case diff < 2*Week:
+ diff -= 1 * Week
+ diffStr = "1 week"
+ case diff < 1*Month:
+ diffStr = fmt.Sprintf("%d weeks", diff/Week)
+ diff -= diff / Week * Week
+
+ case diff < 2*Month:
+ diff -= 1 * Month
+ diffStr = "1 month"
+ case diff < 1*Year:
+ diffStr = fmt.Sprintf("%d months", diff/Month)
+ diff -= diff / Month * Month
+
+ case diff < 2*Year:
+ diff -= 1 * Year
+ diffStr = "1 year"
+ default:
+ diffStr = fmt.Sprintf("%d years", diff/Year)
+ diff = 0
+ }
+ return diff, diffStr
+}
+
+// TimeSincePro calculates the time interval and generate full user-friendly string.
+func TimeSincePro(then time.Time) string {
+ now := time.Now()
+ diff := now.Unix() - then.Unix()
+
+ if then.After(now) {
+ return "future"
+ }
+
+ var timeStr, diffStr string
+ for {
+ if diff == 0 {
+ break
+ }
+
+ diff, diffStr = computeTimeDiff(diff)
+ timeStr += ", " + diffStr
+ }
+ return strings.TrimPrefix(timeStr, ", ")
+}
+
// TimeSince calculates the time interval and generate user-friendly string.
func TimeSince(then time.Time) string {
now := time.Now()
@@ -127,7 +206,6 @@ func TimeSince(then time.Time) string {
}
switch {
-
case diff <= 0:
return "now"
case diff <= 2:
@@ -160,8 +238,10 @@ func TimeSince(then time.Time) string {
case diff < 1*Year:
return fmt.Sprintf("%d months %s", diff/Month, lbl)
- case diff < 18*Month:
+ case diff < 2*Year:
return fmt.Sprintf("1 year %s", lbl)
+ default:
+ return fmt.Sprintf("%d years %s", diff/Year, lbl)
}
return then.String()
}