github.com/wangyougui/gf/v2@v2.6.5/os/gtime/gtime_z_unit_time_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gtime_test
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/wangyougui/gf/v2/internal/json"
    15  	"github.com/wangyougui/gf/v2/os/gtime"
    16  	"github.com/wangyougui/gf/v2/test/gtest"
    17  	"github.com/wangyougui/gf/v2/util/gutil"
    18  )
    19  
    20  func Test_New(t *testing.T) {
    21  	// time.Time
    22  	gtest.C(t, func(t *gtest.T) {
    23  		timeNow := time.Now()
    24  		timeTemp := gtime.New(timeNow)
    25  		t.Assert(timeTemp.Time.UnixNano(), timeNow.UnixNano())
    26  
    27  		timeTemp1 := gtime.New()
    28  		t.Assert(timeTemp1.Time, time.Time{})
    29  	})
    30  	// string
    31  	gtest.C(t, func(t *gtest.T) {
    32  		timeNow := gtime.Now()
    33  		timeTemp := gtime.New(timeNow.String())
    34  		t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), timeNow.Time.Format("2006-01-02 15:04:05"))
    35  	})
    36  	gtest.C(t, func(t *gtest.T) {
    37  		timeNow := gtime.Now()
    38  		timeTemp := gtime.New(timeNow.TimestampMicroStr())
    39  		t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), timeNow.Time.Format("2006-01-02 15:04:05"))
    40  	})
    41  	// int64
    42  	gtest.C(t, func(t *gtest.T) {
    43  		timeNow := gtime.Now()
    44  		timeTemp := gtime.New(timeNow.TimestampMicro())
    45  		t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), timeNow.Time.Format("2006-01-02 15:04:05"))
    46  	})
    47  	// short datetime.
    48  	gtest.C(t, func(t *gtest.T) {
    49  		timeTemp := gtime.New("2021-2-9 08:01:21")
    50  		t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21")
    51  		t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21")
    52  
    53  		timeTemp = gtime.New("2021-02-09 08:01:21", []byte("Y-m-d H:i:s"))
    54  		t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21")
    55  		t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21")
    56  
    57  		timeTemp = gtime.New([]byte("2021-02-09 08:01:21"))
    58  		t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21")
    59  		t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21")
    60  
    61  		timeTemp = gtime.New([]byte("2021-02-09 08:01:21"), "Y-m-d H:i:s")
    62  		t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21")
    63  		t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21")
    64  
    65  		timeTemp = gtime.New([]byte("2021-02-09 08:01:21"), []byte("Y-m-d H:i:s"))
    66  		t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21")
    67  		t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21")
    68  	})
    69  	//
    70  	gtest.C(t, func(t *gtest.T) {
    71  		t.Assert(gtime.New(gtime.Time{}), nil)
    72  		t.Assert(gtime.New(&gtime.Time{}), nil)
    73  	})
    74  
    75  	// unconventional
    76  	gtest.C(t, func(t *gtest.T) {
    77  
    78  		var testUnconventionalDates = []string{
    79  			"2006-01.02",
    80  			"2006.01-02",
    81  		}
    82  
    83  		for _, item := range testUnconventionalDates {
    84  			timeTemp := gtime.New(item)
    85  			t.Assert(timeTemp.TimestampMilli(), 0)
    86  			t.Assert(timeTemp.TimestampMilliStr(), "")
    87  			t.Assert(timeTemp.String(), "")
    88  		}
    89  	})
    90  }
    91  
    92  func Test_Nil(t *testing.T) {
    93  	gtest.C(t, func(t *gtest.T) {
    94  		var t1 *gtime.Time
    95  		t.Assert(t1.String(), "")
    96  	})
    97  	gtest.C(t, func(t *gtest.T) {
    98  		var t1 gtime.Time
    99  		t.Assert(t1.String(), "")
   100  	})
   101  }
   102  
   103  func Test_NewFromStr(t *testing.T) {
   104  	gtest.C(t, func(t *gtest.T) {
   105  		timeTemp := gtime.NewFromStr("2006-01-02 15:04:05")
   106  		t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05")
   107  
   108  		timeTemp1 := gtime.NewFromStr("2006.0102")
   109  		if timeTemp1 != nil {
   110  			t.Error("test fail")
   111  		}
   112  	})
   113  }
   114  
   115  func Test_String(t *testing.T) {
   116  	gtest.C(t, func(t *gtest.T) {
   117  		t1 := gtime.NewFromStr("2006-01-02 15:04:05")
   118  		t.Assert(t1.String(), "2006-01-02 15:04:05")
   119  		t.Assert(fmt.Sprintf("%s", t1), "2006-01-02 15:04:05")
   120  
   121  		t2 := *t1
   122  		t.Assert(t2.String(), "2006-01-02 15:04:05")
   123  		t.Assert(fmt.Sprintf("{%s}", t2.String()), "{2006-01-02 15:04:05}")
   124  	})
   125  }
   126  
   127  func Test_NewFromStrFormat(t *testing.T) {
   128  	gtest.C(t, func(t *gtest.T) {
   129  		timeTemp := gtime.NewFromStrFormat("2006-01-02 15:04:05", "Y-m-d H:i:s")
   130  		t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05")
   131  
   132  		timeTemp1 := gtime.NewFromStrFormat("2006-01-02 15:04:05", "aabbcc")
   133  		if timeTemp1 != nil {
   134  			t.Error("test fail")
   135  		}
   136  	})
   137  
   138  	gtest.C(t, func(t *gtest.T) {
   139  		t1 := gtime.NewFromStrFormat("2019/2/1", "Y/n/j")
   140  		t.Assert(t1.Format("Y-m-d"), "2019-02-01")
   141  
   142  		t2 := gtime.NewFromStrFormat("2019/10/12", "Y/n/j")
   143  		t.Assert(t2.Format("Y-m-d"), "2019-10-12")
   144  	})
   145  }
   146  
   147  func Test_NewFromStrLayout(t *testing.T) {
   148  	gtest.C(t, func(t *gtest.T) {
   149  		timeTemp := gtime.NewFromStrLayout("2006-01-02 15:04:05", "2006-01-02 15:04:05")
   150  		t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05")
   151  
   152  		timeTemp1 := gtime.NewFromStrLayout("2006-01-02 15:04:05", "aabbcc")
   153  		if timeTemp1 != nil {
   154  			t.Error("test fail")
   155  		}
   156  	})
   157  }
   158  
   159  func Test_NewFromTimeStamp(t *testing.T) {
   160  	gtest.C(t, func(t *gtest.T) {
   161  		timeTemp := gtime.NewFromTimeStamp(1554459846000)
   162  		t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2019-04-05 18:24:06")
   163  		timeTemp1 := gtime.NewFromTimeStamp(0)
   164  		t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "0001-01-01 00:00:00")
   165  		timeTemp2 := gtime.NewFromTimeStamp(155445984)
   166  		t.Assert(timeTemp2.Format("Y-m-d H:i:s"), "1974-12-05 11:26:24")
   167  	})
   168  }
   169  
   170  func Test_Time_Second(t *testing.T) {
   171  	gtest.C(t, func(t *gtest.T) {
   172  		timeTemp := gtime.Now()
   173  		t.Assert(timeTemp.Second(), timeTemp.Time.Second())
   174  	})
   175  }
   176  
   177  func Test_Time_IsZero(t *testing.T) {
   178  	gtest.C(t, func(t *gtest.T) {
   179  		var ti *gtime.Time = nil
   180  		t.Assert(ti.IsZero(), true)
   181  	})
   182  }
   183  
   184  func Test_Time_AddStr(t *testing.T) {
   185  	gtest.C(t, func(t *gtest.T) {
   186  		gt := gtime.New("2018-08-08 08:08:08")
   187  		gt1, err := gt.AddStr("10T")
   188  		t.Assert(gt1, nil)
   189  		t.AssertNE(err, nil)
   190  	})
   191  }
   192  
   193  func Test_Time_Equal(t *testing.T) {
   194  	gtest.C(t, func(t *gtest.T) {
   195  		var t1 *gtime.Time = nil
   196  		var t2 = gtime.New()
   197  		t.Assert(t1.Equal(t2), false)
   198  		t.Assert(t1.Equal(t1), true)
   199  		t.Assert(t2.Equal(t1), false)
   200  	})
   201  }
   202  
   203  func Test_Time_After(t *testing.T) {
   204  	gtest.C(t, func(t *gtest.T) {
   205  		var t1 *gtime.Time = nil
   206  		var t2 = gtime.New()
   207  		t.Assert(t1.After(t2), false)
   208  		t.Assert(t2.After(t1), true)
   209  	})
   210  }
   211  
   212  func Test_Time_Sub(t *testing.T) {
   213  	gtest.C(t, func(t *gtest.T) {
   214  		var t1 *gtime.Time = nil
   215  		var t2 = gtime.New()
   216  		t.Assert(t1.Sub(t2), time.Duration(0))
   217  		t.Assert(t2.Sub(t1), time.Duration(0))
   218  	})
   219  }
   220  
   221  func Test_Time_Nanosecond(t *testing.T) {
   222  	gtest.C(t, func(t *gtest.T) {
   223  		timeTemp := gtime.Now()
   224  		t.Assert(timeTemp.Nanosecond(), timeTemp.Time.Nanosecond())
   225  	})
   226  }
   227  
   228  func Test_Time_Microsecond(t *testing.T) {
   229  	gtest.C(t, func(t *gtest.T) {
   230  		timeTemp := gtime.Now()
   231  		t.Assert(timeTemp.Microsecond(), timeTemp.Time.Nanosecond()/1e3)
   232  	})
   233  }
   234  
   235  func Test_Time_Millisecond(t *testing.T) {
   236  	gtest.C(t, func(t *gtest.T) {
   237  		timeTemp := gtime.Now()
   238  		t.Assert(timeTemp.Millisecond(), timeTemp.Time.Nanosecond()/1e6)
   239  	})
   240  }
   241  
   242  func Test_Time_String(t *testing.T) {
   243  	gtest.C(t, func(t *gtest.T) {
   244  		timeTemp := gtime.Now()
   245  		t.Assert(timeTemp.String(), timeTemp.Time.Format("2006-01-02 15:04:05"))
   246  	})
   247  }
   248  
   249  func Test_Time_ISO8601(t *testing.T) {
   250  	gtest.C(t, func(t *gtest.T) {
   251  		now := gtime.Now()
   252  		t.Assert(now.ISO8601(), now.Format("c"))
   253  	})
   254  }
   255  
   256  func Test_Time_RFC822(t *testing.T) {
   257  	gtest.C(t, func(t *gtest.T) {
   258  		now := gtime.Now()
   259  		t.Assert(now.RFC822(), now.Format("r"))
   260  	})
   261  }
   262  
   263  func Test_Clone(t *testing.T) {
   264  	gtest.C(t, func(t *gtest.T) {
   265  		timeTemp := gtime.Now()
   266  		timeTemp1 := timeTemp.Clone()
   267  		t.Assert(timeTemp.Time.Unix(), timeTemp1.Time.Unix())
   268  	})
   269  }
   270  
   271  func Test_ToTime(t *testing.T) {
   272  	gtest.C(t, func(t *gtest.T) {
   273  		timeTemp := gtime.Now()
   274  		timeTemp1 := timeTemp.Time
   275  		t.Assert(timeTemp.Time.UnixNano(), timeTemp1.UnixNano())
   276  	})
   277  }
   278  
   279  func Test_Add(t *testing.T) {
   280  	gtest.C(t, func(t *gtest.T) {
   281  		timeTemp := gtime.NewFromStr("2006-01-02 15:04:05")
   282  		timeTemp = timeTemp.Add(time.Second)
   283  		t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:06")
   284  	})
   285  }
   286  
   287  func Test_ToZone(t *testing.T) {
   288  	gtest.C(t, func(t *gtest.T) {
   289  		timeTemp := gtime.Now()
   290  		timeTemp, _ = timeTemp.ToZone("America/Los_Angeles")
   291  		t.Assert(timeTemp.Time.Location().String(), "America/Los_Angeles")
   292  
   293  		loc, err := time.LoadLocation("Asia/Shanghai")
   294  		if err != nil {
   295  			t.Error("test fail")
   296  		}
   297  		timeTemp = timeTemp.ToLocation(loc)
   298  		t.Assert(timeTemp.Time.Location().String(), "Asia/Shanghai")
   299  
   300  		timeTemp1, _ := timeTemp.ToZone("errZone")
   301  		if timeTemp1 != nil {
   302  			t.Error("test fail")
   303  		}
   304  	})
   305  }
   306  
   307  func Test_AddDate(t *testing.T) {
   308  	gtest.C(t, func(t *gtest.T) {
   309  		timeTemp := gtime.NewFromStr("2006-01-02 15:04:05")
   310  		timeTemp = timeTemp.AddDate(1, 2, 3)
   311  		t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2007-03-05 15:04:05")
   312  	})
   313  }
   314  
   315  func Test_UTC(t *testing.T) {
   316  	gtest.C(t, func(t *gtest.T) {
   317  		timeTemp := gtime.Now()
   318  		timeTemp1 := timeTemp.Time
   319  		timeTemp.UTC()
   320  		t.Assert(timeTemp.UnixNano(), timeTemp1.UTC().UnixNano())
   321  	})
   322  }
   323  
   324  func Test_Local(t *testing.T) {
   325  	gtest.C(t, func(t *gtest.T) {
   326  		timeTemp := gtime.Now()
   327  		timeTemp1 := timeTemp.Time
   328  		timeTemp.Local()
   329  		t.Assert(timeTemp.UnixNano(), timeTemp1.Local().UnixNano())
   330  	})
   331  }
   332  
   333  func Test_Round(t *testing.T) {
   334  	gtest.C(t, func(t *gtest.T) {
   335  		timeTemp := gtime.Now()
   336  		timeTemp1 := timeTemp.Time
   337  		timeTemp = timeTemp.Round(time.Hour)
   338  		t.Assert(timeTemp.UnixNano(), timeTemp1.Round(time.Hour).UnixNano())
   339  	})
   340  }
   341  
   342  func Test_Truncate(t *testing.T) {
   343  	gtest.C(t, func(t *gtest.T) {
   344  		timeTemp := gtime.Now()
   345  		timeTemp1 := timeTemp.Time
   346  		timeTemp = timeTemp.Truncate(time.Hour)
   347  		t.Assert(timeTemp.UnixNano(), timeTemp1.Truncate(time.Hour).UnixNano())
   348  	})
   349  }
   350  
   351  func Test_StartOfMinute(t *testing.T) {
   352  	gtest.C(t, func(t *gtest.T) {
   353  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   354  		timeTemp1 := timeTemp.StartOfMinute()
   355  		t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 18:24:00")
   356  	})
   357  }
   358  
   359  func Test_EndOfMinute(t *testing.T) {
   360  	gtest.C(t, func(t *gtest.T) {
   361  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   362  		timeTemp1 := timeTemp.EndOfMinute()
   363  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 18:24:59.000")
   364  	})
   365  	gtest.C(t, func(t *gtest.T) {
   366  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   367  		timeTemp1 := timeTemp.EndOfMinute(true)
   368  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 18:24:59.999")
   369  	})
   370  }
   371  
   372  func Test_StartOfHour(t *testing.T) {
   373  	gtest.C(t, func(t *gtest.T) {
   374  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   375  		timeTemp1 := timeTemp.StartOfHour()
   376  		t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 18:00:00")
   377  	})
   378  }
   379  
   380  func Test_EndOfHour(t *testing.T) {
   381  	gtest.C(t, func(t *gtest.T) {
   382  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   383  		timeTemp1 := timeTemp.EndOfHour()
   384  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 18:59:59.000")
   385  	})
   386  	gtest.C(t, func(t *gtest.T) {
   387  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   388  		timeTemp1 := timeTemp.EndOfHour(true)
   389  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 18:59:59.999")
   390  	})
   391  }
   392  
   393  func Test_StartOfDay(t *testing.T) {
   394  	gtest.C(t, func(t *gtest.T) {
   395  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   396  		timeTemp1 := timeTemp.StartOfDay()
   397  		t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 00:00:00")
   398  	})
   399  }
   400  
   401  func Test_EndOfDay(t *testing.T) {
   402  	gtest.C(t, func(t *gtest.T) {
   403  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   404  		timeTemp1 := timeTemp.EndOfDay()
   405  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.000")
   406  	})
   407  	gtest.C(t, func(t *gtest.T) {
   408  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   409  		timeTemp1 := timeTemp.EndOfDay(true)
   410  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.999")
   411  	})
   412  }
   413  
   414  func Test_StartOfWeek(t *testing.T) {
   415  	gtest.C(t, func(t *gtest.T) {
   416  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   417  		timeTemp1 := timeTemp.StartOfWeek()
   418  		t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-06 00:00:00")
   419  	})
   420  }
   421  
   422  func Test_EndOfWeek(t *testing.T) {
   423  	gtest.C(t, func(t *gtest.T) {
   424  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   425  		timeTemp1 := timeTemp.EndOfWeek()
   426  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.000")
   427  	})
   428  	gtest.C(t, func(t *gtest.T) {
   429  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   430  		timeTemp1 := timeTemp.EndOfWeek(true)
   431  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.999")
   432  	})
   433  }
   434  
   435  func Test_StartOfMonth(t *testing.T) {
   436  	gtest.C(t, func(t *gtest.T) {
   437  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   438  		timeTemp1 := timeTemp.StartOfMonth()
   439  		t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-01 00:00:00")
   440  	})
   441  }
   442  
   443  func Test_EndOfMonth(t *testing.T) {
   444  	gtest.C(t, func(t *gtest.T) {
   445  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   446  		timeTemp1 := timeTemp.EndOfMonth()
   447  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000")
   448  	})
   449  	gtest.C(t, func(t *gtest.T) {
   450  		timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
   451  		timeTemp1 := timeTemp.EndOfMonth(true)
   452  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999")
   453  	})
   454  }
   455  
   456  func Test_StartOfQuarter(t *testing.T) {
   457  	gtest.C(t, func(t *gtest.T) {
   458  		timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
   459  		timeTemp1 := timeTemp.StartOfQuarter()
   460  		t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-10-01 00:00:00")
   461  	})
   462  }
   463  
   464  func Test_EndOfQuarter(t *testing.T) {
   465  	gtest.C(t, func(t *gtest.T) {
   466  		timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
   467  		timeTemp1 := timeTemp.EndOfQuarter()
   468  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000")
   469  	})
   470  	gtest.C(t, func(t *gtest.T) {
   471  		timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
   472  		timeTemp1 := timeTemp.EndOfQuarter(true)
   473  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999")
   474  	})
   475  }
   476  
   477  func Test_StartOfHalf(t *testing.T) {
   478  	gtest.C(t, func(t *gtest.T) {
   479  		timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
   480  		timeTemp1 := timeTemp.StartOfHalf()
   481  		t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-07-01 00:00:00")
   482  	})
   483  }
   484  
   485  func Test_EndOfHalf(t *testing.T) {
   486  	gtest.C(t, func(t *gtest.T) {
   487  		timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
   488  		timeTemp1 := timeTemp.EndOfHalf()
   489  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000")
   490  	})
   491  	gtest.C(t, func(t *gtest.T) {
   492  		timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
   493  		timeTemp1 := timeTemp.EndOfHalf(true)
   494  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999")
   495  	})
   496  }
   497  
   498  func Test_StartOfYear(t *testing.T) {
   499  	gtest.C(t, func(t *gtest.T) {
   500  		timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
   501  		timeTemp1 := timeTemp.StartOfYear()
   502  		t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-01-01 00:00:00")
   503  	})
   504  }
   505  
   506  func Test_EndOfYear(t *testing.T) {
   507  	gtest.C(t, func(t *gtest.T) {
   508  		timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
   509  		timeTemp1 := timeTemp.EndOfYear()
   510  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000")
   511  	})
   512  	gtest.C(t, func(t *gtest.T) {
   513  		timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
   514  		timeTemp1 := timeTemp.EndOfYear(true)
   515  		t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999")
   516  	})
   517  }
   518  
   519  func Test_OnlyTime(t *testing.T) {
   520  	gtest.C(t, func(t *gtest.T) {
   521  		obj := gtime.NewFromStr("18:24:06")
   522  		t.Assert(obj.String(), "18:24:06")
   523  	})
   524  }
   525  
   526  func Test_DeepCopy(t *testing.T) {
   527  	type User struct {
   528  		Id          int
   529  		CreatedTime *gtime.Time
   530  	}
   531  	gtest.C(t, func(t *gtest.T) {
   532  		u1 := &User{
   533  			Id:          1,
   534  			CreatedTime: gtime.New("2022-03-08T03:01:14+08:00"),
   535  		}
   536  		u2 := gutil.Copy(u1).(*User)
   537  		t.Assert(u1, u2)
   538  	})
   539  	// nil attribute.
   540  	gtest.C(t, func(t *gtest.T) {
   541  		u1 := &User{}
   542  		u2 := gutil.Copy(u1).(*User)
   543  		t.Assert(u1, u2)
   544  	})
   545  	gtest.C(t, func(t *gtest.T) {
   546  		var t1 *gtime.Time = nil
   547  		t.Assert(t1.DeepCopy(), nil)
   548  	})
   549  }
   550  
   551  func Test_UnmarshalJSON(t *testing.T) {
   552  	gtest.C(t, func(t *gtest.T) {
   553  		var t1 gtime.Time
   554  		t.AssertNE(json.Unmarshal([]byte("{}"), &t1), nil)
   555  	})
   556  }