github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/logger/jsonfilelog/jsonlog/time_marshalling_test.go (about)

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