Time Object

You can create a gtime.Time object through a standard library time.Time object, Unix timestamp, time string (e.g., 2018-07-18 12:01:00), or custom time string (requires a given format, supports custom formats and standard library formats).

Creating Objects

A gtime.Time object can be created using the gtime.New method, which supports creating objects from time.Time, timestamps, and time strings. Timestamps support time lengths in nanoseconds. For example:

  1. // Create from a time string
  2. gtime.New("2020-10-24 12:00:00")
  3. // Create from a time.Time object
  4. gtime.New(time.Now())
  5. // Create from a timestamp in seconds
  6. gtime.New(1603710586)
  7. // Create from a timestamp in nanoseconds
  8. gtime.New(1603710586660409000)

Additionally, time strings support common types such as:

  1. 2017-12-14 04:51:34 +0805 LMT
  2. 2017-12-14 04:51:34 +0805 LMT
  3. 2006-01-02T15:04:05Z07:00
  4. 2014-01-17T01:19:15+08:00
  5. 2018-02-09T20:46:17.897Z
  6. 2018-02-09 20:46:17.897
  7. 2018-02-09T20:46:17Z
  8. 2018-02-09 20:46:17
  9. 2018/10/31 - 16:38:46
  10. 2018-02-09
  11. 2018.02.09
  12. 01-Nov-2018 11:50:28
  13. 01/Nov/2018 11:50:28
  14. 01.Nov.2018 11:50:28
  15. 01.Nov.2018:11:50:28
  16. Date separators support '-', '/', '.'

Examples

Example 1: Custom Formatting Syntax

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/os/gtime"
  5. )
  6. func main() {
  7. formats := []string{
  8. "Y-m-d H:i:s.u",
  9. "D M d H:i:s T O Y",
  10. "\\T\\i\\m\\e \\i\\s: h:i:s a",
  11. "2006-01-02T15:04:05.000000000Z07:00",
  12. }
  13. t := gtime.Now()
  14. for _, f := range formats {
  15. fmt.Println(t.Format(f))
  16. }
  17. }

In this example, we specified four format styles and converted the current time to these styles for printing. The output is as follows:

  1. 2018-07-22 11:17:13.797
  2. Sun Jul 22 11:17:13 CST +0800 2018
  3. Time is: 11:17:13 am
  4. 2006-01-02CST15:04:05.000000000Z07:00

Noteworthy points in this example:

  1. When letters conflict with formatting characters, you can escape the character with \ to indicate to the parser it’s a regular letter, not a format character. Hence, the third string outputs as: Time is: 11:17:13 am
  2. The Format method accepts custom formatting syntax (e.g., Y-m-d H:i:s), not the standard library syntax (e.g., 2006-01-02 15:04:05), leading to the fourth string being output as-is.

Example 2: Standard Library Formatting Syntax

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/os/gtime"
  5. )
  6. func main() {
  7. formats := []string{
  8. "2006-01-02 15:04:05.000",
  9. "Mon Jan _2 15:04:05 MST 2006",
  10. "Time is: 03:04:05 PM",
  11. "2006-01-02T15:04:05.000000000Z07:00 MST",
  12. }
  13. t := gtime.Now()
  14. for _, f := range formats {
  15. fmt.Println(t.Layout(f))
  16. }
  17. }

In this example, we use four standard library time formatting syntaxes to format the current time and output the result to the terminal. The output is:

  1. 2018-07-22 11:28:13.945
  2. Sun Jul 22 11:28:13 CST 2018
  3. Time is: 11:28:13 AM
  4. 2018-07-22T11:28:13.945153275+08:00 CST

Key points:

  1. Custom formatting syntax and standard library formatting syntax are not conflicting. They use Format and Layout methods respectively and are independent and non-interchangeable.
  2. The standard library’s formatting syntax has its unique complexities.

Example 3: Chained Operations on Time Objects

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/os/gtime"
  5. "time"
  6. )
  7. func main() {
  8. // This time last year, system time
  9. fmt.Println(gtime.Now().AddDate(-1, 0, 0).Format("Y-m-d"))
  10. // This time last year, UTC time
  11. fmt.Println(gtime.Now().AddDate(-1, 0, 0).Format("Y-m-d H:i:s T"))
  12. fmt.Println(gtime.Now().AddDate(-1, 0, 0).UTC().Format("Y-m-d H:i:s T"))
  13. // Midnight on the 1st of next month
  14. fmt.Println(gtime.Now().AddDate(0, 1, 0).Format("Y-m-01 00:00:00"))
  15. // 1 hour ago
  16. fmt.Println(gtime.Now().Add(-time.Hour).Format("Y-m-d H:i:s"))
  17. }

The output is:

  1. 2020-09-19
  2. 2020-09-19 15:51:48 CST
  3. 2020-09-19 07:51:48 UTC
  4. 2021-10-01 00:00:00
  5. 2021-09-19 14:51:48

This example is straightforward and needs no further elaboration.