github.com/zhongdalu/gf@v1.0.0/g/os/gtime/gtime_z_unit_format_test.go (about)

     1  package gtime_test
     2  
     3  import (
     4  	"github.com/zhongdalu/gf/g/os/gtime"
     5  	"github.com/zhongdalu/gf/g/test/gtest"
     6  	"testing"
     7  )
     8  
     9  func Test_Format(t *testing.T) {
    10  	gtest.Case(t, func() {
    11  		timeTemp, err := gtime.StrToTime("2006-01-11 15:04:05", "Y-m-d H:i:s")
    12  		timeTemp.ToZone("Asia/Shanghai")
    13  		if err != nil {
    14  			t.Error("test fail")
    15  		}
    16  		gtest.Assert(timeTemp.Format("\\T\\i\\m\\e中文Y-m-j G:i:s.u\\"), "Time中文2006-01-11 15:04:05.000")
    17  
    18  		gtest.Assert(timeTemp.Format("d D j l"), "11 Wed 11 Wednesday")
    19  
    20  		gtest.Assert(timeTemp.Format("F m M n"), "January 01 Jan 1")
    21  
    22  		gtest.Assert(timeTemp.Format("Y y"), "2006 06")
    23  
    24  		gtest.Assert(timeTemp.Format("a A g G h H i s u .u"), "pm PM 3 15 03 15 04 05 000 .000")
    25  
    26  		gtest.Assert(timeTemp.Format("O P T"), "+0800 +08:00 CST")
    27  
    28  		gtest.Assert(timeTemp.Format("r"), "Wed, 11 Jan 06 15:04 CST")
    29  
    30  		gtest.Assert(timeTemp.Format("c"), "2006-01-11T15:04:05+08:00")
    31  
    32  		//补零
    33  		timeTemp1, err := gtime.StrToTime("2006-01-02 03:04:05", "Y-m-d H:i:s")
    34  		if err != nil {
    35  			t.Error("test fail")
    36  		}
    37  		gtest.Assert(timeTemp1.Format("Y-m-d h:i:s"), "2006-01-02 03:04:05")
    38  		//不补零
    39  		timeTemp2, err := gtime.StrToTime("2006-01-02 03:04:05", "Y-m-d H:i:s")
    40  		if err != nil {
    41  			t.Error("test fail")
    42  		}
    43  		gtest.Assert(timeTemp2.Format("Y-n-j G:i:s"), "2006-1-2 3:04:05")
    44  
    45  		gtest.Assert(timeTemp2.Format("U"), "1136142245")
    46  
    47  		// 测试数字型的星期
    48  		times := []map[string]string{
    49  			{"k": "2019-04-22", "f": "w", "r": "1"},
    50  			{"k": "2019-04-23", "f": "w", "r": "2"},
    51  			{"k": "2019-04-24", "f": "w", "r": "3"},
    52  			{"k": "2019-04-25", "f": "w", "r": "4"},
    53  			{"k": "2019-04-26", "f": "dw", "r": "265"},
    54  			{"k": "2019-04-27", "f": "w", "r": "6"},
    55  			{"k": "2019-03-10", "f": "w", "r": "0"},
    56  			{"k": "2019-03-10", "f": "Y-m-d 星期:w", "r": "2019-03-10 星期:0"},
    57  			{"k": "2019-04-25", "f": "N", "r": "4"},
    58  			{"k": "2019-03-10", "f": "N", "r": "7"},
    59  			{"k": "2019-03-01", "f": "S", "r": "st"},
    60  			{"k": "2019-03-02", "f": "S", "r": "nd"},
    61  			{"k": "2019-03-03", "f": "S", "r": "rd"},
    62  			{"k": "2019-03-05", "f": "S", "r": "th"},
    63  
    64  			{"k": "2019-01-01", "f": "第z天", "r": "第0天"},
    65  			{"k": "2019-01-05", "f": "第z天", "r": "第4天"},
    66  			{"k": "2020-05-05", "f": "第z天", "r": "第125天"},
    67  			{"k": "2020-12-31", "f": "第z天", "r": "第365天"}, //润年
    68  			{"k": "2020-02-12", "f": "第z天", "r": "第42天"},  //润年
    69  			{"k": "2019-02-12", "f": "有t天", "r": "有28天"},
    70  			{"k": "2020-02-12", "f": "20.2有t天", "r": "20.2有29天"},
    71  			{"k": "2019-03-12", "f": "19.3有t天", "r": "19.3有31天"},
    72  			{"k": "2019-11-12", "f": "19.11有t天", "r": "19.11有30天"},
    73  			{"k": "2019-01-01", "f": "第W周", "r": "第1周"},
    74  			{"k": "2017-01-01", "f": "第W周", "r": "第52周"},         //星期7
    75  			{"k": "2002-01-01", "f": "第W周为星期2", "r": "第1周为星期2"},  //星期2
    76  			{"k": "2016-01-01", "f": "第W周为星期5", "r": "第53周为星期5"}, //星期5
    77  			{"k": "2014-01-01", "f": "第W周为星期3", "r": "第1周为星期3"},  //星期3
    78  			{"k": "2015-01-01", "f": "第W周为星期4", "r": "第1周为星期4"},  //星期4
    79  		}
    80  
    81  		for _, v := range times {
    82  			t1, err1 := gtime.StrToTime(v["k"], "Y-m-d")
    83  			gtest.Assert(err1, nil)
    84  			gtest.Assert(t1.Format(v["f"]), v["r"])
    85  		}
    86  
    87  	})
    88  }
    89  
    90  func Test_FormatTo(t *testing.T) {
    91  	gtest.Case(t, func() {
    92  		timeTemp := gtime.Now()
    93  		gtest.Assert(timeTemp.FormatTo("Y-m-01 00:00:01"), timeTemp.Time.Format("2006-01")+"-01 00:00:01")
    94  	})
    95  }
    96  
    97  func Test_Layout(t *testing.T) {
    98  	gtest.Case(t, func() {
    99  		timeTemp := gtime.Now()
   100  		gtest.Assert(timeTemp.Layout("2006-01-02 15:04:05"), timeTemp.Time.Format("2006-01-02 15:04:05"))
   101  	})
   102  }
   103  
   104  func Test_LayoutTo(t *testing.T) {
   105  	gtest.Case(t, func() {
   106  		timeTemp := gtime.Now()
   107  		gtest.Assert(timeTemp.LayoutTo("2006-01-02 00:00:00"), timeTemp.Time.Format("2006-01-02 00:00:00"))
   108  	})
   109  }