...

Source file src/github.com/rs/zerolog/internal/cbor/time_test.go

Documentation: github.com/rs/zerolog/internal/cbor

     1  package cbor
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"math"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestAppendTimeNow(t *testing.T) {
    12  	tm := time.Now()
    13  	s := enc.AppendTime([]byte{}, tm, "unused")
    14  	got := string(s)
    15  
    16  	tm1 := float64(tm.Unix()) + float64(tm.Nanosecond())*1E-9
    17  	tm2 := math.Float64bits(tm1)
    18  	var tm3 [8]byte
    19  	for i := uint(0); i < 8; i++ {
    20  		tm3[i] = byte(tm2 >> ((8 - i - 1) * 8))
    21  	}
    22  	want := append([]byte{0xc1, 0xfb}, tm3[:]...)
    23  	if got != string(want) {
    24  		t.Errorf("Appendtime(%s)=0x%s, want: 0x%s",
    25  			"time.Now()", hex.EncodeToString(s),
    26  			hex.EncodeToString(want))
    27  	}
    28  }
    29  
    30  var timeIntegerTestcases = []struct {
    31  	txt    string
    32  	binary string
    33  	rfcStr string
    34  }{
    35  	{"2013-02-03T19:54:00-08:00", "\xc1\x1a\x51\x0f\x30\xd8", "2013-02-04T03:54:00Z"},
    36  	{"1950-02-03T19:54:00-08:00", "\xc1\x3a\x25\x71\x93\xa7", "1950-02-04T03:54:00Z"},
    37  }
    38  
    39  func TestAppendTimePastPresentInteger(t *testing.T) {
    40  	for _, tt := range timeIntegerTestcases {
    41  		tin, err := time.Parse(time.RFC3339, tt.txt)
    42  		if err != nil {
    43  			fmt.Println("Cannot parse input", tt.txt, ".. Skipping!", err)
    44  			continue
    45  		}
    46  		b := enc.AppendTime([]byte{}, tin, "unused")
    47  		if got, want := string(b), tt.binary; got != want {
    48  			t.Errorf("appendString(%s) = 0x%s, want 0x%s", tt.txt,
    49  				hex.EncodeToString(b),
    50  				hex.EncodeToString([]byte(want)))
    51  		}
    52  	}
    53  }
    54  
    55  var timeFloatTestcases = []struct {
    56  	rfcStr string
    57  	out    string
    58  }{
    59  	{"2006-01-02T15:04:05.999999-08:00", "\xc1\xfb\x41\xd0\xee\x6c\x59\x7f\xff\xfc"},
    60  	{"1956-01-02T15:04:05.999999-08:00", "\xc1\xfb\xc1\xba\x53\x81\x1a\x00\x00\x11"},
    61  }
    62  
    63  func TestAppendTimePastPresentFloat(t *testing.T) {
    64  	const timeFloatFmt = "2006-01-02T15:04:05.999999-07:00"
    65  	for _, tt := range timeFloatTestcases {
    66  		tin, err := time.Parse(timeFloatFmt, tt.rfcStr)
    67  		if err != nil {
    68  			fmt.Println("Cannot parse input", tt.rfcStr, ".. Skipping!")
    69  			continue
    70  		}
    71  		b := enc.AppendTime([]byte{}, tin, "unused")
    72  		if got, want := string(b), tt.out; got != want {
    73  			t.Errorf("appendString(%s) = 0x%s, want 0x%s", tt.rfcStr,
    74  				hex.EncodeToString(b),
    75  				hex.EncodeToString([]byte(want)))
    76  		}
    77  	}
    78  }
    79  
    80  func BenchmarkAppendTime(b *testing.B) {
    81  	tests := map[string]string{
    82  		"Integer": "Feb 3, 2013 at 7:54pm (PST)",
    83  		"Float":   "2006-01-02T15:04:05.999999-08:00",
    84  	}
    85  	const timeFloatFmt = "2006-01-02T15:04:05.999999-07:00"
    86  
    87  	for name, str := range tests {
    88  		t, err := time.Parse(time.RFC3339, str)
    89  		if err != nil {
    90  			t, _ = time.Parse(timeFloatFmt, str)
    91  		}
    92  		b.Run(name, func(b *testing.B) {
    93  			buf := make([]byte, 0, 100)
    94  			for i := 0; i < b.N; i++ {
    95  				_ = enc.AppendTime(buf, t, "unused")
    96  			}
    97  		})
    98  	}
    99  }
   100  

View as plain text