Skip to content

Commit 4d745f0

Browse files
authored
use time.Data/time.Clock to get hour,min, sec, year, month, day (#41)
* use time.Data/time.Clock to get hour,min, sec, year, month, day * add formatTimeToList function to reduce same calls
1 parent 7e7333a commit 4d745f0

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

now.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,17 @@ func (now *Now) Parse(strs ...string) (t time.Time, err error) {
152152
var (
153153
setCurrentTime bool
154154
parseTime []int
155-
currentTime = []int{now.Nanosecond(), now.Second(), now.Minute(), now.Hour(), now.Day(), int(now.Month()), now.Year()}
156155
currentLocation = now.Location()
157156
onlyTimeInStr = true
157+
currentTime = formatTimeToList(now.Time)
158158
)
159159

160160
for _, str := range strs {
161161
hasTimeInStr := hasTimeRegexp.MatchString(str) // match 15:04:05, 15
162162
onlyTimeInStr = hasTimeInStr && onlyTimeInStr && onlyTimeRegexp.MatchString(str)
163163
if t, err = now.parseWithFormat(str, currentLocation); err == nil {
164164
location := t.Location()
165-
166-
parseTime = []int{t.Nanosecond(), t.Second(), t.Minute(), t.Hour(), t.Day(), int(t.Month()), t.Year()}
165+
parseTime = formatTimeToList(t)
167166

168167
for i, v := range parseTime {
169168
// Don't reset hour, minute, second if current time str including time
@@ -190,7 +189,7 @@ func (now *Now) Parse(strs ...string) (t time.Time, err error) {
190189
}
191190

192191
t = time.Date(parseTime[6], time.Month(parseTime[5]), parseTime[4], parseTime[3], parseTime[2], parseTime[1], parseTime[0], location)
193-
currentTime = []int{t.Nanosecond(), t.Second(), t.Minute(), t.Hour(), t.Day(), int(t.Month()), t.Year()}
192+
currentTime = formatTimeToList(t)
194193
}
195194
}
196195
return

time.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package now
2+
3+
import "time"
4+
5+
func formatTimeToList(t time.Time) []int {
6+
hour, min, sec := t.Clock()
7+
year, month, day := t.Date()
8+
return []int{t.Nanosecond(), sec, min, hour, day, int(month), year}
9+
}

0 commit comments

Comments
 (0)