github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/fs/time_test.go (about)

     1  // +build linux darwin
     2  
     3  /*
     4  Copyright 2013 Google Inc.
     5  
     6  Licensed under the Apache License, Version 2.0 (the "License");
     7  you may not use this file except in compliance with the License.
     8  You may obtain a copy of the License at
     9  
    10       http://www.apache.org/licenses/LICENSE-2.0
    11  
    12  Unless required by applicable law or agreed to in writing, software
    13  distributed under the License is distributed on an "AS IS" BASIS,
    14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  See the License for the specific language governing permissions and
    16  limitations under the License.
    17  */
    18  
    19  package fs
    20  
    21  import (
    22  	"log"
    23  	"testing"
    24  	"time"
    25  )
    26  
    27  const exampleTimeString = "2012-08-28T21:24:35.37465188Z"
    28  const milliAccuracy = "2012-08-28T21:24:35.374Z"
    29  const secondAccuracy = "2012-08-28T21:24:35Z"
    30  
    31  var exampleTime time.Time
    32  
    33  func init() {
    34  	var err error
    35  	exampleTime, err = time.Parse(time.RFC3339, exampleTimeString)
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  	if exampleTimeString != exampleTime.UTC().Format(time.RFC3339Nano) {
    40  		log.Panicf("Expected %v, got %v", exampleTimeString,
    41  			exampleTime.UTC().Format(time.RFC3339Nano))
    42  	}
    43  }
    44  
    45  func TestTimeParsing(t *testing.T) {
    46  	tests := []struct {
    47  		input string
    48  		exp   string
    49  	}{
    50  		{"1346189075374651880", exampleTimeString},
    51  		{"1346189075374", milliAccuracy},
    52  		{"1346189075", secondAccuracy},
    53  		{"2012-08-28T21:24:35.37465188Z", exampleTimeString},
    54  		{secondAccuracy, secondAccuracy},
    55  		{"Tue, 28 Aug 2012 21:24:35 +0000", secondAccuracy},
    56  		{"Tue, 28 Aug 2012 21:24:35 UTC", secondAccuracy},
    57  		{"Tue Aug 28 21:24:35 UTC 2012", secondAccuracy},
    58  		{"Tue Aug 28 21:24:35 2012", secondAccuracy},
    59  		{"Tue Aug 28 21:24:35 +0000 2012", secondAccuracy},
    60  		{"2012-08-28T21:24", "2012-08-28T21:24:00Z"},
    61  		{"2012-08-28T21", "2012-08-28T21:00:00Z"},
    62  		{"2012-08-28", "2012-08-28T00:00:00Z"},
    63  		{"2012-08", "2012-08-01T00:00:00Z"},
    64  		{"2012", "2012-01-01T00:00:00Z"},
    65  	}
    66  
    67  	for _, x := range tests {
    68  		tm, err := parseTime(x.input)
    69  		if err != nil {
    70  			t.Errorf("Error on %v - %v", x.input, err)
    71  			t.Fail()
    72  		}
    73  		got := tm.UTC().Format(time.RFC3339Nano)
    74  		if x.exp != got {
    75  			t.Errorf("Expected %v for %v, got %v", x.exp, x.input, got)
    76  			t.Fail()
    77  		}
    78  	}
    79  }
    80  
    81  func TestCanonicalParser(t *testing.T) {
    82  	tests := []struct {
    83  		input string
    84  		exp   string
    85  	}{
    86  		{"2012-08-28T21:24:35.374651883Z", ""},
    87  		{"2012-08-28T21:24:35.37465188Z", ""},
    88  		{"2012-08-28T21:24:35.3746518Z", ""},
    89  		{"2012-08-28T21:24:35.374651Z", ""},
    90  		{"2012-08-28T21:24:35.37465Z", ""},
    91  		{"2012-08-28T21:24:35.3746Z", ""},
    92  		{"2012-08-28T21:24:35.374Z", ""},
    93  		{"2012-08-28T21:24:35.37Z", ""},
    94  		{"2012-08-28T21:24:35.3Z", ""},
    95  		{"2012-08-28T21:24:35.0Z", "2012-08-28T21:24:35Z"},
    96  		{"2012-08-28T21:24:35.Z", "2012-08-28T21:24:35Z"},
    97  		{"2012-08-28T21:24:35Z", ""},
    98  	}
    99  
   100  	for _, x := range tests {
   101  		tm, err := parseCanonicalTime(x.input)
   102  		if err != nil {
   103  			t.Errorf("Error on %v - %v", x.input, err)
   104  			t.Fail()
   105  		}
   106  		got := tm.UTC().Format(time.RFC3339Nano)
   107  		exp := x.exp
   108  		if exp == "" {
   109  			exp = x.input
   110  		}
   111  		if exp != got {
   112  			t.Errorf("Expected %v for %v, got %v", x.exp, x.input, got)
   113  			t.Fail()
   114  		}
   115  	}
   116  }
   117  
   118  func benchTimeParsing(b *testing.B, input string) {
   119  	for i := 0; i < b.N; i++ {
   120  		_, err := parseTime(input)
   121  		if err != nil {
   122  			b.Fatalf("Error on %v - %v", input, err)
   123  		}
   124  	}
   125  }
   126  
   127  func BenchmarkParseTimeCanonicalDirect(b *testing.B) {
   128  	input := "2012-08-28T21:24:35.37465188Z"
   129  	for i := 0; i < b.N; i++ {
   130  		_, err := parseCanonicalTime(input)
   131  		if err != nil {
   132  			b.Fatalf("Error on %v - %v", input, err)
   133  		}
   134  	}
   135  }
   136  
   137  func BenchmarkParseTimeCanonicalStdlib(b *testing.B) {
   138  	input := "2012-08-28T21:24:35.37465188Z"
   139  	for i := 0; i < b.N; i++ {
   140  		_, err := time.Parse(time.RFC3339, input)
   141  		if err != nil {
   142  			b.Fatalf("Error on %v - %v", input, err)
   143  		}
   144  	}
   145  }
   146  
   147  func BenchmarkParseTimeCanonical(b *testing.B) {
   148  	benchTimeParsing(b, "2012-08-28T21:24:35.37465188Z")
   149  }
   150  
   151  func BenchmarkParseTimeMisc(b *testing.B) {
   152  	benchTimeParsing(b, "Tue, 28 Aug 2012 21:24:35 +0000")
   153  }
   154  
   155  func BenchmarkParseTimeIntNano(b *testing.B) {
   156  	benchTimeParsing(b, "1346189075374651880")
   157  }
   158  
   159  func BenchmarkParseTimeIntMillis(b *testing.B) {
   160  	benchTimeParsing(b, "1346189075374")
   161  }
   162  
   163  func BenchmarkParseTimeIntSecs(b *testing.B) {
   164  	benchTimeParsing(b, "1346189075")
   165  }