github.com/gogf/gf/v2@v2.7.4/os/gtime/gtime_z_example_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/gogf/gf.
     6  
     7  package gtime_test
     8  
     9  import (
    10  	"encoding/json"
    11  	"fmt"
    12  	"reflect"
    13  	"time"
    14  
    15  	"github.com/gogf/gf/v2/os/gtime"
    16  )
    17  
    18  func ExampleNew_Basic() {
    19  	curTime := "2018-08-08 08:08:08"
    20  	timer, _ := time.Parse("2006-01-02 15:04:05", curTime)
    21  	t1 := gtime.New(&timer)
    22  	t2 := gtime.New(curTime)
    23  	t3 := gtime.New(curTime, "Y-m-d H:i:s")
    24  	t4 := gtime.New(curTime)
    25  	t5 := gtime.New(1533686888)
    26  	t6 := gtime.New("08:08:08")
    27  
    28  	fmt.Println(t1)
    29  	fmt.Println(t2)
    30  	fmt.Println(t3)
    31  	fmt.Println(t4)
    32  	fmt.Println(t5)
    33  	fmt.Println(t6)
    34  
    35  	// Output:
    36  	// 2018-08-08 08:08:08
    37  	// 2018-08-08 08:08:08
    38  	// 2018-08-08 08:08:08
    39  	// 2018-08-08 08:08:08
    40  	// 2018-08-08 08:08:08
    41  	// 08:08:08
    42  }
    43  
    44  func ExampleNew_WithFormat() {
    45  	fmt.Println(gtime.New("20220629133225", "YmdHis").Format("Y-m-d H:i:s"))
    46  
    47  	// Output:
    48  	// 2022-06-29 13:32:25
    49  }
    50  
    51  // Now creates and returns a time object of now.
    52  func ExampleNow() {
    53  	t := gtime.Now()
    54  	fmt.Println(t)
    55  
    56  	// May Output:
    57  	// 2021-11-06 13:41:08
    58  }
    59  
    60  // NewFromTime creates and returns a Time object with given time.Time object.
    61  func ExampleNewFromTime() {
    62  	timer, _ := time.Parse("2006-01-02 15:04:05", "2018-08-08 08:08:08")
    63  	nTime := gtime.NewFromTime(timer)
    64  
    65  	fmt.Println(nTime)
    66  
    67  	// Output:
    68  	// 2018-08-08 08:08:08
    69  }
    70  
    71  // NewFromStr creates and returns a Time object with given string.
    72  // Note that it returns nil if there's error occurs.
    73  func ExampleNewFromStr() {
    74  	t1 := gtime.NewFromStr("2018-08-08 08:08:08")
    75  	t2 := gtime.NewFromStr("08:08:08")
    76  
    77  	fmt.Println(t1)
    78  	fmt.Println(t2)
    79  
    80  	// Output:
    81  	// 2018-08-08 08:08:08
    82  	// 08:08:08
    83  }
    84  
    85  // NewFromStrFormat creates and returns a Time object with given string and
    86  // custom format like: Y-m-d H:i:s.
    87  // Note that it returns nil if there's error occurs.
    88  func ExampleNewFromStrFormat() {
    89  	t := gtime.NewFromStrFormat("2018-08-08 08:08:08", "Y-m-d H:i:s")
    90  	fmt.Println(t)
    91  
    92  	// Output:
    93  	// 2018-08-08 08:08:08
    94  }
    95  
    96  // NewFromStrLayout creates and returns a Time object with given string and
    97  // stdlib layout like: 2006-01-02 15:04:05.
    98  // Note that it returns nil if there's error occurs.
    99  func ExampleNewFromStrLayout() {
   100  	t := gtime.NewFromStrLayout("2018-08-08 08:08:08", "2006-01-02 15:04:05")
   101  	fmt.Println(t)
   102  
   103  	// Output:
   104  	// 2018-08-08 08:08:08
   105  }
   106  
   107  // NewFromTimeStamp creates and returns a Time object with given timestamp,
   108  // which can be in seconds to nanoseconds.
   109  // Eg: 1600443866 and 1600443866199266000 are both considered as valid timestamp number.
   110  func ExampleNewFromTimeStamp() {
   111  	t1 := gtime.NewFromTimeStamp(1533686888)
   112  	t2 := gtime.NewFromTimeStamp(1533686888000)
   113  
   114  	fmt.Println(t1.String() == t2.String())
   115  	fmt.Println(t1)
   116  
   117  	// Output:
   118  	// true
   119  	// 2018-08-08 08:08:08
   120  }
   121  
   122  // Timestamp returns the timestamp in seconds.
   123  func ExampleTime_Timestamp() {
   124  	t := gtime.Timestamp()
   125  
   126  	fmt.Println(t)
   127  
   128  	// May output:
   129  	// 1533686888
   130  }
   131  
   132  // Timestamp returns the timestamp in milliseconds.
   133  func ExampleTime_TimestampMilli() {
   134  	t := gtime.TimestampMilli()
   135  
   136  	fmt.Println(t)
   137  
   138  	// May output:
   139  	// 1533686888000
   140  }
   141  
   142  // Timestamp returns the timestamp in microseconds.
   143  func ExampleTime_TimestampMicro() {
   144  	t := gtime.TimestampMicro()
   145  
   146  	fmt.Println(t)
   147  
   148  	// May output:
   149  	// 1533686888000000
   150  }
   151  
   152  // Timestamp returns the timestamp in nanoseconds.
   153  func ExampleTime_TimestampNano() {
   154  	t := gtime.TimestampNano()
   155  
   156  	fmt.Println(t)
   157  
   158  	// May output:
   159  	// 1533686888000000
   160  }
   161  
   162  // TimestampStr is a convenience method which retrieves and returns
   163  // the timestamp in seconds as string.
   164  func ExampleTime_TimestampStr() {
   165  	t := gtime.TimestampStr()
   166  
   167  	fmt.Println(reflect.TypeOf(t))
   168  
   169  	// Output:
   170  	// string
   171  }
   172  
   173  // Month returns the month of the year specified by t.
   174  func ExampleTime_Month() {
   175  	gt := gtime.New("2018-08-08 08:08:08")
   176  	t1 := gt.Month()
   177  
   178  	fmt.Println(t1)
   179  
   180  	// Output:
   181  	// 8
   182  }
   183  
   184  // Second returns the second offset within the minute specified by t,
   185  // in the range [0, 59].
   186  func ExampleTime_Second() {
   187  	gt := gtime.New("2018-08-08 08:08:08")
   188  	t1 := gt.Second()
   189  
   190  	fmt.Println(t1)
   191  
   192  	// Output:
   193  	// 8
   194  }
   195  
   196  // String returns current time object as string.
   197  func ExampleTime_String() {
   198  	gt1 := gtime.New("2018-08-08 08:08:08")
   199  	t1 := gt1.String()
   200  	gt2 := gtime.New("08:08:08")
   201  
   202  	fmt.Println(t1)
   203  	fmt.Println(reflect.TypeOf(t1))
   204  	fmt.Println(gt2)
   205  
   206  	// Output:
   207  	// 2018-08-08 08:08:08
   208  	// string
   209  	// 08:08:08
   210  }
   211  
   212  // IsZero reports whether t represents the zero time instant,
   213  // January 1, year 1, 00:00:00 UTC.
   214  func ExampleTime_IsZero() {
   215  	gt1 := gtime.New("2018-08-08 08:08:08")
   216  	gt2 := gtime.New("00:00:00")
   217  	timer, _ := time.Parse("15:04:05", "00:00:00")
   218  	gt3 := gtime.NewFromTime(timer)
   219  
   220  	fmt.Println(gt1.IsZero())
   221  	fmt.Println(gt2.IsZero())
   222  	fmt.Println(timer.IsZero()) // stdlib is also false
   223  	fmt.Println(gt3.IsZero())
   224  
   225  	// Output:
   226  	// false
   227  	// false
   228  	// false
   229  	// false
   230  }
   231  
   232  // Add adds the duration to current time.
   233  func ExampleTime_Add() {
   234  	gt := gtime.New("2018-08-08 08:08:08")
   235  	gt1 := gt.Add(time.Duration(10) * time.Second)
   236  
   237  	fmt.Println(gt1)
   238  
   239  	// Output:
   240  	// 2018-08-08 08:08:18
   241  }
   242  
   243  // AddStr parses the given duration as string and adds it to current time.
   244  // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
   245  func ExampleTime_AddStr() {
   246  	gt := gtime.New("2018-08-08 08:08:08")
   247  	gt1, _ := gt.AddStr("10s")
   248  
   249  	fmt.Println(gt1)
   250  
   251  	// Output:
   252  	// 2018-08-08 08:08:18
   253  }
   254  
   255  // AddDate adds year, month and day to the time.
   256  func ExampleTime_AddDate() {
   257  	var (
   258  		year  = 1
   259  		month = 2
   260  		day   = 3
   261  	)
   262  	gt := gtime.New("2018-08-08 08:08:08")
   263  	gt = gt.AddDate(year, month, day)
   264  
   265  	fmt.Println(gt)
   266  
   267  	// Output:
   268  	// 2019-10-11 08:08:08
   269  }
   270  
   271  // Round returns the result of rounding t to the nearest multiple of d (since the zero time).
   272  // The rounding behavior for halfway values is to round up.
   273  // If d <= 0, Round returns t stripped of any monotonic clock reading but otherwise unchanged.
   274  //
   275  // Round operates on the time as an absolute duration since the
   276  // zero time; it does not operate on the presentation form of the
   277  // time. Thus, Round(Hour) may return a time with a non-zero
   278  // minute, depending on the time's Location.
   279  func ExampleTime_Round() {
   280  	gt := gtime.New("2018-08-08 08:08:08")
   281  	t := gt.Round(time.Duration(10) * time.Second)
   282  
   283  	fmt.Println(t)
   284  
   285  	// Output:
   286  	// 2018-08-08 08:08:10
   287  }
   288  
   289  // Truncate returns the result of rounding t down to a multiple of d (since the zero time).
   290  // If d <= 0, Truncate returns t stripped of any monotonic clock reading but otherwise unchanged.
   291  //
   292  // Truncate operates on the time as an absolute duration since the
   293  // zero time; it does not operate on the presentation form of the
   294  // time. Thus, Truncate(Hour) may return a time with a non-zero
   295  // minute, depending on the time's Location.
   296  func ExampleTime_Truncate() {
   297  	gt := gtime.New("2018-08-08 08:08:08")
   298  	t := gt.Truncate(time.Duration(10) * time.Second)
   299  
   300  	fmt.Println(t)
   301  
   302  	// Output:
   303  	// 2018-08-08 08:08:00
   304  }
   305  
   306  // Equal reports whether t and u represent the same time instant.
   307  // Two times can be equal even if they are in different locations.
   308  // For example, 6:00 +0200 CEST and 4:00 UTC are Equal.
   309  // See the documentation on the Time type for the pitfalls of using == with
   310  // Time values; most code should use Equal instead.
   311  func ExampleTime_Equal() {
   312  	gt1 := gtime.New("2018-08-08 08:08:08")
   313  	gt2 := gtime.New("2018-08-08 08:08:08")
   314  
   315  	fmt.Println(gt1.Equal(gt2))
   316  
   317  	// Output:
   318  	// true
   319  }
   320  
   321  // Before reports whether the time instant t is before u.
   322  func ExampleTime_Before() {
   323  	gt1 := gtime.New("2018-08-07")
   324  	gt2 := gtime.New("2018-08-08")
   325  
   326  	fmt.Println(gt1.Before(gt2))
   327  
   328  	// Output:
   329  	// true
   330  }
   331  
   332  // After reports whether the time instant t is after u.
   333  func ExampleTime_After() {
   334  	gt1 := gtime.New("2018-08-07")
   335  	gt2 := gtime.New("2018-08-08")
   336  
   337  	fmt.Println(gt1.After(gt2))
   338  
   339  	// Output:
   340  	// false
   341  }
   342  
   343  // Sub returns the duration t-u. If the result exceeds the maximum (or minimum)
   344  // value that can be stored in a Duration, the maximum (or minimum) duration
   345  // will be returned.
   346  // To compute t-d for a duration d, use t.Add(-d).
   347  func ExampleTime_Sub() {
   348  	gt1 := gtime.New("2018-08-08 08:08:08")
   349  	gt2 := gtime.New("2018-08-08 08:08:10")
   350  
   351  	fmt.Println(gt2.Sub(gt1))
   352  
   353  	// Output:
   354  	// 2s
   355  }
   356  
   357  // StartOfMinute clones and returns a new time of which the seconds is set to 0.
   358  func ExampleTime_StartOfMinute() {
   359  	gt1 := gtime.New("2018-08-08 08:08:08")
   360  
   361  	fmt.Println(gt1.StartOfMinute())
   362  
   363  	// Output:
   364  	// 2018-08-08 08:08:00
   365  }
   366  
   367  func ExampleTime_StartOfHour() {
   368  	gt1 := gtime.New("2018-08-08 08:08:08")
   369  
   370  	fmt.Println(gt1.StartOfHour())
   371  
   372  	// Output:
   373  	// 2018-08-08 08:00:00
   374  }
   375  
   376  func ExampleTime_StartOfDay() {
   377  	gt1 := gtime.New("2018-08-08 08:08:08")
   378  
   379  	fmt.Println(gt1.StartOfDay())
   380  
   381  	// Output:
   382  	// 2018-08-08 00:00:00
   383  }
   384  
   385  func ExampleTime_StartOfWeek() {
   386  	gt1 := gtime.New("2018-08-08 08:08:08")
   387  
   388  	fmt.Println(gt1.StartOfWeek())
   389  
   390  	// Output:
   391  	// 2018-08-05 00:00:00
   392  }
   393  
   394  func ExampleTime_StartOfQuarter() {
   395  	gt1 := gtime.New("2018-08-08 08:08:08")
   396  
   397  	fmt.Println(gt1.StartOfQuarter())
   398  
   399  	// Output:
   400  	// 2018-07-01 00:00:00
   401  }
   402  
   403  func ExampleTime_StartOfHalf() {
   404  	gt1 := gtime.New("2018-08-08 08:08:08")
   405  
   406  	fmt.Println(gt1.StartOfHalf())
   407  
   408  	// Output:
   409  	// 2018-07-01 00:00:00
   410  }
   411  
   412  func ExampleTime_StartOfYear() {
   413  	gt1 := gtime.New("2018-08-08 08:08:08")
   414  
   415  	fmt.Println(gt1.StartOfYear())
   416  
   417  	// Output:
   418  	// 2018-01-01 00:00:00
   419  }
   420  
   421  func ExampleTime_EndOfMinute() {
   422  	gt1 := gtime.New("2018-08-08 08:08:08")
   423  
   424  	fmt.Println(gt1.EndOfMinute())
   425  
   426  	// Output:
   427  	// 2018-08-08 08:08:59
   428  }
   429  
   430  func ExampleTime_EndOfHour() {
   431  	gt1 := gtime.New("2018-08-08 08:08:08")
   432  
   433  	fmt.Println(gt1.EndOfHour())
   434  
   435  	// Output:
   436  	// 2018-08-08 08:59:59
   437  }
   438  
   439  func ExampleTime_EndOfDay() {
   440  	gt1 := gtime.New("2018-08-08 08:08:08")
   441  
   442  	fmt.Println(gt1.EndOfDay())
   443  
   444  	// Output:
   445  	// 2018-08-08 23:59:59
   446  }
   447  
   448  func ExampleTime_EndOfWeek() {
   449  	gt1 := gtime.New("2018-08-08 08:08:08")
   450  
   451  	fmt.Println(gt1.EndOfWeek())
   452  
   453  	// Output:
   454  	// 2018-08-11 23:59:59
   455  }
   456  
   457  func ExampleTime_EndOfMonth() {
   458  	gt1 := gtime.New("2018-08-08 08:08:08")
   459  
   460  	fmt.Println(gt1.EndOfMonth())
   461  
   462  	// Output:
   463  	// 2018-08-31 23:59:59
   464  }
   465  
   466  func ExampleTime_EndOfQuarter() {
   467  	gt1 := gtime.New("2018-08-08 08:08:08")
   468  
   469  	fmt.Println(gt1.EndOfQuarter())
   470  
   471  	// Output:
   472  	// 2018-09-30 23:59:59
   473  }
   474  
   475  func ExampleTime_EndOfHalf() {
   476  	gt1 := gtime.New("2018-08-08 08:08:08")
   477  
   478  	fmt.Println(gt1.EndOfHalf())
   479  
   480  	// Output:
   481  	// 2018-12-31 23:59:59
   482  }
   483  
   484  func ExampleTime_EndOfYear() {
   485  	gt1 := gtime.New("2018-08-08 08:08:08")
   486  
   487  	fmt.Println(gt1.EndOfYear())
   488  
   489  	// Output:
   490  	// 2018-12-31 23:59:59
   491  }
   492  
   493  func ExampleTime_MarshalJSON() {
   494  	type Person struct {
   495  		Name     string      `json:"name"`
   496  		Birthday *gtime.Time `json:"birthday"`
   497  	}
   498  	p := new(Person)
   499  	p.Name = "goframe"
   500  	p.Birthday = gtime.New("2018-08-08 08:08:08")
   501  	j, _ := json.Marshal(p)
   502  	fmt.Println(string(j))
   503  
   504  	// Output:
   505  	// {"name":"goframe","birthday":"2018-08-08 08:08:08"}
   506  }
   507  
   508  func ExampleTime_UnmarshalJSON() {
   509  	type Person struct {
   510  		Name     string      `json:"name"`
   511  		Birthday *gtime.Time `json:"birthday"`
   512  	}
   513  	p := new(Person)
   514  	src := `{"name":"goframe","birthday":"2018-08-08 08:08:08"}`
   515  	json.Unmarshal([]byte(src), p)
   516  
   517  	fmt.Println(p)
   518  
   519  	// Output
   520  	// &{goframe 2018-08-08 08:08:08}
   521  }