The use of time package in Go_21:golang two

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Constant declaration:

Const Timelayout " 2006-01-02 15:04:05 "

This is a wonderful, must be this time point, is said to be the day of the birth of Go, memory method: 6-1-2-3-4-5

1. Get the current timestamp

func Getcurrentsystemtimestamp () Int64 {    return time  . Now (). Unix ()}

2. Get the current string time

string {    return time  . Now (). Format (timelayout)}

3. Timestamp to String time format

Func timestamp2standardtimestr (timestampstring  {    returntime . Unix (timestamp0). Format (timelayout)}

4. String time-to-timestamp

This is a little bit more complicated, in the Golang language, when this time conversion is done, if you are using parse to format directly, the default is to use UTC format. So what exactly is the UTC time format?

The entire Earth is divided into 24 time zones, each with its own local time. In international radiocommunication situations, for the sake of unification, a uniform time is used, known as universal Coordination (UTC, Universal-time coordinated). UTC, like Greenwich mean Time (GMT, Greenwich Mean time), is the same as in London, England. Beijing time Zone is the East Eight District, leading UTC eight hours, so if the incoming string is: 2017-11-13 11:14:21, the result will return UTC after the conversion timestamp is: 1510571661 (2017-11-13 19:14:21).

To solve this problem, you need to use the other parsing function that time gives us: parseinlocaltion

Func Timestr2timestamp (timestrstring) Int64 {//get local time zoneLOC, _: = time. Loadlocation ("Local")    //the time string at the end of the read file will be preceded by a newline character, which needs to be removed or resolved.TM, Error: = time. Parseinlocation (Timelayout, strings. Trim (TIMESTR,"\ n"), loc)ifError! =Nil {log. Fatal (Error)return 0    }    returnTM. Unix ()}

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.