times

package
v0.0.0-...-82e7740 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2019 License: MIT Imports: 3 Imported by: 10

README

util-times GoDoc

time library for Go

Installation

go get gopkg.in/goyy/goyy.v0/util/times

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

Examples

Constants

View Source
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

func Add(t time.Time, d Duration) time.Time

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

func AddGMT(t string, d Duration) (string, error)

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

func AddStr(layout, t string, d Duration) (string, error)

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

func AddUnix(t int64, d Duration) int64

AddUnix returns the time t+d.

Example
fmt.Println(times.AddUnix(i, times.Day))
Output:

1396589505

func AddYYMD

func AddYYMD(t string, d Duration) (string, error)

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

func AddYYMDHM(t string, d Duration) (string, error)

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

func AddYYMDHMS(t string, d Duration) (string, error)

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

func Format(layout string, t time.Time) string

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

func FormatGMT(t time.Time) string

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

func FormatUnix(layout string, i int64) string

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

func FormatUnixGMT(i int64) string

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

func FormatUnixYYMD(i int64) string

FormatUnixYYMD time formatted as 2006-01-02.

Example
fmt.Println(times.FormatUnixYYMD(i))
Output:

2014-04-03

func FormatUnixYYMDHM

func FormatUnixYYMDHM(i int64) string

FormatUnixYYMDHM time formatted as 2006-01-02 15:04.

Example
fmt.Println(times.FormatUnixYYMDHM(i))
Output:

2014-04-03 13:31

func FormatUnixYYMDHMS

func FormatUnixYYMDHMS(i int64) string

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

func FormatYYMD(t time.Time) string

FormatYYMD time formatted as 2006-01-02.

Example
fmt.Println(times.FormatYYMD(in))
Output:

2014-04-03

func FormatYYMDHM

func FormatYYMDHM(t time.Time) string

FormatYYMDHM time formatted as 2006-01-02 15:04.

Example
fmt.Println(times.FormatYYMDHM(in))
Output:

2014-04-03 13:31

func FormatYYMDHMS

func FormatYYMDHMS(t time.Time) string

FormatYYMDHMS time formatted as 2006-01-02 15:04:05.

Example
fmt.Println(times.FormatYYMDHMS(in))
Output:

2014-04-03 13:31:45

func Now

func Now() time.Time

Now returns the current local time of unix.

func NowUnix

func NowUnix() int64

NowUnix returns the current local time of unix.

func NowUnixStr

func NowUnixStr() string

NowUnixStr returns the current local time of unix.

func Parse

func Parse(layout, value string) (out time.Time, err error)

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

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

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

func ParseUnix(layout, value string) (int64, error)

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

func ParseUnixGMT(value string) (int64, error)

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

func ParseUnixGmt(value string) (string, error)

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

func ParseUnixNano(layout, value string) (int64, error)

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

func ParseUnixStr(layout, value string) (string, error)

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

func ParseUnixYYMD(value string) (int64, error)

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

func ParseUnixYYMDHM(value string) (int64, error)

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

func ParseUnixYYMDHMS(value string) (int64, error)

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

func ParseUnixYymd(value string) (string, error)

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

func ParseUnixYymdhm(value string) (string, error)

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

func ParseUnixYymdhms(value string) (string, error)

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

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

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

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

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

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

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

func Unix(sec int64, nsec int64) time.Time

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).

Types

type Duration

type Duration time.Duration

Duration returns the time.Duration

Jump to

Keyboard shortcuts

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