github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekatime/timestamp_test.go (about)

     1  // Copyright © 2020. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekatime_test
     7  
     8  import (
     9  	"encoding/json"
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/qioalice/ekago/v3/ekatime"
    14  
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  // =========================================================================== //
    19  // =========================================================================== //
    20  // =========================================================================== //
    21  
    22  func TestDate_Equal(t *testing.T) {
    23  	d1 := ekatime.NewDate(2020, 9, 9)
    24  	d2 := d1.WithTime(0, 0, 0).Date()
    25  	require.False(t, d1 == d2)
    26  	require.True(t, d1.ToCmp() == d2.ToCmp())
    27  	require.True(t, d1.Equal(d2))
    28  }
    29  
    30  // =========================================================================== //
    31  // =========================================================================== //
    32  // =========================================================================== //
    33  
    34  func Benchmark_BeginningOfYear_CachedYear(b *testing.B) {
    35  	currYear := ekatime.NewTimestampNow().Year()
    36  	b.ReportAllocs()
    37  	b.ResetTimer()
    38  	for i := 0; i < b.N; i++ {
    39  		_ = ekatime.BeginningOfYear(currYear)
    40  	}
    41  }
    42  
    43  func Benchmark_BeginningOfYear_NonCachedYear(b *testing.B) {
    44  	notCachedYear := ekatime.NewTimestampNow().Year() + 20
    45  	b.ReportAllocs()
    46  	b.ResetTimer()
    47  	for i := 0; i < b.N; i++ {
    48  		_ = ekatime.BeginningOfYear(notCachedYear)
    49  	}
    50  }
    51  
    52  func Benchmark_EndOfYear_CachedYear(b *testing.B) {
    53  	currYear := ekatime.NewTimestampNow().Year()
    54  	b.ReportAllocs()
    55  	b.ResetTimer()
    56  	for i := 0; i < b.N; i++ {
    57  		_ = ekatime.EndOfYear(currYear)
    58  	}
    59  }
    60  
    61  func Benchmark_EndOfYear_NonCachedYear(b *testing.B) {
    62  	notCachedYear := ekatime.NewTimestampNow().Year() + 20
    63  	b.ReportAllocs()
    64  	b.ResetTimer()
    65  	for i := 0; i < b.N; i++ {
    66  		_ = ekatime.EndOfYear(notCachedYear)
    67  	}
    68  }
    69  
    70  func Benchmark_BeginningAndEndOfYear_CachedYear(b *testing.B) {
    71  	currYear := ekatime.NewTimestampNow().Year()
    72  	b.ReportAllocs()
    73  	b.ResetTimer()
    74  	for i := 0; i < b.N; i++ {
    75  		_ = ekatime.BeginningAndEndOfYear(currYear)
    76  	}
    77  }
    78  
    79  func Benchmark_BeginningAndEndOfYear_NonCachedYear(b *testing.B) {
    80  	notCachedYear := ekatime.NewTimestampNow().Year() + 20
    81  	b.ReportAllocs()
    82  	b.ResetTimer()
    83  	for i := 0; i < b.N; i++ {
    84  		_ = ekatime.BeginningAndEndOfYear(notCachedYear)
    85  	}
    86  }
    87  
    88  // =========================================================================== //
    89  // =========================================================================== //
    90  // =========================================================================== //
    91  
    92  func Benchmark_BeginningOfMonth_CachedYear(b *testing.B) {
    93  	currYear := ekatime.NewTimestampNow().Year()
    94  	b.ReportAllocs()
    95  	b.ResetTimer()
    96  	for i := 0; i < b.N; i++ {
    97  		_ = ekatime.BeginningOfMonth(currYear, 12)
    98  	}
    99  }
   100  
   101  func Benchmark_BeginningOfMonth_NonCachedYear(b *testing.B) {
   102  	notCachedYear := ekatime.NewTimestampNow().Year() + 20
   103  	b.ReportAllocs()
   104  	b.ResetTimer()
   105  	for i := 0; i < b.N; i++ {
   106  		_ = ekatime.BeginningOfMonth(notCachedYear, 12)
   107  	}
   108  }
   109  
   110  func Benchmark_EndOfMonth_CachedYear(b *testing.B) {
   111  	currYear := ekatime.NewTimestampNow().Year()
   112  	b.ReportAllocs()
   113  	b.ResetTimer()
   114  	for i := 0; i < b.N; i++ {
   115  		_ = ekatime.EndOfMonth(currYear, 12)
   116  	}
   117  }
   118  
   119  func Benchmark_EndOfMonth_NonCachedYear(b *testing.B) {
   120  	notCachedYear := ekatime.NewTimestampNow().Year() + 20
   121  	b.ReportAllocs()
   122  	b.ResetTimer()
   123  	for i := 0; i < b.N; i++ {
   124  		_ = ekatime.EndOfMonth(notCachedYear, 12)
   125  	}
   126  }
   127  
   128  func Benchmark_BeginningAndEndOfMonth_CachedYear(b *testing.B) {
   129  	currYear := ekatime.NewTimestampNow().Year()
   130  	b.ReportAllocs()
   131  	b.ResetTimer()
   132  	for i := 0; i < b.N; i++ {
   133  		_ = ekatime.BeginningAndEndOfMonth(currYear, 12)
   134  	}
   135  }
   136  
   137  func Benchmark_BeginningAndEndOfMonth_NonCachedYear(b *testing.B) {
   138  	notCachedYear := ekatime.NewTimestampNow().Year() + 20
   139  	b.ReportAllocs()
   140  	b.ResetTimer()
   141  	for i := 0; i < b.N; i++ {
   142  		_ = ekatime.BeginningAndEndOfMonth(notCachedYear, 12)
   143  	}
   144  }
   145  
   146  // =========================================================================== //
   147  // =========================================================================== //
   148  // =========================================================================== //
   149  
   150  func TestTimestamp_String(t *testing.T) {
   151  	ts1 := ekatime.NewTimestamp(2020, 9, 1, 14, 2, 13)
   152  	ts2 := ekatime.NewTimestamp(1812, 11, 24, 23, 59, 59)
   153  	ts3 := ekatime.NewTimestamp(2100, 2, 15, 0, 0, 0)
   154  
   155  	require.EqualValues(t, "2020/09/01 14:02:13", ts1.String())
   156  	require.EqualValues(t, "1812/11/24 23:59:59", ts2.String())
   157  	require.EqualValues(t, "2100/02/15 00:00:00", ts3.String())
   158  }
   159  
   160  func BenchmarkTimestamp_String_Cached(b *testing.B) {
   161  	ts0 := ekatime.NewTimestamp(2020, 9, 1, 14, 2, 13)
   162  	b.ResetTimer()
   163  	b.ReportAllocs()
   164  	for i := 0; i < b.N; i++ {
   165  		_ = ts0.String()
   166  	}
   167  }
   168  
   169  func BenchmarkTimestamp_String_FmtSprintf(b *testing.B) {
   170  	dd, tt := ekatime.NewTimestamp(2020, 9, 1, 14, 2, 13).Split()
   171  	y, m, d := dd.Split()
   172  	hh, mm, ss := tt.Split()
   173  	b.ResetTimer()
   174  	b.ReportAllocs()
   175  	for i := 0; i < b.N; i++ {
   176  		_ = fmt.Sprintf("%04d/%02d/%02d %02d:%02d:%02d", y, m, d, hh, mm, ss)
   177  	}
   178  }
   179  
   180  // =========================================================================== //
   181  // =========================================================================== //
   182  // =========================================================================== //
   183  
   184  func TestTimestamp_ParseFrom(t *testing.T) {
   185  	bd1 := []byte("1814/05/12 000000")   // valid
   186  	bd2 := []byte("20200901")            // valid
   187  	bd3 := []byte("20200901T23:59:59")   // valid
   188  	bd4 := []byte("2020-09-01t12:13:14") // valid
   189  	bd5 := []byte("2020-09-0112:13:14")  // invalid (w/o T)
   190  
   191  	orig1 := ekatime.NewTimestamp(1814, 5, 12, 0, 0, 0)
   192  	orig2 := ekatime.NewTimestamp(2020, 9, 1, 0, 0, 0)
   193  	orig3 := ekatime.NewTimestamp(2020, 9, 1, 23, 59, 59)
   194  	orig4 := ekatime.NewTimestamp(2020, 9, 1, 12, 13, 14)
   195  
   196  	var (
   197  		ts1, ts2, ts3, ts4, ts5 ekatime.Timestamp
   198  	)
   199  
   200  	err1 := ts1.ParseFrom(bd1)
   201  	err2 := ts2.ParseFrom(bd2)
   202  	err3 := ts3.ParseFrom(bd3)
   203  	err4 := ts4.ParseFrom(bd4)
   204  	err5 := ts5.ParseFrom(bd5)
   205  
   206  	require.Equal(t, ts1, orig1)
   207  	require.Equal(t, ts2, orig2)
   208  	require.Equal(t, ts3, orig3)
   209  	require.Equal(t, ts4, orig4)
   210  
   211  	require.NoError(t, err1)
   212  	require.NoError(t, err2)
   213  	require.NoError(t, err3)
   214  	require.NoError(t, err4)
   215  	require.Error(t, err5)
   216  }
   217  
   218  func BenchmarkTimestamp_ParseFrom(b *testing.B) {
   219  	bd0 := []byte("2020-09-01T12:13:14")
   220  	var ts ekatime.Timestamp
   221  	b.ResetTimer()
   222  	b.ReportAllocs()
   223  	for i := 0; i < b.N; i++ {
   224  		_ = ts.ParseFrom(bd0)
   225  	}
   226  }
   227  
   228  func TestTimestamp_MarshalJSON(t *testing.T) {
   229  	var ts = struct {
   230  		TS ekatime.Timestamp `json:"ts"`
   231  	}{
   232  		TS: ekatime.NewTimestamp(2020, 9, 12, 13, 14, 15),
   233  	}
   234  	d, err := json.Marshal(&ts)
   235  
   236  	require.NoError(t, err)
   237  	require.EqualValues(t, string(d), `{"ts":"2020-09-12T13:14:15"}`)
   238  }