Constants
const (
ANSIC = Mon Jan _2 15:04:05 2006
UnixDate = Mon Jan _2 15:04:05 MST 2006
RubyDate = Mon Jan 02 15:04:05 -0700 2006
RFC822 = 02 Jan 06 15:04 MST
RFC822Z = 02 Jan 06 15:04 -0700 // RFC822 with numeric zone
RFC850 = Monday, 02-Jan-06 15:04:05 MST
RFC1123 = Mon, 02 Jan 2006 15:04:05 MST
RFC1123Z = Mon, 02 Jan 2006 15:04:05 -0700 // RFC1123 with numeric zone
RFC3339 = 2006-01-02T15:04:05Z07:00
RFC3339Nano = 2006-01-02T15:04:05.999999999Z07:00
Kitchen = 3:04PM
// Handy time stamps.
Stamp = Jan _2 15:04:05
StampMilli = Jan _2 15:04:05.000
StampMicro = Jan _2 15:04:05.000000
StampNano = Jan _2 15:04:05.000000000
)
这些是预定义的布局,用于Time.Format和time.Parse中。在这些布局中使用的参考时间是特定的时间戳。
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
常见的持续时间。没有关于日或更大单位的定义,以避免在夏令时区的转换中出现混淆。
Functions
func Tick
func Tick(d Duration) <-chan Time
Tick是NewTicker的一个方便的包装器,只提供对Ticking通道的访问。虽然Tick对于那些不需要关闭Ticker的客户端来说很有用,但要注意的是,如果没有关闭的方法,底层的Ticker就不能被垃圾收集器恢复;它 "泄漏 "了。与NewTicker不同,如果d<=0,Tick将返回nil。
types
type Duration
Duration表示两个瞬间之间经过的时间,作为一个int64纳秒计数。代表将最大的可代表的时间限制在大约290年。 A Duration represents the elapsed time between two instants as an int64 nanosecond count. The representation limits the largest representable duration to approximately 290 years.
type Time
时间表示具有纳秒精度的时间 A Time represents an instant in time with nanosecond precision.
常用示例
获取当前时间戳
fmt.Println(time.Now().Unix())
格式化当前时间
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
时间戳转str格式化时间
str_time := time.Unix(1389058332, 0).Format("2006-01-02 15:04:05")
the_time, err := time.Parse(2006-01-02 15:04:05, 2014-01-08 09:04:41)
if err == nil {
unix_time := the_time.Unix()
fmt.Println(unix_time)
}
# 1389171881
func (Time) Date
func (t Time) Date() (year int, month Month, day int)
Date returns the year, month, and day in which t occurs.
time.Sleep
func Sleep(d Duration)
Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately.
Sleep使当前的goroutine暂停,至少持续时间为d,负的或零的持续时间使Sleep立即返回。
time.Now().Add()
func (t Time) Add(d Duration) Time
Add returns the time t+d.
time.Now().Before()
func (t Time) Before(u Time) bool
Before reports whether the time instant t is before u.
Before报告时间瞬间t是否在u之前。
func (Time) Format
func (t Time) Format(layout string) string
Format returns a textual representation of the time value formatted according to the layout defined by the argument. See the documentation for the constant called Layout to see how to represent the layout format.
Format返回一个根据参数定义的布局格式化的时间值的文本表示。参见名为Layout的常量的文档,了解如何表示布局格式。
Types
type Duration
type Duration int64
A Duration represents the elapsed time between two instants as an int64 nanosecond count. The representation limits the largest representable duration to approximately 290 years.
一个持续时间以int64纳秒计数的形式表示两个瞬间之间的时间流逝。该表示法将最大的可表示的持续时间限制在大约290年。
func ParseDuration
func ParseDuration(s string) (Duration, error)
ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
ParseDuration解析一个持续时间字符串。持续时间字符串是一个可能有符号的十进制数字序列,每个数字都有可选的分数和单位后缀,如 "300ms"、"-1.5h "或 "2h45m"。有效的时间单位是 "ns"、"us"(或 "µs")、"ms"、"s"、"m"、"h"。
Q&A
Go 的时间格式化为什么是 2006-01-02 15:04:05?
常规方式:2006-01-02 15:04:05,而 21-8-4 9:30:00 这种格式,只需要对应的改变值即可:06-1-2 3:04:05。而且,我查了下,PHP 没法表示没有前导零的分钟数和秒数,而 Go 很容易实现。
按照下面来记忆 [ 2006-01-02 15:04:05 ]
1: month (January, Jan, 01, etc)
2: day
3: hour (15 is 3pm on a 24 hour clock)
4: minute
5: second
6: year (2006)
7: timezone (GMT-7 is MST)