github.com/wangyougui/gf/v2@v2.6.5/os/gtime/gtime_z_unit_format_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gtime_test 8 9 import ( 10 "testing" 11 12 "github.com/wangyougui/gf/v2/os/gtime" 13 "github.com/wangyougui/gf/v2/test/gtest" 14 ) 15 16 func Test_Format(t *testing.T) { 17 gtest.C(t, func(t *gtest.T) { 18 timeTemp, err := gtime.StrToTime("2006-01-11 15:04:05", "Y-m-d H:i:s") 19 timeTemp.ToZone("Asia/Shanghai") 20 if err != nil { 21 t.Error("test fail") 22 } 23 t.Assert(timeTemp.Format("\\T\\i\\m\\e中文Y-m-j G:i:s.u\\"), "Time中文2006-01-11 15:04:05.000") 24 25 t.Assert(timeTemp.Format("d D j l"), "11 Wed 11 Wednesday") 26 27 t.Assert(timeTemp.Format("F m M n"), "January 01 Jan 1") 28 29 t.Assert(timeTemp.Format("Y y"), "2006 06") 30 31 t.Assert(timeTemp.Format("a A g G h H i s u .u"), "pm PM 3 15 03 15 04 05 000 .000") 32 33 t.Assert(timeTemp.Format("O P T"), "+0800 +08:00 CST") 34 35 t.Assert(timeTemp.Format("r"), "Wed, 11 Jan 06 15:04 CST") 36 37 t.Assert(timeTemp.Format("c"), "2006-01-11T15:04:05+08:00") 38 39 //补零 40 timeTemp1, err := gtime.StrToTime("2006-01-02 03:04:05", "Y-m-d H:i:s") 41 if err != nil { 42 t.Error("test fail") 43 } 44 t.Assert(timeTemp1.Format("Y-m-d h:i:s"), "2006-01-02 03:04:05") 45 //不补零 46 timeTemp2, err := gtime.StrToTime("2006-01-02 03:04:05", "Y-m-d H:i:s") 47 if err != nil { 48 t.Error("test fail") 49 } 50 t.Assert(timeTemp2.Format("Y-n-j G:i:s"), "2006-1-2 3:04:05") 51 52 t.Assert(timeTemp2.Format("U"), "1136142245") 53 54 // 测试数字型的星期 55 times := []map[string]string{ 56 {"k": "2019-04-22", "f": "w", "r": "1"}, 57 {"k": "2019-04-23", "f": "w", "r": "2"}, 58 {"k": "2019-04-24", "f": "w", "r": "3"}, 59 {"k": "2019-04-25", "f": "w", "r": "4"}, 60 {"k": "2019-04-26", "f": "dw", "r": "265"}, 61 {"k": "2019-04-27", "f": "w", "r": "6"}, 62 {"k": "2019-03-10", "f": "w", "r": "0"}, 63 {"k": "2019-03-10", "f": "Y-m-d 星期:w", "r": "2019-03-10 星期:0"}, 64 {"k": "2019-04-25", "f": "N", "r": "4"}, 65 {"k": "2019-03-10", "f": "N", "r": "7"}, 66 {"k": "2019-03-01", "f": "S", "r": "st"}, 67 {"k": "2019-03-02", "f": "S", "r": "nd"}, 68 {"k": "2019-03-03", "f": "S", "r": "rd"}, 69 {"k": "2019-03-05", "f": "S", "r": "th"}, 70 71 {"k": "2019-01-01", "f": "第z天", "r": "第0天"}, 72 {"k": "2019-01-05", "f": "第z天", "r": "第4天"}, 73 {"k": "2020-05-05", "f": "第z天", "r": "第125天"}, 74 {"k": "2020-12-31", "f": "第z天", "r": "第365天"}, //润年 75 {"k": "2020-02-12", "f": "第z天", "r": "第42天"}, //润年 76 {"k": "2019-02-12", "f": "有t天", "r": "有28天"}, 77 {"k": "2020-02-12", "f": "20.2有t天", "r": "20.2有29天"}, 78 {"k": "2019-03-12", "f": "19.3有t天", "r": "19.3有31天"}, 79 {"k": "2019-11-12", "f": "19.11有t天", "r": "19.11有30天"}, 80 {"k": "2019-01-01", "f": "第W周", "r": "第1周"}, 81 {"k": "2017-01-01", "f": "第W周", "r": "第52周"}, //星期7 82 {"k": "2002-01-01", "f": "第W周为星期2", "r": "第1周为星期2"}, //星期2 83 {"k": "2016-01-01", "f": "第W周为星期5", "r": "第53周为星期5"}, //星期5 84 {"k": "2014-01-01", "f": "第W周为星期3", "r": "第1周为星期3"}, //星期3 85 {"k": "2015-01-01", "f": "第W周为星期4", "r": "第1周为星期4"}, //星期4 86 } 87 88 for _, v := range times { 89 t1, err1 := gtime.StrToTime(v["k"], "Y-m-d") 90 t.Assert(err1, nil) 91 t.Assert(t1.Format(v["f"]), v["r"]) 92 } 93 94 }) 95 gtest.C(t, func(t *gtest.T) { 96 var ti *gtime.Time = nil 97 t.Assert(ti.Format("Y-m-d h:i:s"), "") 98 t.Assert(ti.FormatNew("Y-m-d h:i:s"), nil) 99 t.Assert(ti.FormatTo("Y-m-d h:i:s"), nil) 100 t.Assert(ti.Layout("Y-m-d h:i:s"), "") 101 t.Assert(ti.LayoutNew("Y-m-d h:i:s"), nil) 102 t.Assert(ti.LayoutTo("Y-m-d h:i:s"), nil) 103 }) 104 } 105 106 func Test_Format_ZeroString(t *testing.T) { 107 gtest.C(t, func(t *gtest.T) { 108 timeTemp, err := gtime.StrToTime("0000-00-00 00:00:00") 109 t.AssertNE(err, nil) 110 t.Assert(timeTemp.String(), "") 111 }) 112 } 113 114 func Test_FormatTo(t *testing.T) { 115 gtest.C(t, func(t *gtest.T) { 116 timeTemp := gtime.Now() 117 t.Assert(timeTemp.FormatTo("Y-m-01 00:00:01"), timeTemp.Time.Format("2006-01")+"-01 00:00:01") 118 }) 119 } 120 121 func Test_Layout(t *testing.T) { 122 gtest.C(t, func(t *gtest.T) { 123 timeTemp := gtime.Now() 124 t.Assert(timeTemp.Layout("2006-01-02 15:04:05"), timeTemp.Time.Format("2006-01-02 15:04:05")) 125 }) 126 } 127 128 func Test_LayoutTo(t *testing.T) { 129 gtest.C(t, func(t *gtest.T) { 130 timeTemp := gtime.Now() 131 t.Assert(timeTemp.LayoutTo("2006-01-02 00:00:00"), timeTemp.Time.Format("2006-01-02 00:00:00")) 132 }) 133 }