<div class = "post">
<h1 class = "postTitle">
<a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/zhepama/archive/2013/04/12/3017230.html">golang 的time包之time</a>
</h1>
<div class="clear"></div>
<div class="postBody">
<div id="cnblogs_post_body" class="blogpost-body"><h2>先看看有哪些类型</h2>
<h3>Time</h3>
<p>时间类型,包含了秒和纳秒以及Location</p>
<h3>Month</h3>
<p>type Month int 月份.定义了十二个月的常量</p>
<h3>Weekday</h3>
<p>type Weekday int 周,定义了一周的七天</p>
<h3>Duration</h3>
<p>type Duration int64 持续时间.定义了以下持续时间类型.多用于时间的加减 需要传入Duration做为参数的时候.可以直接传入time.Second</p>
<pre>const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
</pre>
<h2>Location</h2>
<p>在time包里有两个时区变量:</p>
<ul>
<li>time.UTC utc时间</li>
<li>time.Local 本地时间</li>
</ul>
<p> FixedZone(name string, offset int) *Location<br />
设置时区名,以及与UTC0的时间偏差.返回Location
</p>
<h2>时间格式化</h2>
<dl><dt>Format(layout string) string</dt><dd>传入目标模板(Mon Jan 02 15:04:05 -0700 2006).时间以这个为准
<pre> p(t.Format("3:04PM"))
p(t.Format("Mon Jan _2 15:04:05 2006"))
p(t.Format("2006-01-02T15:04:05.999999-07:00"))
p(t.Format("2006-01-02T15:04:05Z07:00"))
fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
</pre>
</dd><dt>Parse(layout, value string) (Time, error)</dt><dd>将字符窜转换为Time类型.
<pre> p := fmt.Println
withNanos := "2006-01-02 15:04:05"
t, _ := time.Parse(withNanos, "2013-10-05 18:30:50")
p(t.Year())
</pre>
</dd><dt>ParseDuration(s string) (Duration, error)</dt><dd>将字duration符窜("ns", "us" (or "碌s"), "ms", "s", "m", "h".)转换为Duration类型.就是纳秒
<pre> p := fmt.Println
t, _ := time.ParseDuration("1h")
p(t.Seconds())
</pre>
</dd></dl>
<h2>Time相关</h2>
<h3>time常用函数</h3>
<dl><dt>Now() Time</dt><dd>获取当前时间,返回Time类型</dd><dt>Unix(sec int64, nsec int64) Time</dt><dd>根据秒数和纳秒,返回Time类型</dd><dt>Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time</dt><dd>设置年月日返回,Time类型</dd><dt>Since(t Time) Duration</dt><dd>返回与当前时间的时间差</dd></dl>
<h3>time常用方法</h3>
<dl><dt>After(u Time) bool</dt><dd>时间类型比较,是否在Time之后</dd><dt>Before(u Time) bool</dt><dd>时间类型比较,是否在Time之前</dd><dt>Equal(u Time) bool</dt><dd>比较两个时间是否相等</dd><dt>IsZero() bool</dt><dd>判断时间是否为零值,如果sec和nsec两个属性都是0的话,则该时间类型为0</dd><dt>Date() (year int, month Month, day int)</dt><dd>返回年月日,三个参数</dd><dt>Year() int</dt><dd>返回年份</dd><dt>Month() Month</dt><dd>返回月份.是Month类型</dd><dt>Day() int</dt><dd>返回多少号</dd><dt>Weekday() Weekday</dt><dd>返回星期几,是Weekday类型</dd><dt>ISOWeek() (year, week int)</dt><dd>返回年份,和该填是在这年的第几周.</dd><dt>Clock() (hour, min, sec int)</dt><dd>返回小时,分钟,秒</dd><dt>Hour() int</dt><dd>返回小时</dd><dt>Minute() int</dt><dd>返回分钟</dd><dt>Second() int</dt><dd>返回秒数</dd><dt>Nanosecond() int</dt><dd>返回纳秒</dd><dt>Add(d Duration) Time</dt><dd>为一个时间,添加的时间类型为Duration.更精确到纳秒.比起AddDate</dd><dt>Sub(u Time) Duration</dt><dd>计算两个时间的差.返回类型Duration</dd><dt>AddDate(years int, months int, days int) Time</dt><dd>添加时间.以年月日为参数</dd><dt>UTC() Time</dt><dd>设置location为UTC,然后返回时间.就是utc为0.比中国晚了八个小时.</dd><dt>Local() Time</dt><dd>设置location为本地时间.就是电脑时间.</dd><dt>In(loc *Location) Time</dt><dd>设置location为指定location</dd><dt>Location() *Location</dt><dd>获取时间的Location,如果是nic,返回UTC,如果为空,则代表本地</dd><dt>Zone() (name string, offset int)</dt><dd>返回时区,以及与utc的时间偏差</dd><dt>Unix() int64</dt><dd>返回时间戳,自从1970年1月1号到现在</dd><dt>UnixNano() int64</dt><dd>返回时间戳.包含纳秒
<pre> func main() {
now := time.Now()
secs := now.Unix()
nanos := now.UnixNano()
fmt.Println(now)
millis := nanos / 1000000
fmt.Println(secs)
fmt.Println(millis)
fmt.Println(nanos)
fmt.Println(time.Unix(secs, 0))
fmt.Println(time.Unix(0, nanos))
}
</pre>
</dd><dt>GobEncode() ([]byte, error)</dt><dd>编码为gob</dd><dt>GobDecode(buf []byte) error</dt><dd>从gob解码</dd><dt>MarshalJSON() ([]byte, error)</dt><dd>编列为json</dd><dt>UnmarshalJSON(data []byte) (err error)</dt><dd>解码为json</dd></dl>
<div>
<pre> func main() {
p := fmt.Println
now := time.Now()
p(now)
d := time.Duration(7200 * 1000 * 1000 * 1000)
p(d)
then := time.Date(
2013, 1, 7, 20, 34, 58, 651387237, time.UTC)
p(then)
p(then.Year())
p(then.Month())
p(then.Day())
p(then.Hour())
p(then.Minute())
p(then.Second())
p(then.Nanosecond())
p(then.Location())
p(then.Weekday())
p(then.Before(now))
p(then.After(now))
p(then.Equal(now))
p(then.Date())
p(then.ISOWeek())
p("----------")
p(now.UTC())
p(now.Local())
p(now.Location())
p(now.Zone())
p(now.Unix())
p(time.Unix(now.Unix(), 0))
p(now.UnixNano())
p(time.Unix(0, now.UnixNano()))
p(now.GobEncode())
p(now.MarshalJSON())
p(time.Since(now))
p("----------")
diff := now.Sub(then)
p(diff)
p(diff.Hours())
p(diff.Minutes())
p(diff.Seconds())
p(diff.Nanoseconds())
p(then.Add(diff))
p(then.Add(-diff))
p(d)
p(d.Hours())
p(d.Minutes())
p(d.Seconds())
p(d.Nanoseconds())
p(then.Add(d))
}
</pre>
</div></div><divid="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory"></div>
<div id="EntryTag"></div>
<div id="blog_post_info">
</div>
<div class="clear"></div>
<div id="post_next_prev"></div>
</div>
golang标准库 time包
最新推荐文章于 2023-08-17 23:00:21 发布