Documentation ¶
Overview ¶
Package times implements time utility functions.
Format* : time --> string Parse* : time <-- string time.Unix : time --> unix Unix : time <-- unix FormatUnix* : unix --> string ParseUnix* : unix <-- string
Add : time + d AddUnix : unix + d AddStr : string + d
Index ¶
- Constants
- func Add(t time.Time, d Duration) time.Time
- func AddGMT(t string, d Duration) (string, error)
- func AddStr(layout, t string, d Duration) (string, error)
- func AddUnix(t int64, d Duration) int64
- func AddYYMD(t string, d Duration) (string, error)
- func AddYYMDHM(t string, d Duration) (string, error)
- func AddYYMDHMS(t string, d Duration) (string, error)
- func Format(layout string, t time.Time) string
- func FormatGMT(t time.Time) string
- func FormatUnix(layout string, i int64) string
- func FormatUnixGMT(i int64) string
- func FormatUnixYYMD(i int64) string
- func FormatUnixYYMDHM(i int64) string
- func FormatUnixYYMDHMS(i int64) string
- func FormatYYMD(t time.Time) string
- func FormatYYMDHM(t time.Time) string
- func FormatYYMDHMS(t time.Time) string
- func Now() time.Time
- func NowUnix() int64
- func NowUnixStr() string
- func Parse(layout, value string) (out time.Time, err error)
- func ParseGMT(value string) (time.Time, error)
- func ParseUnix(layout, value string) (int64, error)
- func ParseUnixGMT(value string) (int64, error)
- func ParseUnixGmt(value string) (string, error)
- func ParseUnixNano(layout, value string) (int64, error)
- func ParseUnixStr(layout, value string) (string, error)
- func ParseUnixYYMD(value string) (int64, error)
- func ParseUnixYYMDHM(value string) (int64, error)
- func ParseUnixYYMDHMS(value string) (int64, error)
- func ParseUnixYymd(value string) (string, error)
- func ParseUnixYymdhm(value string) (string, error)
- func ParseUnixYymdhms(value string) (string, error)
- func ParseYYMD(value string) (time.Time, error)
- func ParseYYMDHM(value string) (time.Time, error)
- func ParseYYMDHMS(value string) (time.Time, error)
- func Unix(sec int64, nsec int64) time.Time
- type Duration
Examples ¶
- Add
- AddGMT
- AddStr
- AddUnix
- AddYYMD
- AddYYMDHM
- AddYYMDHMS
- Format
- FormatGMT
- FormatUnix
- FormatUnixGMT
- FormatUnixYYMD
- FormatUnixYYMDHM
- FormatUnixYYMDHMS
- FormatYYMD
- FormatYYMDHM
- FormatYYMDHMS
- Parse
- ParseGMT
- ParseUnix
- ParseUnixGMT
- ParseUnixGmt
- ParseUnixNano
- ParseUnixStr
- ParseUnixYYMD
- ParseUnixYYMDHM
- ParseUnixYYMDHMS
- ParseUnixYymd
- ParseUnixYymdhm
- ParseUnixYymdhms
- ParseYYMD
- ParseYYMDHM
- ParseYYMDHMS
Constants ¶
const ( // Default returns the default value of time. Default int64 = -62135596800 // YYMD 2006-01-02 YYMD = "2006-01-02" // YYMDHMS 2006-01-02 15:04:05 YYMDHMS = "2006-01-02 15:04:05" // YYMDHM 2006-01-02 15:04 YYMDHM = "2006-01-02 15:04" // GMT Mon, 02 Jan 2006 15:04:05 GMT GMT = "Mon, 02 Jan 2006 15:04:05 GMT" // Nanosecond 1 Nanosecond Duration = 1 // Microsecond 1000 * Nanosecond Microsecond = 1000 * Nanosecond // Millisecond 1000 * Microsecond Millisecond = 1000 * Microsecond // Second 1000 * Millisecond Second = 1000 * Millisecond // Minute 60 * Second Minute = 60 * Second // Hour 60 * Minute Hour = 60 * Minute // Day 24 * Hour Day = 24 * Hour )
Variables ¶
This section is empty.
Functions ¶
func Add ¶
Add returns the time t+d.
Example ¶
fmt.Println(times.Add(in, times.Day))
Output: 2014-04-04 13:31:45.001234454 +0800 CST
func AddGMT ¶
AddGMT returns the time t+d of unix string. The layout is "Mon, 02 Jan 2006 15:04:05 GMT"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { out, _ := times.AddGMT("Thu, 03 Apr 2014 13:31:45 GMT", times.Day) fmt.Println(out) }
Output: Fri, 04 Apr 2014 13:31:45 GMT
func AddStr ¶
AddStr returns the time t+d of unix string.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { out, _ := times.AddStr(times.YYMDHMS, "2014-04-03 13:31:45", times.Day) fmt.Println(out) }
Output: 2014-04-04 13:31:45
func AddUnix ¶
AddUnix returns the time t+d.
Example ¶
fmt.Println(times.AddUnix(i, times.Day))
Output: 1396589505
func AddYYMD ¶
AddYYMD returns the time t+d of unix string. The layout is "2006-01-02"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { out, _ := times.AddYYMD("2014-04-03", times.Day) fmt.Println(out) }
Output: 2014-04-04
func AddYYMDHM ¶
AddYYMDHM returns the time t+d of unix string. The layout is "2006-01-02 15:04"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { out, _ := times.AddYYMDHM("2014-04-03 13:31", times.Day) fmt.Println(out) }
Output: 2014-04-04 13:31
func AddYYMDHMS ¶
AddYYMDHMS returns the time t+d of unix string. The layout is "2006-01-02 15:04:05"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { out, _ := times.AddYYMDHMS("2014-04-03 13:31:45", times.Day) fmt.Println(out) }
Output: 2014-04-04 13:31:45
func Format ¶
Format returns a textual representation of the time value formatted according to layout, which defines the format by showing how the reference time, defined to be
Mon Jan 2 15:04:05 -0700 MST 2006
would be displayed if it were the value; it serves as an example of the desired output. The same display rules will then be applied to the time value.
A fractional second is represented by adding a period and zeros to the end of the seconds section of layout string, as in "15:04:05.000" to format a time stamp with millisecond precision.
Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard and convenient representations of the reference time. For more information about the formats and the definition of the reference time, see the documentation for ANSIC and the other constants defined by this package.
Example ¶
fmt.Println(times.Format(times.YYMDHMS, in))
Output: 2014-04-03 13:31:45
func FormatGMT ¶
FormatGMT time formatted as Mon, 02 Jan 2006 15:04:05 GMT.
Example ¶
fmt.Println(times.FormatGMT(in))
Output: Thu, 03 Apr 2014 13:31:45 GMT
func FormatUnix ¶
FormatUnix returns a textual representation of the time value formatted according to layout, which defines the format by showing how the reference time, defined to be
Mon Jan 2 15:04:05 -0700 MST 2006
would be displayed if it were the value; it serves as an example of the desired output. The same display rules will then be applied to the time value.
A fractional second is represented by adding a period and zeros to the end of the seconds section of layout string, as in "15:04:05.000" to format a time stamp with millisecond precision.
Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard and convenient representations of the reference time. For more information about the formats and the definition of the reference time, see the documentation for ANSIC and the other constants defined by this package.
Example ¶
fmt.Println(times.FormatUnix(times.YYMDHMS, i))
Output: 2014-04-03 13:31:45
func FormatUnixGMT ¶
FormatUnixGMT time formatted as Mon, 02 Jan 2006 15:04:05 GMT.
Example ¶
fmt.Println(times.FormatUnixGMT(i))
Output: Thu, 03 Apr 2014 13:31:45 GMT
func FormatUnixYYMD ¶
FormatUnixYYMD time formatted as 2006-01-02.
Example ¶
fmt.Println(times.FormatUnixYYMD(i))
Output: 2014-04-03
func FormatUnixYYMDHM ¶
FormatUnixYYMDHM time formatted as 2006-01-02 15:04.
Example ¶
fmt.Println(times.FormatUnixYYMDHM(i))
Output: 2014-04-03 13:31
func FormatUnixYYMDHMS ¶
FormatUnixYYMDHMS time formatted as 2006-01-02 15:04:05.
Example ¶
fmt.Println(times.FormatUnixYYMDHMS(i))
Output: 2014-04-03 13:31:45
func FormatYYMD ¶
FormatYYMD time formatted as 2006-01-02.
Example ¶
fmt.Println(times.FormatYYMD(in))
Output: 2014-04-03
func FormatYYMDHM ¶
FormatYYMDHM time formatted as 2006-01-02 15:04.
Example ¶
fmt.Println(times.FormatYYMDHM(in))
Output: 2014-04-03 13:31
func FormatYYMDHMS ¶
FormatYYMDHMS time formatted as 2006-01-02 15:04:05.
Example ¶
fmt.Println(times.FormatYYMDHMS(in))
Output: 2014-04-03 13:31:45
func Parse ¶
Parse parses a formatted string and returns the time value it represents. The layout defines the format by showing how the reference time.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.Parse(times.YYMDHMS, "2014-04-03 13:31:45") fmt.Printf("%v", v) }
Output: 2014-04-03 13:31:45 +0800 CST
func ParseGMT ¶
ParseGMT parses a formatted string and returns the time value it represents. The layout is "Mon, 02 Jan 2006 15:04:05 GMT"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseGMT("Thu, 03 Apr 2014 13:31:45 GMT") fmt.Printf("%v", v) }
Output: 2014-04-03 13:31:45 +0800 CST
func ParseUnix ¶
ParseUnix parses a formatted string and returns the time unix value it represents. The layout defines the format by showing how the reference time.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseUnix(times.YYMDHMS, "2014-04-03 13:31:45") fmt.Printf("%v", v) }
Output: 1396503105
func ParseUnixGMT ¶
ParseUnixGMT parses a formatted string and returns the time unix value it represents. The layout is "Mon, 02 Jan 2006 15:04:05 GMT"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseUnixGMT("Thu, 03 Apr 2014 13:31:45 GMT") fmt.Printf("%v", v) }
Output: 1396503105
func ParseUnixGmt ¶
ParseUnixGmt parses a formatted string and returns the time unix value it represents. The layout is "Mon, 02 Jan 2006 15:04:05 GMT"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseUnixGmt("Thu, 03 Apr 2014 13:31:45 GMT") fmt.Printf("%v", v) }
Output: 1396503105
func ParseUnixNano ¶
ParseUnixNano parses a formatted string and returns the time unix nano value it represents. The layout defines the format by showing how the reference time.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseUnixNano(times.YYMDHMS, "2014-04-03 13:31:45") fmt.Printf("%v", v) }
Output: 1396503105000000000
func ParseUnixStr ¶
ParseUnixStr parses a formatted string and returns the time unix value it represents. The layout defines the format by showing how the reference time.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseUnixStr(times.YYMDHMS, "2014-04-03 13:31:45") fmt.Printf("%v", v) }
Output: 1396503105
func ParseUnixYYMD ¶
ParseUnixYYMD parses a formatted string and returns the time unix value it represents. The layout is "2006-01-02"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseUnixYYMD("2014-04-03") fmt.Printf("%v", v) }
Output: 1396454400
func ParseUnixYYMDHM ¶
ParseUnixYYMDHM parses a formatted string and returns the time unix value it represents. The layout is "2006-01-02 15:04"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseUnixYYMDHM("2014-04-03 13:31") fmt.Printf("%v", v) }
Output: 1396503060
func ParseUnixYYMDHMS ¶
ParseUnixYYMDHMS parses a formatted string and returns the time unix value it represents. The layout is "2006-01-02 15:04:05"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseUnixYYMDHMS("2014-04-03 13:31:45") fmt.Printf("%v", v) }
Output: 1396503105
func ParseUnixYymd ¶
ParseUnixYymd parses a formatted string and returns the time unix value it represents. The layout is "2006-01-02"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseUnixYymd("2014-04-03") fmt.Printf("%v", v) }
Output: 1396454400
func ParseUnixYymdhm ¶
ParseUnixYymdhm parses a formatted string and returns the time unix value it represents. The layout is "2006-01-02 15:04"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseUnixYymdhm("2014-04-03 13:31") fmt.Printf("%v", v) }
Output: 1396503060
func ParseUnixYymdhms ¶
ParseUnixYymdhms parses a formatted string and returns the time unix value it represents. The layout is "2006-01-02 15:04:05"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseUnixYymdhms("2014-04-03 13:31:45") fmt.Printf("%v", v) }
Output: 1396503105
func ParseYYMD ¶
ParseYYMD parses a formatted string and returns the time value it represents. The layout is "2006-01-02"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseYYMD("2014-04-03") fmt.Printf("%v", v) }
Output: 2014-04-03 00:00:00 +0800 CST
func ParseYYMDHM ¶
ParseYYMDHM parses a formatted string and returns the time value it represents. The layout is "2006-01-02 15:04"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseYYMDHM("2014-04-03 13:31") fmt.Printf("%v", v) }
Output: 2014-04-03 13:31:00 +0800 CST
func ParseYYMDHMS ¶
ParseYYMDHMS parses a formatted string and returns the time value it represents. The layout is "2006-01-02 15:04:05"
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/times" ) func main() { v, _ := times.ParseYYMDHMS("2014-04-03 13:31:45") fmt.Printf("%v", v) }
Output: 2014-04-03 13:31:45 +0800 CST
func Unix ¶
Unix returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC. It is valid to pass nsec outside the range [0, 999999999]. Not all sec values have a corresponding time value. One such value is 1<<63-1 (the largest int64 value).