xtime

package
v0.0.0-...-ed9fe1d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2018 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package xtime provides extended utility functions for time

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultFormatter = &Formatter{
	DateSeperator:    "/",
	TimeSeperator:    ":",
	ShowSeconds:      false,
	humanOffsetHours: 0,
}

DefaultFormatter is a default Formatter

View Source
var JST = time.FixedZone("JST", 9*60*60)

JST timezone

Functions

func Benchmark

func Benchmark(f func()) time.Duration

Benchmark measure the time.Duration for func f execution.

func FormatDateString

func FormatDateString(t time.Time) string

FormatDateString is a shortcut for DefaultFormatter.FormatDateString

func FormatDateTimeString

func FormatDateTimeString(t time.Time) string

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

func FormatTimeString(t time.Time) string

FormatTimeString is a shortcut for DefaultFormatter.FormatTimeString

func HumanToday

func HumanToday(offsetHours int) time.Time

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

func HumanTodayIn(offsetHours int, loc *time.Location) time.Time

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 MustParse

func MustParse(value string) time.Time

MustParse is like Parse but panic if an error occurrs.

func MustParseDate

func MustParseDate(s string, location *time.Location, hintYear int) time.Time

MustParseDate is like ParseDate but panic if an error occurrs.

func MustParseTime

func MustParseTime(s string, location *time.Location) time.Time

MustParseTime is like ParseTime but panic if an error occurrs.

func Now

func Now() time.Time

Now returns time.Now() but you can set fixed return value by RunAt function

func Parse

func Parse(value string) (time.Time, error)

Parse parse RFC3339 format by default.

func ParseDate

func ParseDate(s string, location *time.Location, hintYear int) (time.Time, error)

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

func ParseJPDate(s string, hintYear int) time.Time

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

func ParseTime(s string, location *time.Location) (time.Time, error)

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

func RunAt(t time.Time, f func())

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

func SleepAndEnsureAfter(base time.Time, d time.Duration)

SleepAndEnsureAfter sleeps for several time to ensure the duration passes after the base time for the routines that executes at a regular cycle.

func Today

func Today() time.Time

Today returns time.Today() but you can set fixed return value by RunAt function

func WaitAndEnsureAfter

func WaitAndEnsureAfter(base time.Time, d time.Duration)

WaitAndEnsureAfter waits for several time and ensure the duration passes after the base time.

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

func (f *Formatter) FormatDateString(t time.Time) string

FormatDateString formats the date part of a time.Time to string.

func (*Formatter) FormatDateTimeString

func (f *Formatter) FormatDateTimeString(t time.Time) string

FormatDateTimeString formats the date and time part of a time.Time to string..

func (*Formatter) FormatTimeString

func (f *Formatter) FormatTimeString(t time.Time) string

FormatTimeString formats the time part of a time.Time to string.

func (*Formatter) Humanize

func (f *Formatter) Humanize(offsetHours int) *Formatter

Humanize returns a new formatter to use humanized offset date/hours to format.

Example
t := time.Date(
	2015, 5, 4, 2, 10, 0, 0, time.UTC,
)
fmt.Println(DefaultFormatter.Humanize(4).FormatDateTimeString(t))
Output:

2015/05/03 26:10

type Timestamp

type Timestamp time.Time

Timestamp is a time.Time alias that uses timestamp serialization for json.

func (*Timestamp) MarshalJSON

func (t *Timestamp) MarshalJSON() ([]byte, error)

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL