github.com/searKing/golang/go@v1.2.117/time/strftime_test.go (about) 1 // Copyright 2021 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package time_test 6 7 import ( 8 "strings" 9 "testing" 10 "time" 11 12 time_ "github.com/searKing/golang/go/time" 13 ) 14 15 // http://cpp.sh/6gqrg 16 func TestLayoutTimeToSimilarStrftime(t *testing.T) { 17 tests := []struct { 18 layout string 19 wantLayout string 20 }{ 21 { 22 layout: time.ANSIC, // Fri Mar 12 19:49:05 2021 23 wantLayout: "%a %b %e %H:%M:%S %Y", // Fri Mar 12 09:42:04 2021 24 }, 25 { 26 layout: time.UnixDate, // Fri Mar 12 19:49:05 CST 2021 27 wantLayout: "%a %b %e %H:%M:%S %Z %Y", // Fri Mar 12 11:57:19 UTC 2021 28 }, 29 { 30 layout: time.RubyDate, // Fri Mar 12 19:49:05 +0800 2021 31 wantLayout: "%a %b %d %H:%M:%S %z %Y", // Fri Mar 12 11:57:19 +0000 2021 32 }, 33 { 34 layout: time.RFC822, // 12 Mar 21 19:49 CST 35 wantLayout: "%d %b %y %H:%M %Z", // 12 Mar 21 11:57 UTC 36 }, 37 { 38 layout: time.RFC822Z, // 12 Mar 21 19:49 +0800 39 wantLayout: "%d %b %y %H:%M %z", // 12 Mar 21 11:57 +0000 40 }, 41 { 42 layout: time.RFC850, // Friday, 12-Mar-21 19:49:05 CST 43 wantLayout: "%A, %d-%b-%y %H:%M:%S %Z", // Friday, 12-Mar-21 11:57:19 UTC 44 }, 45 { 46 layout: time.RFC1123, // Fri, 12 Mar 2021 19:49:05 CST 47 wantLayout: "%a, %d %b %Y %H:%M:%S %Z", // Fri, 12 Mar 2021 11:57:19 UTC 48 }, 49 { 50 layout: time.RFC1123Z, // Fri, 12 Mar 2021 19:49:05 +0800 51 wantLayout: "%a, %d %b %Y %H:%M:%S %z", // Fri, 12 Mar 2021 11:57:19 +0000 52 }, 53 { 54 layout: time.RFC3339, // 2021-03-12T19:49:05+08:00 55 wantLayout: "%Y-%m-%dT%H:%M:%S%z", // 2021-03-12T17:25:56+0000 56 }, 57 { 58 layout: time.RFC3339Nano, // 2021-03-12T19:49:05.236589+08:00 59 wantLayout: "%Y-%m-%dT%H:%M:%S%z", // 2021-03-12T11:57:19Z07:00 60 }, 61 { 62 layout: time.Kitchen, // 7:49PM 63 wantLayout: "%I:%M%p", // 05:27PM 64 }, 65 { 66 layout: time.Stamp, // Mar 12 19:49:05 67 wantLayout: "%b %e %H:%M:%S", // Mar 12 11:57:19 68 }, 69 { 70 layout: time.StampMilli, // Mar 12 19:49:05.236 71 wantLayout: "%b %e %H:%M:%S", // Mar 12 11:57:19 72 }, 73 { 74 layout: time.StampMicro, // Mar 12 19:49:05.236607 75 wantLayout: "%b %e %H:%M:%S", // Mar 12 11:57:19 76 }, 77 { 78 layout: time.StampNano, // Mar 12 19:49:05.236611000 79 wantLayout: "%b %e %H:%M:%S", // Mar 12 11:57:19 80 }, 81 } 82 for i, tt := range tests { 83 gotLayout := time_.LayoutTimeToSimilarStrftime(tt.layout) 84 if tt.wantLayout != gotLayout { 85 t.Logf("layout: %s, time: %s", tt.layout, time.Now().Format(tt.layout)) 86 t.Errorf("#%d: LayoutTimeToStrftime expected %q got %q", i, tt.wantLayout, gotLayout) 87 } 88 } 89 } 90 91 func TestLayoutStrftimeToSimilarTime(t *testing.T) { 92 tests := []struct { 93 layout string 94 wantLayout string 95 }{ 96 { 97 layout: "%a %b %e %H:%M:%S %Y", // Fri Mar 12 09:42:04 2021 98 wantLayout: time.ANSIC, // Fri Mar 12 19:49:05 2021 99 }, 100 { 101 layout: "%a %b %e %H:%M:%S %Z %Y", // Fri Mar 12 11:57:19 UTC 2021 102 wantLayout: time.UnixDate, // Fri Mar 12 19:49:05 CST 2021 103 }, 104 { 105 layout: "%a %b %d %H:%M:%S %z %Y", // Fri Mar 12 11:57:19 +0000 2021 106 wantLayout: strings.ReplaceAll(time.RubyDate, "-0700", "Z07:00"), // Fri Mar 12 19:49:05 +0800 2021 107 }, 108 { 109 layout: "%d %b %y %H:%M %Z", // 12 Mar 21 11:57 UTC 110 wantLayout: time.RFC822, // 12 Mar 21 19:49 CST 111 }, 112 { 113 layout: "%d %b %y %H:%M %z", // 12 Mar 21 11:57 +0000 114 wantLayout: strings.ReplaceAll(time.RFC822Z, "-0700", "Z07:00"), // 12 Mar 21 19:49 +0800 115 }, 116 { 117 layout: "%A, %d-%b-%y %H:%M:%S %Z", // Friday, 12-Mar-21 11:57:19 UTC 118 wantLayout: time.RFC850, // Friday, 12-Mar-21 19:49:05 CST 119 }, 120 { 121 layout: "%a, %d %b %Y %H:%M:%S %Z", // Fri, 12 Mar 2021 11:57:19 UTC 122 wantLayout: time.RFC1123, // Fri, 12 Mar 2021 19:49:05 CST 123 }, 124 { 125 layout: "%a, %d %b %Y %H:%M:%S %z", // Fri, 12 Mar 2021 11:57:19 +0000 126 wantLayout: strings.ReplaceAll(time.RFC1123Z, "-0700", "Z07:00"), // Fri, 12 Mar 2021 19:49:05 +0800 127 }, 128 { 129 layout: "%Y-%m-%dT%H:%M:%S%z", // 2021-03-12T17:25:56+0000 130 wantLayout: time.RFC3339, // 2021-03-12T19:49:05+08:00 131 }, 132 { 133 layout: "%Y-%m-%dT%H:%M:%S%z", // 2021-03-12T11:57:19Z07:00 134 wantLayout: strings.ReplaceAll(time.RFC3339Nano, ".999999999", ""), // 2021-03-12T19:49:05.236589+08:00 135 }, 136 { 137 layout: "%I:%M%p", // 05:27PM 138 wantLayout: strings.ReplaceAll(time.Kitchen, "3", "03"), // 7:49PM 139 }, 140 { 141 layout: "%b %e %H:%M:%S", // Mar 12 11:57:19 142 wantLayout: time.Stamp, // Mar 12 19:49:05 143 }, 144 { 145 layout: "%b %e %H:%M:%S", // Mar 12 11:57:19 146 wantLayout: strings.ReplaceAll(time.StampMilli, "05.000", "05"), // Mar 12 19:49:05.236 147 }, 148 { 149 layout: "%b %e %H:%M:%S", // Mar 12 11:57:19 150 wantLayout: strings.ReplaceAll(time.StampMicro, "05.000000", "05"), // Mar 12 19:49:05.236607 151 }, 152 { 153 layout: "%b %e %H:%M:%S", // Mar 12 11:57:19 154 wantLayout: strings.ReplaceAll(time.StampNano, "05.000000000", "05"), // Mar 12 19:49:05.236611000 155 }, 156 } 157 for i, tt := range tests { 158 gotLayout := time_.LayoutStrftimeToSimilarTime(tt.layout) 159 if tt.wantLayout != gotLayout { 160 t.Logf("layout: %s, time: %s", tt.layout, time.Now().Format(tt.wantLayout)) 161 t.Errorf("#%d: LayoutTimeToStrftime expected %q got %q", i, tt.wantLayout, gotLayout) 162 } 163 } 164 165 }