From b25bf0e16cedac0d6291eab8fe038776ac923942 Mon Sep 17 00:00:00 2001 From: lealife Date: Sat, 28 Nov 2015 13:49:00 +0800 Subject: [PATCH] =?UTF-8?q?util=20=E6=97=B6=E9=97=B4=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/lea/Util.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/app/lea/Util.go b/app/lea/Util.go index 4ff66f7..b2ea077 100644 --- a/app/lea/Util.go +++ b/app/lea/Util.go @@ -394,3 +394,40 @@ func FixFilename(filename string) string { } return filename } + +// 是否是合法的时间 +// 不是, 0001-01-01T00:00:00Z, 且比今天小 +func IsValidTime(t time.Time) bool { + if t.Year() > 20 { + now := time.Now() + Log("------") + Log(t) + Log(now) + if t.Before(now) { + return true + } + } + return false +} + +// url传过来的时间没有时区信息, 转到本地时间 +func ToLocalTime(t time.Time) time.Time { + return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local) +} + +// 修复传过来的时间, 如果比今天大, 则设为现在 +func FixUrlTime(t time.Time) time.Time { + localTime := ToLocalTime(t) + if IsValidTime(localTime) { + return localTime + } + return time.Now() +} + +// 得到用户的随机文件路径 3位/userId/2位 +func GetRandomFilePath(userId, uuid string) string { + if uuid == "" { + uuid = NewGuid() + } + return Digest3(userId) + "/" + userId + "/" + Digest2(uuid) +}