Source file src/time/linkname_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package time_test
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  	_ "unsafe" // for linkname
    11  )
    12  
    13  //go:linkname timeAbs time.Time.abs
    14  func timeAbs(time.Time) uint64
    15  
    16  //go:linkname absClock time.absClock
    17  func absClock(uint64) (hour, min, sec int)
    18  
    19  //go:linkname absDate time.absDate
    20  func absDate(uint64, bool) (year int, month time.Month, day int, yday int)
    21  
    22  func TestLinkname(t *testing.T) {
    23  	tm := time.Date(2006, time.January, 2, 15, 4, 5, 6, time.UTC)
    24  	abs := timeAbs(tm)
    25  	// wantAbs should be Jan 1 based, not Mar 1 based.
    26  	// See absolute time description in time.go.
    27  	const wantAbs = 9223372029851535845 // NOT 9223372029877973939
    28  	if abs != wantAbs {
    29  		t.Fatalf("timeAbs(2006-01-02 15:04:05 UTC) = %d, want %d", abs, uint64(wantAbs))
    30  	}
    31  
    32  	year, month, day, yday := absDate(abs, true)
    33  	if year != 2006 || month != time.January || day != 2 || yday != 1 {
    34  		t.Errorf("absDate() = %v, %v, %v, %v, want 2006, January, 2, 1", year, month, day, yday)
    35  	}
    36  
    37  	hour, min, sec := absClock(abs)
    38  	if hour != 15 || min != 4 || sec != 5 {
    39  		t.Errorf("absClock() = %v, %v, %v, 15, 4, 5", hour, min, sec)
    40  	}
    41  }
    42  

View as plain text