gitee.com/quant1x/gox@v1.21.2/api/datetime_test.go (about) 1 package api 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 ) 8 9 func TestDifferDays(t *testing.T) { 10 layout := "2006-01-02 15:04:05" 11 12 // just one second 13 t1, _ := time.Parse(layout, "2007-01-02 23:59:59") 14 t2, _ := time.Parse(layout, "2007-01-03 00:00:00") 15 if DifferDays(t2, t1) != 1 { 16 panic("one second but different day should return 1") 17 } 18 19 // just one day 20 t1, _ = time.Parse(layout, "2007-01-02 23:59:59") 21 t2, _ = time.Parse(layout, "2007-01-03 00:00:01") 22 if DifferDays(t2, t1) != 1 { 23 panic("just one day should return 1") 24 } 25 26 // just one day 27 t1, _ = time.Parse(layout, "2007-01-02 23:59:59") 28 t2, _ = time.Parse(layout, "2007-01-03 23:59:59") 29 if DifferDays(t2, t1) != 1 { 30 panic("just one day should return 1") 31 } 32 33 t1, _ = time.Parse(layout, "2017-09-01 10:00:00") 34 t2, _ = time.Parse(layout, "2017-09-02 11:00:00") 35 if DifferDays(t2, t1) != 1 { 36 panic("just one day should return 1") 37 } 38 39 // more than one day 40 t1, _ = time.Parse(layout, "2007-01-02 23:59:59") 41 t2, _ = time.Parse(layout, "2007-01-04 00:00:00") 42 if DifferDays(t2, t1) != 2 { 43 panic("just one day should return 2") 44 } 45 // just 3 day 46 t1, _ = time.Parse(layout, "2007-01-02 00:00:00") 47 t2, _ = time.Parse(layout, "2007-01-05 00:00:00") 48 if DifferDays(t2, t1) != 3 { 49 panic("just 3 day should return 3") 50 } 51 52 // different month 53 t1, _ = time.Parse(layout, "2007-01-02 00:00:00") 54 t2, _ = time.Parse(layout, "2007-02-02 00:00:00") 55 if DifferDays(t2, t1) != 31 { 56 fmt.Println(DifferDays(t2, t1)) 57 panic("just one month:31 days should return 31") 58 } 59 60 // 29 days in 2mth 61 t1, _ = time.Parse(layout, "2000-02-01 00:00:00") 62 t2, _ = time.Parse(layout, "2000-03-01 00:00:00") 63 if DifferDays(t2, t1) != 29 { 64 fmt.Println(DifferDays(t2, t1)) 65 panic("just one month:29 days should return 29") 66 } 67 t1 = time.Date(2018, 1, 10, 0, 0, 1, 100, time.Local) 68 t2 = time.Date(2018, 1, 9, 23, 59, 22, 100, time.Local) 69 if DifferDays(t1, t2) != 1 { 70 panic(fmt.Sprintf("just one day: should return 1 but got %v", DifferDays(t1, t2))) 71 } 72 73 t1 = time.Date(2018, 1, 10, 0, 0, 1, 100, time.UTC) 74 t2 = time.Date(2018, 1, 9, 23, 59, 22, 100, time.UTC) 75 if DifferDays(t1, t2) != 1 { 76 panic(fmt.Sprintf("just one day: should return 1 but got %v", DifferDays(t1, t2))) 77 } 78 } 79 80 func TestParseTime(t *testing.T) { 81 var ( 82 tm time.Time 83 err error 84 ) 85 86 tm, err = ParseTime("900301") 87 fmt.Printf("time: %+v, error: %v\n", tm, err) 88 tm, err = ParseTime("000301") 89 fmt.Printf("time: %+v, error: %v\n", tm, err) 90 91 tm, err = ParseTime("20000301") 92 fmt.Printf("time: %+v, error: %v\n", tm, err) 93 tm, err = ParseTime("2000-03-02") 94 fmt.Printf("time: %+v, error: %v\n", tm, err) 95 tm, err = ParseTime("20000303010203") 96 fmt.Printf("time: %+v, error: %v\n", tm, err) 97 tm, err = ParseTime("2000-03-04 05:06:07") 98 fmt.Printf("time: %+v, error: %v\n", tm, err) 99 tm, err = ParseTime("2000-03-05 06:07:08.123") 100 fmt.Printf("time: %+v, error: %v\n", tm, err) 101 } 102 103 func TestGetQuarterDay(t *testing.T) { 104 s, e := GetQuarterDay(4) 105 fmt.Println(s, e) 106 fmt.Println("------------------------------------------------------------") 107 date := "2023-04-01" 108 fmt.Print(date, " => ") 109 s, e = GetQuarterDayByDate(date) 110 fmt.Println(s, e) 111 date = "2023-03-31" 112 fmt.Print(date, " => ") 113 s, e = GetQuarterDayByDate(date) 114 fmt.Println(s, e) 115 date = "2023-06-30" 116 fmt.Print(date, " => ") 117 s, e = GetQuarterDayByDate(date) 118 fmt.Println(s, e) 119 } 120 121 func TestGetWeekDay(t *testing.T) { 122 s, e := GetWeekDay() 123 fmt.Println(s, e) 124 125 s, e = GetWeekDay("20230630") 126 fmt.Println(s, e) 127 }