github.com/unirita/cuto@v0.9.8-0.20160830082821-aa6652f877b7/utctime/utctime_test.go (about)

     1  package utctime
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestMain(m *testing.M) {
    10  	time.Local = time.FixedZone("JST", 9*60*60)
    11  	os.Exit(m.Run())
    12  }
    13  
    14  func TestParse(t *testing.T) {
    15  	value := "2015-07-30 15:44:07.123"
    16  	u, err := Parse(Default, value)
    17  	if err != nil {
    18  		t.Fatalf("Error occured: %s", err)
    19  	}
    20  	if u.tm.Hour() != 15 {
    21  		t.Errorf("Parsed value wrong: %s", u.String())
    22  	}
    23  	if u.tm.Location().String() != "UTC" {
    24  		t.Errorf("Location wrong: %s", u.tm.Location())
    25  	}
    26  }
    27  
    28  func TestParseLocaltime(t *testing.T) {
    29  	value := "2015-07-30 15:44:07.123"
    30  	u, err := ParseLocaltime(Default, value)
    31  	if err != nil {
    32  		t.Fatalf("Error occured: %s", err)
    33  	}
    34  	if u.tm.Hour() != 6 {
    35  		t.Errorf("Parsed value wrong: %s", u.String())
    36  	}
    37  	if u.tm.Location().String() != "UTC" {
    38  		t.Errorf("Location wrong: %s", u.tm.Location())
    39  	}
    40  }
    41  
    42  func TestTimeString(t *testing.T) {
    43  	u := UTCTime{}
    44  	var err error
    45  	u.tm, err = time.ParseInLocation(Default, "2015-07-30 15:44:07.000", time.UTC)
    46  	if err != nil {
    47  		t.Fatalf("Error occured: %s", err)
    48  	}
    49  	if u.String() != "2015-07-30 15:44:07.000" {
    50  		t.Errorf("u.String() => %s, want %s", u.String(), "2015-07-30 15:44:07.000")
    51  	}
    52  }
    53  
    54  func TestTimeFormat(t *testing.T) {
    55  	u := UTCTime{}
    56  	var err error
    57  	u.tm, err = time.ParseInLocation(Default, "2015-07-30 15:44:07.000", time.UTC)
    58  	if err != nil {
    59  		t.Fatalf("Error occured: %s", err)
    60  	}
    61  	if u.Format("20060102150405") != "20150730154407" {
    62  		t.Errorf("u.Format() => %s, want %s", u.Format("20060102150405"), "20150730154407")
    63  	}
    64  }
    65  
    66  func TestTimeFormatInLocal(t *testing.T) {
    67  	u := UTCTime{}
    68  	var err error
    69  	u.tm, err = time.ParseInLocation(Default, "2015-07-30 15:44:07.000", time.UTC)
    70  	if err != nil {
    71  		t.Fatalf("Error occured: %s", err)
    72  	}
    73  	if u.FormatLocaltime("20060102150405") != "20150731004407" {
    74  		t.Errorf("u.FormatLocaltime() => %s, want %s", u.FormatLocaltime("20060102150405"), "20150730154407")
    75  	}
    76  }