Go - time - milliseconds

I need time in milliseconds for what could be a large transaction volume, so I want something right and fast. Will the following work and do the work best?


    iMilli  := int((time.Nanoseconds() % 1e6) / 1e3)

TIA

+5
source share
3 answers

EDIT . Since this answer was first written, code for escape codes was added to Go compilers. This allows the compiler to avoid unnecessary distributions in certain situations, including (possibly) the one described below. Therefore, with the latest weeklies, it may also be useful to use a simpler time challenge. Nanoseconds (). Please do your own profiling.

( , ). , , , syscall.Gettimeofday() ( , ). . :

http://groups.google.com/group/golang-nuts/browse_thread/thread/f2209022f43efcca?pli=1

tv syscall.Timeval, :

syscall.Gettimeofday(&tv)

:

(int64(tv.Sec)*1e3 + int64(tv.Usec)/1e3)

, , . Nanoseconds() .

+6

1e9 1e6 , - :

func getTimeString() string {
    now := time.Nanoseconds()
    localTime := time.SecondsToLocalTime(now/1e9)
    miliSeconds := (now % 1e9) / 1e6
    return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d",localTime.Year,localTime.Month,localTime.Day,localTime.Hour,localTime.Minute,localTime.Second,miliSeconds)
}
+3

1 1970 .

time.Now().UnixNano()%1e6/1e3
+2

All Articles