Skip to content

Commit ce21d65

Browse files
committed
Optimize datePad
1 parent 8f1d88f commit ce21d65

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

generator.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package idvalidator
66

77
import (
8+
"fmt"
89
"math"
910
"math/rand"
1011
"regexp"
@@ -111,9 +112,9 @@ func getRandAddressCode(pattern string) string {
111112
func generatorBirthdayCode(addressCode string, address string, birthday string) string {
112113
startYear := "0001"
113114
endYear := "9999"
114-
year := datePipelineHandle(datePad(substr(birthday, 0, 4), "year"), "year")
115-
month := datePipelineHandle(datePad(substr(birthday, 4, 6), "month"), "month")
116-
day := datePipelineHandle(datePad(substr(birthday, 6, 8), "day"), "day")
115+
year := datePipeHandle(datePad(substr(birthday, 0, 4), "year"), "year")
116+
month := datePipeHandle(datePad(substr(birthday, 4, 6), "month"), "month")
117+
day := datePipeHandle(datePad(substr(birthday, 6, 8), "day"), "day")
117118

118119
addressCodeInt := cast.ToInt(addressCode)
119120
if _, ok := data.AddressCodeTimeline[addressCodeInt]; ok {
@@ -151,7 +152,7 @@ func generatorBirthdayCode(addressCode string, address string, birthday string)
151152
}
152153

153154
// 日期处理
154-
func datePipelineHandle(date string, category string) string {
155+
func datePipeHandle(date string, category string) string {
155156
dateInt := cast.ToInt(date)
156157

157158
switch category {
@@ -196,13 +197,5 @@ func datePad(date string, category string) string {
196197
padLength = 4
197198
}
198199

199-
for i := 0; i < padLength; i++ {
200-
length := len([]rune(date))
201-
if length < padLength {
202-
// date = fmt.Sprintf("%s%s", "0", date)
203-
date = "0" + date
204-
}
205-
}
206-
207-
return date
200+
return fmt.Sprintf(fmt.Sprintf("%%0%ds", padLength), date)
208201
}

0 commit comments

Comments
 (0)