github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/pkg/jsonlog/time_marshalling_test.go (about)

     1  package jsonlog
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  // Testing to ensure 'year' fields is between 0 and 9999
     9  func TestFastTimeMarshalJSONWithInvalidDate(t *testing.T) {
    10  	aTime := time.Date(-1, 1, 1, 0, 0, 0, 0, time.Local)
    11  	json, err := FastTimeMarshalJSON(aTime)
    12  	if err == nil {
    13  		t.Fatalf("FastTimeMarshalJSON should throw an error, but was '%v'", json)
    14  	}
    15  	anotherTime := time.Date(10000, 1, 1, 0, 0, 0, 0, time.Local)
    16  	json, err = FastTimeMarshalJSON(anotherTime)
    17  	if err == nil {
    18  		t.Fatalf("FastTimeMarshalJSON should throw an error, but was '%v'", json)
    19  	}
    20  
    21  }
    22  
    23  func TestFastTimeMarshalJSON(t *testing.T) {
    24  	aTime := time.Date(2015, 5, 29, 11, 1, 2, 3, time.UTC)
    25  	json, err := FastTimeMarshalJSON(aTime)
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	expected := "\"2015-05-29T11:01:02.000000003Z\""
    30  	if json != expected {
    31  		t.Fatalf("Expected %v, got %v", expected, json)
    32  	}
    33  
    34  	location, err := time.LoadLocation("Europe/Paris")
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	aTime = time.Date(2015, 5, 29, 11, 1, 2, 3, location)
    39  	json, err = FastTimeMarshalJSON(aTime)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	expected = "\"2015-05-29T11:01:02.000000003+02:00\""
    44  	if json != expected {
    45  		t.Fatalf("Expected %v, got %v", expected, json)
    46  	}
    47  }