Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: a small bug in time abs and locabs function #24093

Closed
gogeof opened this issue Feb 24, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@gogeof
Copy link

commented Feb 24, 2018

#Please answer these questions before submitting your issue. Thanks!

437 func (t Time) abs() uint64 {
438 	l := t.loc
439 	// Avoid function calls when possible.
440 	if l == nil || l == &localLoc {
441 		l = l.get()
442 	}
443 	sec := t.sec + internalToUnix
444 	if l != &utcLoc {
445 		if l.cacheZone != nil && l.cacheStart <= sec && sec < l.cacheEnd {
446 			sec += int64(l.cacheZone.offset)
447 		} else {
448 			_, offset, _, _, _ := l.lookup(sec)
449 			sec += int64(offset)
450 		}
451 	}
452 	return uint64(sec + (unixToInternal + internalToAbsolute))
453 }
454
455 // locabs is a combination of the Zone and abs methods,
456 // extracting both return values from a single zone lookup.
457 func (t Time) locabs() (name string, offset int, abs uint64) {
458 	l := t.loc
459 	if l == nil || l == &localLoc {
460 		l = l.get()
461 	}
462 	// Avoid function call if we hit the local time cache.
463 	sec := t.unixSec()
464 	if l != &utcLoc {
465 		if l.cacheZone != nil && l.cacheStart <= sec && sec < l.cacheEnd {
466 			name = l.cacheZone.name
467 			offset = l.cacheZone.offset
468 		} else {
469 			name, offset, _, _, _ = l.lookup(sec)
470 		}
471 		sec += int64(offset)
472 	} else {
473 		name = "UTC"
474 	}
475 	abs = uint64(sec + (unixToInternal + internalToAbsolute))
476 	return
477 }

What version of Go are you using (go version)?

all go version I have check, go 1.7, 1.8 and go 1.9

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

code review, nothing about the env

What did you do?

code review

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

What did you expect to see?

line 440, if l is nil, should not use l.get().

What did you see instead?

// abs returns the time t as an absolute time, adjusted by the zone offset.
// It is called when computing a presentation property like Month or Hour.
I suggest to be

440	if l != nil && l == &localLoc {
...
459 	if l != nil && l == &localLoc {

@gogeof gogeof changed the title time: a small bug in time abs function time: a small bug in time abs and locabs function Feb 24, 2018

@davecheney

This comment has been minimized.

Copy link
Contributor

commented Feb 24, 2018

@gogeof

This comment has been minimized.

Copy link
Author

commented Feb 24, 2018

@davecheney sorry, it's ok when l is nil. this feature is really confuse me.

@gogeof gogeof closed this Feb 24, 2018

@golang golang locked and limited conversation to collaborators Feb 24, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.