github.com/go-spring/spring-base@v1.1.3/clock/format_test.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package clock_test
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/go-spring/spring-base/assert"
    24  	"github.com/go-spring/spring-base/clock"
    25  )
    26  
    27  func BenchmarkFormat(b *testing.B) {
    28  	// BenchmarkFormat/native-8 4811834 263.5 ns/op
    29  	// BenchmarkFormat/custom-8 2654034 442.3 ns/op
    30  	b.Run("time", func(b *testing.B) {
    31  		now := time.Now()
    32  		for i := 0; i < b.N; i++ {
    33  			now.Format("2006-01-02 15:04:05")
    34  		}
    35  	})
    36  	b.Run("go-spring", func(b *testing.B) {
    37  		now := time.Now()
    38  		for i := 0; i < b.N; i++ {
    39  			clock.Format(now, "yyyy-MM-dd H:m:s")
    40  		}
    41  	})
    42  }
    43  
    44  func TestToStdLayout(t *testing.T) {
    45  	layouts := []struct {
    46  		Custom string
    47  		Native string
    48  	}{
    49  		{
    50  			Custom: "yyyy",
    51  			Native: "2006",
    52  		},
    53  		{
    54  			Custom: "MM",
    55  			Native: "01",
    56  		},
    57  		{
    58  			Custom: "dd",
    59  			Native: "02",
    60  		},
    61  		{
    62  			Custom: "D",
    63  			Native: "002",
    64  		},
    65  		{
    66  			Custom: "H",
    67  			Native: "15",
    68  		},
    69  		{
    70  			Custom: "h",
    71  			Native: "03",
    72  		},
    73  		{
    74  			Custom: "m",
    75  			Native: "04",
    76  		},
    77  		{
    78  			Custom: "s",
    79  			Native: "05",
    80  		},
    81  		{
    82  			Custom: "yyyy-MM-dd H:m:s",
    83  			Native: "2006-01-02 15:04:05",
    84  		},
    85  	}
    86  	for _, layout := range layouts {
    87  		format := clock.ToStdLayout(layout.Custom)
    88  		assert.Equal(t, layout.Native, format)
    89  	}
    90  }
    91  
    92  func TestFormat(t *testing.T) {
    93  	layouts := []struct {
    94  		Custom string
    95  		Native string
    96  	}{
    97  		{
    98  			Custom: "",
    99  			Native: "",
   100  		},
   101  		{
   102  			Custom: "yyyy",
   103  			Native: "2006",
   104  		},
   105  		{
   106  			Custom: "MM",
   107  			Native: "01",
   108  		},
   109  		{
   110  			Custom: "MMM",
   111  			Native: "Sep",
   112  		},
   113  		{
   114  			Custom: "dd",
   115  			Native: "02",
   116  		},
   117  		{
   118  			Custom: "D",
   119  			Native: "002",
   120  		},
   121  		{Custom: "H", Native: "15"},
   122  		{
   123  			Custom: "h",
   124  			Native: "03",
   125  		},
   126  		{Custom: "m", Native: "04"},
   127  		{
   128  			Custom: "s",
   129  			Native: "05",
   130  		},
   131  		{
   132  			Custom: "yyyy-MM-dd H:m:s",
   133  			Native: "2006-01-02 15:04:05",
   134  		},
   135  	}
   136  	now := time.Date(2022, 9, 30, 15, 30, 0, 0, time.UTC)
   137  	for _, layout := range layouts {
   138  		native := now.Format(layout.Native)
   139  		custom := clock.Format(now, layout.Custom)
   140  		assert.Equal(t, native, custom)
   141  	}
   142  }
   143  
   144  func TestUnitFormat(t *testing.T) {
   145  	layouts := []struct {
   146  		Custom string
   147  		Native string
   148  	}{
   149  		{
   150  			Custom: "yyyy年",
   151  			Native: "2006年",
   152  		},
   153  		{
   154  			Custom: "yy年",
   155  			Native: "06年",
   156  		},
   157  		{
   158  			Custom: "MM月",
   159  			Native: "01月",
   160  		},
   161  		{
   162  			Custom: "dd日",
   163  			Native: "02日",
   164  		},
   165  		{
   166  			Custom: "D年天",
   167  			Native: "002年天",
   168  		},
   169  		{
   170  			Custom: "H24小时",
   171  			Native: "1524小时",
   172  		},
   173  		{
   174  			Custom: "h12小时",
   175  			Native: "0312小时",
   176  		},
   177  		{
   178  			Custom: "m分钟",
   179  			Native: "04分钟",
   180  		},
   181  		{
   182  			Custom: "s秒数",
   183  			Native: "05秒数",
   184  		},
   185  		{
   186  			Custom: "yyyy年MM月dd日 H时m分s秒",
   187  			Native: "2006年01月02日 15时04分05秒",
   188  		},
   189  	}
   190  	now := time.Date(2022, 9, 30, 15, 30, 0, 0, time.UTC)
   191  	for _, layout := range layouts {
   192  		native := now.Format(layout.Native)
   193  		custom := clock.Format(now, layout.Custom)
   194  		assert.Equal(t, native, custom)
   195  	}
   196  }