package time_test import ( "fmt" "math" "testing" "time" ) func TestRound(tx *testing.T) { t, _ := time.Parse("2006-01-02 15:04:05.000000000", "2006-01-02 15:04:05.123456789") fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(1 * time.Second) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(1 * time.Minute) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(1 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(24 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) //// t, _ = time.Parse("2006-01-02 15:04:05.000000000", "2006-01-02 15:30:05.123456789") fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(1 * time.Second) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(1 * time.Minute) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(1 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(24 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) //// t, _ = time.Parse("2006-01-02 15:04:05.000000000", time.Now().Format("2006-01-02 15:04:05.000000000")) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(1 * time.Second) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(1 * time.Minute) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(1 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(24 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) //// t = time.Now() fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(1 * time.Second) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(1 * time.Minute) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(1 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Round(24 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) } func TestTruncate(tx *testing.T) { t, _ := time.Parse("2006-01-02 15:04:05.000000000", "2006-01-02 15:04:05.123456789") fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(1 * time.Second) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(1 * time.Minute) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(1 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(24 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) //// t, _ = time.Parse("2006-01-02 15:04:05.000000000", "2006-01-02 15:30:05.123456789") fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(1 * time.Second) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(1 * time.Minute) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(1 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(24 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) //// t, _ = time.Parse("2006-01-02 15:04:05.000000000", time.Now().Format("2006-01-02 15:04:05.000000000")) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(1 * time.Second) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(1 * time.Minute) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(1 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(24 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) //// t = time.Now() fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(1 * time.Second) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(1 * time.Minute) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(1 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) t = t.Truncate(24 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) //// t = time.Now().Truncate(366 * 24 * time.Hour) fmt.Println(t.Format("2006-01-02 15:04:05.000000000")) } func TestRange(t *testing.T) { n := int64(math.MinInt64) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) n = int64(math.MaxInt64) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) n = int64(1e16) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) n = int64(1e17) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) n = int64(1e18) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) n = int64(2e18) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) n = int64(3e18) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) n = int64(4e18) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) n = int64(5e18) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) n = int64(6e18) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) n = int64(7e18) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) n = int64(8e18) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) n = int64(9e18) fmt.Println(time.Unix(0, n).Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(n))) tm, _ := time.Parse("2006-01-02 15:04:05", "2001-09-09 09:46:40") fmt.Println(tm.Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%e", float64(tm.UnixNano()))) tm = time.Time{} fmt.Println(tm.Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%d", tm.Unix()), fmt.Sprintf("%d", tm.UnixNano())) tm, _ = time.Parse("2006-01-02 15:04:05", "0220-09-09 09:09:09") fmt.Println(tm.Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%d", tm.Unix()), fmt.Sprintf("%d", tm.UnixNano())) n = int64(-6795364578871345152) tm = time.Unix(0, n) fmt.Println(tm.Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%d", tm.Unix()), fmt.Sprintf("%d", tm.UnixNano())) } func TestRange2(t *testing.T) { tm := time.Unix(-9223372028715350743, -1) fmt.Println(tm.Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%d", tm.Unix()), fmt.Sprintf("%d", tm.UnixNano())) tm = time.Unix(math.MinInt64, 0) fmt.Println(tm.Format("2006-01-02 15:04:05.000000000"), " ", fmt.Sprintf("%d", tm.Unix()), fmt.Sprintf("%d", tm.UnixNano())) }