GO語言"time"包中"Time.Truncate"類型的用法及代碼示例。
用法:
func(t Time) Truncate(d Duration) Time
截斷返回將 t 向下舍入為 d 的倍數(從零開始)的結果。如果 d <= 0,則 Truncate 返回 t 去除任何單調時鍾讀數,否則保持不變。
Truncate 對時間進行操作,作為從零時間開始的絕對持續時間;它不適用於當時的演示文稿形式。因此,Truncate(Hour) 可能會返回一個非零分鍾的時間,具體取決於時間的位置。
例子:
package main
import (
"fmt"
"time"
)
func main() {
t, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 12:15:30.918273645")
trunc := []time.Duration{
time.Nanosecond,
time.Microsecond,
time.Millisecond,
time.Second,
2 * time.Second,
time.Minute,
10 * time.Minute,
}
for _, d := range trunc {
fmt.Printf("t.Truncate(%5s) = %s\n", d, t.Truncate(d).Format("15:04:05.999999999"))
}
// To round to the last midnight in the local timezone, create a new Date.
midnight := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
_ = midnight
}
輸出:
t.Truncate( 1ns) = 12:15:30.918273645 t.Truncate( 1µs) = 12:15:30.918273 t.Truncate( 1ms) = 12:15:30.918 t.Truncate( 1s) = 12:15:30 t.Truncate( 2s) = 12:15:30 t.Truncate( 1m0s) = 12:15:00 t.Truncate(10m0s) = 12:10:00
相關用法
- GO Time.Sub用法及代碼示例
- GO Time.Equal用法及代碼示例
- GO Time.After用法及代碼示例
- GO Time.Format用法及代碼示例
- GO Time.AppendFormat用法及代碼示例
- GO Time.AddDate用法及代碼示例
- GO Time.Date用法及代碼示例
- GO Time.Round用法及代碼示例
- GO Time.Add用法及代碼示例
- GO Time.GoString用法及代碼示例
- GO Time.Day用法及代碼示例
- GO Time.String用法及代碼示例
- GO Time.Unix用法及代碼示例
- GO Time.Before用法及代碼示例
- GO Title用法及代碼示例
- GO Tick用法及代碼示例
- GO TrailingZeros8用法及代碼示例
- GO Tx.ExecContext用法及代碼示例
- GO Template用法及代碼示例
- GO ToTitle用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Time.Truncate。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。