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:
// Create from a time string
gtime.New("2020-10-24 12:00:00")
// Create from a time.Time object
gtime.New(time.Now())
// Create from a timestamp in seconds
gtime.New(1603710586)
// Create from a timestamp in nanoseconds
gtime.New(1603710586660409000)
Additionally, time strings support common types such as:
2017-12-14 04:51:34 +0805 LMT
2017-12-14 04:51:34 +0805 LMT
2006-01-02T15:04:05Z07:00
2014-01-17T01:19:15+08:00
2018-02-09T20:46:17.897Z
2018-02-09 20:46:17.897
2018-02-09T20:46:17Z
2018-02-09 20:46:17
2018/10/31 - 16:38:46
2018-02-09
2018.02.09
01-Nov-2018 11:50:28
01/Nov/2018 11:50:28
01.Nov.2018 11:50:28
01.Nov.2018:11:50:28
Date separators support '-', '/', '.'
Examples
Example 1: Custom Formatting Syntax
package main
import (
"fmt"
"github.com/gogf/gf/v2/os/gtime"
)
func main() {
formats := []string{
"Y-m-d H:i:s.u",
"D M d H:i:s T O Y",
"\\T\\i\\m\\e \\i\\s: h:i:s a",
"2006-01-02T15:04:05.000000000Z07:00",
}
t := gtime.Now()
for _, f := range formats {
fmt.Println(t.Format(f))
}
}
In this example, we specified four format
styles and converted the current time to these styles for printing. The output is as follows:
2018-07-22 11:17:13.797
Sun Jul 22 11:17:13 CST +0800 2018
Time is: 11:17:13 am
2006-01-02CST15:04:05.000000000Z07:00
Noteworthy points in this example:
- 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
- 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
package main
import (
"fmt"
"github.com/gogf/gf/v2/os/gtime"
)
func main() {
formats := []string{
"2006-01-02 15:04:05.000",
"Mon Jan _2 15:04:05 MST 2006",
"Time is: 03:04:05 PM",
"2006-01-02T15:04:05.000000000Z07:00 MST",
}
t := gtime.Now()
for _, f := range formats {
fmt.Println(t.Layout(f))
}
}
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:
2018-07-22 11:28:13.945
Sun Jul 22 11:28:13 CST 2018
Time is: 11:28:13 AM
2018-07-22T11:28:13.945153275+08:00 CST
Key points:
- Custom formatting syntax and standard library formatting syntax are not conflicting. They use
Format
andLayout
methods respectively and are independent and non-interchangeable. - The standard library's formatting syntax has its unique complexities.
Example 3: Chained Operations on Time Objects
package main
import (
"fmt"
"github.com/gogf/gf/v2/os/gtime"
"time"
)
func main() {
// This time last year, system time
fmt.Println(gtime.Now().AddDate(-1, 0, 0).Format("Y-m-d"))
// This time last year, UTC time
fmt.Println(gtime.Now().AddDate(-1, 0, 0).Format("Y-m-d H:i:s T"))
fmt.Println(gtime.Now().AddDate(-1, 0, 0).UTC().Format("Y-m-d H:i:s T"))
// Midnight on the 1st of next month
fmt.Println(gtime.Now().AddDate(0, 1, 0).Format("Y-m-01 00:00:00"))
// 1 hour ago
fmt.Println(gtime.Now().Add(-time.Hour).Format("Y-m-d H:i:s"))
}
The output is:
2020-09-19
2020-09-19 15:51:48 CST
2020-09-19 07:51:48 UTC
2021-10-01 00:00:00
2021-09-19 14:51:48
This example is straightforward and needs no further elaboration.