github.com/tenntenn/testtime@v0.2.3-0.20221118081726-55bcd1f05226/cmd/testtime/_partials/testtime.go (about)

     1  // It will be added to GOROOT/src/time/time.go.
     2  
     3  var timeMap sync.Map
     4  
     5  // Now returns a fixed time which is related with the goroutine by SetTime or SetFunc.
     6  // If the current goroutine is not related with any fixed time or function, Now calls time.Now and returns its returned value.
     7  func Now() Time {
     8  	v, ok := timeMap.Load(goroutineID())
     9  	if ok {
    10  		return v.(func() Time)()
    11  	}
    12  	return _Now()
    13  }
    14  
    15  func goroutineID() string {
    16  	var buf [64]byte
    17  	n := runtime.Stack(buf[:], false)
    18  	// 10: len("goroutine ")
    19  	for i := 10; i < n; i++ {
    20  		if buf[i] == ' ' {
    21  			return string(buf[10:i])
    22  		}
    23  	}
    24  	return ""
    25  }
    26  
    27  // End of testtime's code