Documentation ¶
Overview ¶
Package xtime provides extended utility functions for time
Index ¶
- Variables
- func Benchmark(f func()) time.Duration
- func FormatDateString(t time.Time) string
- func FormatDateTimeString(t time.Time) string
- func FormatTimeString(t time.Time) string
- func HumanToday(offsetHours int) time.Time
- func HumanTodayIn(offsetHours int, loc *time.Location) time.Time
- func MustParse(value string) time.Time
- func MustParseDate(s string, location *time.Location, hintYear int) time.Time
- func MustParseTime(s string, location *time.Location) time.Time
- func Now() time.Time
- func Parse(value string) (time.Time, error)
- func ParseDate(s string, location *time.Location, hintYear int) (time.Time, error)
- func ParseJPDate(s string, hintYear int) time.Time
- func ParseTime(s string, location *time.Location) (time.Time, error)
- func RunAt(t time.Time, f func())
- func SleepAndEnsureAfter(base time.Time, d time.Duration)
- func Today() time.Time
- func WaitAndEnsureAfter(base time.Time, d time.Duration)
- type Formatter
- type Timestamp
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultFormatter = &Formatter{ DateSeperator: "/", TimeSeperator: ":", ShowSeconds: false, humanOffsetHours: 0, }
DefaultFormatter is a default Formatter
var JST = time.FixedZone("JST", 9*60*60)
JST timezone
Functions ¶
func FormatDateString ¶
FormatDateString is a shortcut for DefaultFormatter.FormatDateString
func FormatDateTimeString ¶
FormatDateTimeString is a shortcut for DefaultFormatter.FormatDateTimeString
Example ¶
t := time.Date( 2015, 5, 4, 2, 10, 0, 0, time.UTC, ) fmt.Println(FormatDateTimeString(t))
Output: 2015/05/04 02:10
func FormatTimeString ¶
FormatTimeString is a shortcut for DefaultFormatter.FormatTimeString
func HumanToday ¶
HumanToday returns Today for humans (considering time offset) The time between 0am - 4am should be considerd as the previous day if the offset is 4.
Example ¶
t := time.Date( 2016, 5, 4, 2, 10, 0, 0, time.UTC, ) RunAt(t, func() { fmt.Println(HumanToday(4)) })
Output: 2016-05-03 04:00:00 +0000 UTC
func HumanTodayIn ¶
HumanTodayIn returns Today for humans (considering time offset) in the given location.
Example ¶
// Case if we want to get today of different location from system one. // Now returns 2016/05/04 02:10 am in UTC, which is 2016/05/04 11:10 am in JST // So human today for JST should return 2016/05/04 04:00, not 2016/05/03 04:00 t := time.Date( 2016, 5, 4, 2, 10, 0, 0, time.UTC, ) RunAt(t, func() { fmt.Println(HumanTodayIn(4, JST)) })
Output: 2016-05-04 04:00:00 +0900 JST
func MustParseDate ¶
MustParseDate is like ParseDate but panic if an error occurrs.
func MustParseTime ¶
MustParseTime is like ParseTime but panic if an error occurrs.
func ParseDate ¶
ParseDate parse the string expression of YYYY/MM/DD formatted time and returns it as time.Time YYYY/ can be omitted and hintYear is used for that case. The delimiter can be either '/' or '-'
func ParseJPDate ¶
ParseJPDate parse the string expression of JP date YYYY年MM月DD日 and returns it as time.Time YYYY年 can be omitted and hintYear is used for that case. If `hintYear` is 0, Now().Year() is used as an year.
func ParseTime ¶
ParseTime parse the string expression of HH:MM:SS formatted time and returns it as time.Time :SS can be omitted and hintYear is used for that case
func RunAt ¶
RunAt sets the base time for Now(). This is not concurrently safe so don't use this for parallel tests.
Example ¶
t := time.Date( 2016, 1, 1, 13, 12, 0, 0, time.UTC, ) RunAt(t, func() { fmt.Println(Now()) fmt.Println(Today()) })
Output: 2016-01-01 13:12:00 +0000 UTC 2016-01-01 00:00:00 +0000 UTC
func SleepAndEnsureAfter ¶
SleepAndEnsureAfter sleeps for several time to ensure the duration passes after the base time for the routines that executes at a regular cycle.
Types ¶
type Formatter ¶
type Formatter struct { DateSeperator string TimeSeperator string ShowSeconds bool // contains filtered or unexported fields }
Formatter provides format functions for time
func (*Formatter) FormatDateString ¶
FormatDateString formats the date part of a time.Time to string.
func (*Formatter) FormatDateTimeString ¶
FormatDateTimeString formats the date and time part of a time.Time to string..
func (*Formatter) FormatTimeString ¶
FormatTimeString formats the time part of a time.Time to string.