<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] # 簡介 介紹Go 標準庫`time`常用導出函數,結構體及其方法。 ~~~ import "time" ~~~ time包提供顯示和計算時間用的函數。 # ## 時間處理類型 Go 中時間處理依賴的數據類型:`time.Time`,`time.Month`,`time.Weekday`,`time.Duration`,`time.Location` ## time.Time 時間點 time.Time 代表一個納秒精度的時間點. /usr/local/go/src/time/time.go 定義如下: ~~~ type Time struct { sec int64 // 從1年1月1日 00:00:00 UTC 至今過去的秒數 nsec int32 // 最近一秒到下一秒過去的納秒數 loc *Location // 時區 } ~~~ 時間變量標識一個具體的時間點,演示如下: ~~~ var t time.Time // 定義 time.Time 類型變量 t = time.Now() // 獲取當前時間 fmt.Printf("時間: %v, 時區: %v, 時間類型: %T\n", t, t.Location(), t) // 時間: 2017-02-22 09:06:05.816187261 +0800 CST, 時區: Local, 時間類型: time.Time // time.UTC() time 返回UTC 時區的時間 fmt.Printf("時間: %v, 時區: %v, 時間類型: %T\n", t.UTC(), t.UTC().Location(), t) // 時間: 2017-02-22 01:07:15.179280004 +0000 UTC, 時區: UTC, 時間類型: time.Time ~~~ 代碼中應使用time.Time 類型值來保存和傳遞時間,而不能用指針。**表示時間的變量和字段,應為time.Time類型,而不是*time.Time類型**。 每一個時間點都具有時區信息,當計算時間的表示格式時,如Format、Hour和Year等方法,都會考慮該信息。Local、UTC和In方法返回一個指定時區(但指向同一時間點)的Time。修改時區信息只是會改變其表示,不會修改被表示的時間點 ## time.Month 月份 `time.Month`代表一年中的某個月 `/usr/local/go/src/time/time.go`定義如下: ~~~ type Month int ~~~ ~~~ const ( January Month = 1 + iota February March April May June July August September October November December ) ~~~ ## time.Weekday 星期 `time.Weekday`代表一周的周幾。 `/usr/local/go/src/time/time.go`定義如下: ~~~ type Weekday int ~~~ 和星期相關的常量: ~~~ const ( Sunday Weekday = iota Monday Tuesday Wednesday Thursday Friday Saturday ) ~~~ ## time.Duration 時間段 `time.Duration`類型代表兩個時間點之間經過的納秒數,可表示的最長時間段約為290年。 `/usr/local/go/src/time/time.go`定義如下: ~~~ type Duration int64 ~~~ 涉及常量如下: ~~~ const ( Nanosecond Duration = 1 Microsecond = 1000 * Nanosecond Millisecond = 1000 * Microsecond Second = 1000 * Millisecond Minute = 60 * Second Hour = 60 * Minute ) ~~~ ## time.Location 時區 Location代表一個地點,以及該地點所在的時區信息。北京時間可以使用`Asia/Shanghai` `/usr/local/go/src/time/zoneinfo.go`中定義: ~~~ type Location struct { name string zone []zone tx []zoneTrans cacheStart int64 cacheEnd int64 cacheZone *zone } ~~~ 預定義時區變量: ~~~ var UTC *Location = &utcLoc var Local *Location = &localLoc ~~~ # time.Time 方法 介紹`time.Time`接受者的方法:獲取時間點,獲取時間相關信息,時間比較,計算和序列化操作 ## 獲取一個時間的方法 * func Now() Time {} // 當前本地時間 * func Unix(sec int64, nsec int64) Time {} // 根據時間戳返回本地時間 * func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time {} // 返回指定時間 ~~~ // 當前本地時間 t = time.Now() fmt.Println("'time.Now': ", t) // 根據時間戳返回本地時間 t_by_unix := time.Unix(1487780010, 0) fmt.Println("'time.Unix': ", t_by_unix) // 返回指定時間 t_by_date := time.Date(2017, time.Month(2), 23, 1, 30, 30, 0, l) fmt.Println("'time.Date': ", t_by_date) ~~~ ## 時間顯示 * func (t Time) UTC() Time {} // 獲取指定時間在UTC 時區的時間表示 * func (t Time) Local() Time {} // 以本地時區表示 * func (t Time) In(loc *Location) Time {} // 時間在指定時區的表示 * func (t Time) Format(layout string) string {} // 按指定格式顯示時間 ~~~ // 獲取指定時間在UTC 時區的時間表示 t_by_utc := t.UTC() fmt.Println("'t.UTC': ", t_by_utc) // 獲取本地時間表示 t_by_local := t.Local() fmt.Println("'t.Local': ", t_by_local) // 時間在指定時區的表示 t_in := t.In(time.UTC) fmt.Println("'t.In': ", t_in) // Format fmt.Println("t.Format", t.Format(time.RFC3339)) ~~~ ## 獲取日期信息 * func (t Time) Date() (year int, month Month, day int) {} // 返回時間的日期信息 * func (t Time) Year() int {} // 返回年 * func (t Time) Month() Month {} // 月 * func (t Time) Day() int {} // 日 * func (t Time) Weekday() Weekday {} // 星期 * func (t Time) ISOWeek() (year, week int) {} // 返回年,星期范圍編號 * func (t Time) Clock() (hour, min, sec int) {} // 返回時間的時分秒 * func (t Time) Hour() int {} // 返回小時 * func (t Time) Minute() int {} // 分鐘 * func (t Time) Second() int {} // 秒 * func (t Time) Nanosecond() int {} // 納秒 * func (t Time) YearDay() int {} // 一年中對應的天 * func (t Time) Location() *Location {} // 時間的時區 * func (t Time) Zone() (name string, offset int) {} // 時間所在時區的規范名和想對UTC 時間偏移量 * func (t Time) Unix() int64 {} // 時間轉為時間戳 * func (t Time) UnixNano() int64 {} // 時間轉為時間戳(納秒) ~~~ // 返回時間的日期信息 year, month, day := t.Date() fmt.Println("'t.Date': ", year, month, day) // 星期 week := t.Weekday() fmt.Println("'t.Weekday': ", week) // 返回年,星期范圍編號 year, week_int := t.ISOWeek() fmt.Println("'t.ISOWeek': ", year, week_int) // 返回時間的時分秒 hour, min, sec := t.Clock() fmt.Println("'t.Clock': ", hour, min, sec) ~~~ ## 時間比較與計算 * func (t Time) IsZero() bool {} // 是否是零時時間 * func (t Time) After(u Time) bool {} // 時間在u 之前 * func (t Time) Before(u Time) bool {} // 時間在u 之后 * func (t Time) Equal(u Time) bool {} // 時間與u 相同 * func (t Time) Add(d Duration) Time {} // 返回t +d 的時間點 * func (t Time) Sub(u Time) Duration {} // 返回 t-u * func (t Time) AddDate(years int, months int, days int) Time {} 返回增加了給出的年份、月份和天數的時間點Time ~~~ // 返回增加了給出的年份、月份和天數的時間點Time t_new := t.AddDate(0, 1, 1) fmt.Println("'t.AddDate': ", t_new) // 時間在u 之前 is_after := t.After(t_new) fmt.Println("'t.After': ", is_after) ~~~ ## 時間序列化 * func (t Time) MarshalBinary() ([]byte, error) {} // 時間序列化 * func (t Time) UnmarshalBinary(data []byte) error {} // 反序列化 * func (t Time) MarshalJSON() ([]byte, error) {} // 時間序列化 * func (t Time) MarshalText() ([]byte, error) {} // 時間序列化 * func (t Time) GobEncode() ([]byte, error) {} // 時間序列化 * func (t Time) GobDecode() ([]byte, error) {} // 時間序列化 ~~~ // 時間序列化 t_byte, err := t.MarshalJSON() fmt.Println("'t.MarshalJSON': ", t_byte, err) // 時間數據反序列化 var t_un time.Time err = t_un.UnmarshalJSON(t_byte) fmt.Println("'t_un.UnmarshalJSON': ", t_un, err) ~~~ # time.Duration 方法 介紹 time.Duration 時間段接受者的方法:輸出和改變時間段單位。 * func (d Duration) String() string // 格式化輸出 Duration * func (d Duration) Nanoseconds() int64 // 將時間段表示為納秒 * func (d Duration) Seconds() float64 // 將時間段表示為秒 * func (d Duration) Minutes() float64 // 將時間段表示為分鐘 * func (d Duration) Hours() float64 // 將時間段表示為小時 ~~~ // time.Duration 時間段 fmt.Println("time.Duration 時間段") d = time.Duration(10000000000000) fmt.Printf("'String: %v', 'Nanoseconds: %v', 'Seconds: %v', 'Minutes: %v', 'Hours: %v'\n", d.String(), d.Nanoseconds(), d.Seconds(), d.Minutes(), d.Hours()) // 'String: 2h46m40s', 'Nanoseconds: 10000000000000', 'Seconds: 10000', 'Minutes: 166.66666666666666', 'Hours: 2.7777777777777777' ~~~ # time.Location 方法 time.Location 時區的導出函數 * func (l *Location) String() string // 輸出時區名 * func FixedZone(name string, offset int) *Location // FixedZone 使用給定的地點名name和時間偏移量offset(單位秒)創建并返回一個Location * func LoadLocation(name string) (*Location, error) // LoadLocation 使用給定的名字創建Location ~~~ var local *time.Location local, ok := time.LoadLocation("Asia/Shanghai") fmt.Printf("%v, %T, %v\n", local, local, ok) ~~~ # 其他方法 * `func Sleep(d Duration)`// Sleep阻塞當前go程至少d代表的時間段。d<=0時,Sleep會立刻返回 ~~~ d_second := time.Second time.Sleep(d_second) ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看