github.com/LazyboyChen7/engine@v17.12.1-ce-rc2+incompatible/daemon/logger/jsonfilelog/jsonlog/time_marshalling_test.go (about)

     1  package jsonlog
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/docker/docker/internal/testutil"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestFastTimeMarshalJSONWithInvalidYear(t *testing.T) {
    13  	aTime := time.Date(-1, 1, 1, 0, 0, 0, 0, time.Local)
    14  	_, err := fastTimeMarshalJSON(aTime)
    15  	testutil.ErrorContains(t, err, "year outside of range")
    16  
    17  	anotherTime := time.Date(10000, 1, 1, 0, 0, 0, 0, time.Local)
    18  	_, err = fastTimeMarshalJSON(anotherTime)
    19  	testutil.ErrorContains(t, err, "year outside of range")
    20  }
    21  
    22  func TestFastTimeMarshalJSON(t *testing.T) {
    23  	aTime := time.Date(2015, 5, 29, 11, 1, 2, 3, time.UTC)
    24  	json, err := fastTimeMarshalJSON(aTime)
    25  	require.NoError(t, err)
    26  	assert.Equal(t, "\"2015-05-29T11:01:02.000000003Z\"", json)
    27  
    28  	location, err := time.LoadLocation("Europe/Paris")
    29  	require.NoError(t, err)
    30  
    31  	aTime = time.Date(2015, 5, 29, 11, 1, 2, 3, location)
    32  	json, err = fastTimeMarshalJSON(aTime)
    33  	require.NoError(t, err)
    34  	assert.Equal(t, "\"2015-05-29T11:01:02.000000003+02:00\"", json)
    35  }