Analysis of Goframe Framework's Gtime module and custom time format syntax

Source: Internet
Author: User
This article to share the content is about the Goframe framework of the Gtime time module and support custom time format syntax, has a certain reference value, the need for friends can refer to.

A universal Time management module that encapsulates commonly used time/date-related methods. and supports custom date formatting syntax, formatting syntax similar to PHP's date syntax.

How to use:

Import "Gitee.com/johng/gf/g/os/gtime"

Method List: Godoc.org/github.com/johng-cn/gf/g/os/gtime

Time format

gtimeThe most important feature of the module is to support the custom time format, refer to the PHP date time format syntax, the following is a list of supported time format syntax:

Time Object

List of methods:

Type time func New (t ... time. Time) *time func newfromstr (str string) *time func newfromstrformat (str string, format string) *time func NewFrom Strlayout (str string, layout string) *time func newfromtime (t time. Time) *time func newfromtimestamp (timestamp Int64) *time func Now () *time func (t *time) Add (d time. Duration) *time func (t *time) adddate (years int, months int, days int) *time func (t *time) Clone () *time func ( T *time) format (format string) string func (t *time) layout (layout string) string func (t *time) Local () *time Fu NC (t *time) microsecond () Int64 func (T *time) millisecond () Int64 func (T *time) nanosecond () Int64 func (t *ti Me) Round (d time. Duration) *time func (t *time) Second () Int64 func (T *time) string () string func (t *time) tolocation (location * Time. Location) *time func (t *time) ToTime () time. Time func (t *time) Truncate (d time. Duration) *time func (t *time) UTC () *time

gtime.Timeobjects can be created through standard library time.Time objects, Unix timestamps, time strings (for example: 2018-07-18 12:01:00), Custom time strings (which require a given format, support for custom formats and standard library formats).

Example 1, custom time formatting syntax

Package Mainimport (    "FMT"    "Gitee.com/johng/gf/g/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 have given four format formats and have converted the current time in these four formats to print. After execution, the output is as follows:

2018-07-22 11:17:13.797sun Jul 11:17:13 CST +0800 2018Time is:11:17:13 am2006-01-02cst15:04:05.000000000z07:00

As you can see, this example shows a few points to note:

    1. If you use a letter that conflicts with a formatted character, you can use the \ symbol to transfer the character, so that the time format parser considers the character to be not a formatted character, but a normal letter. So here's the third example of a string output as:Time is: 11:17:13 am

    2. The use Format method receives a custom time formatting syntax (such as:) instead Y-m-d H:i:s of the standard library's event format syntax (such as: 2006-01-02 15:04:05 ), and the two syntaxes cannot be mixed, so the value of the parameter is output as is in the fourth string example here;

Example 2, standard library time formatting syntax

Package Mainimport (    "FMT"    "Gitee.com/johng/gf/g/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:0 4:05.000000000z07:00 MST ",    }    t: = Gtime. Now ()    for _, F: = range formats {        FMT. Println (T.layout (f))    }}

In this example, we use the time format syntax of four standard libraries to format the current time and output the results to the terminal. After execution, the output is:

2018-07-22 11:28:13.945sun Jul 11:28:13 CST 2018Time is:11:28:13 am2018-07-22t11:28:13.945153275+08:00 CST

To eradicate this example, there are several points to note:

    1. The custom time formatting syntax does not conflict with the standard library time formatting syntax, the former uses the Format method, the latter uses the Layout syntax to format, independent, non-conflicting, can not be mixed;

    2. The standard library's time formatting grammar has its own characteristics, is not feeling a bit complicated;

Example 3, time-object chained operation

Package Mainimport (    "FMT"    "Gitee.com/johng/gf/g/os/gtime" "Time    ") func main () {    //Last year    FMT today. Println (Gtime. Now (). Adddate (-1, 0, 0). Format ("y-m-d"))    //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"))    //1th 0 o'clock in the morning next month    . Println (Gtime. Now (). Adddate (0, 1, 0). Format ("y-m-d 00:00:00"))    //2 hours ago    fmt. Println (Gtime. Now (). ADD (-time. Hour). Format ("Y-m-d h:i:s")}

After execution, the output is:

2017-07-222017-07-22 11:42:36 cst2017-07-22 03:42:36 utc2018-08-22 00:00:002018-07-22 10:42:36

This example is relatively simple and does not have much to repeat.

Tool methods

Godoc.org/github.com/johng-cn/gf/g/os/gtime

Func Date () Stringfunc Datetime () stringfunc microsecond () int64func millisecond () Int64func nanosecond () Int64func Secon D () Int64func setinterval (t time. Duration, callback func () bool) Func Settimezone (Zone string) Errorfunc SetTimeout (t time. Duration, callback Func ()) Func strtotime (str string) (time. Time, error) func Strtotimeformat (str string, format string) (time. Time, error) func strtotimelayout (str string, layout string) (time. Time, error)

The method is relatively simple, the following methods are commonly used;

    1. SecondUsed to obtain the current timestamp Millisecond , Microsecond and Nanosecond to obtain the current millisecond, microsecond, and nanosecond values;

    2. Dateand Datetime used to obtain the current date and the current date time;

    3. SetTimeZoneUsed to set the global time zone for the current process;

    4. See the interface documentation for additional method descriptions;

Simple example:

Package Mainimport (    "FMT"    "Gitee.com/johng/gf/g/os/gtime") func main () {    fmt. Println ("Date       :", Gtime. Date ())    FMT. Println ("Datetime   :", Gtime. Datetime ())    FMT. Println ("Second     :", Gtime. Second ())    FMT. Println ("Millisecond:", Gtime.millisecond ())    FMT. Println ("microsecond:", Gtime. Microsecond ())    FMT. Println ("nanosecond:", Gtime. Nanosecond ())}

After execution, the output is:

date:2018-07-22datetime:2018-07-22 11:52:22second:1532231542millise cond:1532231542688microsecond:1532231542688688nanosecond:1532231542688690259 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.