Created
September 28, 2019 05:02
-
-
Save timakin/d3d91128c9a0e3f5208c22a17edf6285 to your computer and use it in GitHub Desktop.
Test Utility for time package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package library | |
import "time" | |
var fakeTime time.Time | |
func SetFakeTime(t time.Time) { | |
fakeTime = t | |
} | |
func ResetFake() { | |
fakeTime = time.Time{} | |
} | |
// TimeNow ... 現在時刻(JST)を返す | |
func TimeNow() time.Time { | |
if !fakeTime.IsZero() { | |
return fakeTime | |
} | |
return ToJST(time.Now()) | |
} | |
// ToJST ... 時刻をJSTに変換する | |
func ToJST(t time.Time) time.Time { | |
jstloc := time.FixedZone("Asia/Tokyo", 9*60*60) | |
jstTime := t.UTC().In(jstloc) | |
return jstTime | |
} | |
// TruncatedTime ... 日付以外を切り捨てた時刻を取得 | |
func TruncatedTime(t time.Time) time.Time { | |
return t.Truncate(time.Hour).Add(-time.Duration(t.Hour()) * time.Hour) | |
} | |
// TruncatedNow ... 今日の日付を取得 | |
func TruncatedNow() time.Time { | |
return TruncatedTime(TimeNow()) | |
} | |
// TruncateUnix ... UnixTimeStampを切り捨て済みの日付に変換する | |
func TruncateUnix(u int64) time.Time { | |
t := time.Unix(u, 0) | |
return TruncatedTime(t) | |
} | |
// IsOverOffsetTime ... 現在時刻が基準時刻を過ぎているかどうか | |
func IsOverOffsetTime(now time.Time, ot time.Duration) bool { | |
ost := TruncatedNow().Add(ot) | |
return now.After(ost) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment