Brother Lian Blockchain technology training share go language time programming

Source: Internet
Author: User
Tags epoch time unix epoch time

Internet more than 20 years, has reached the crossroads. Before the advent of the blockchain, the internet was known as the classical Internet, and the use of blockchain technology in the Internet only entered the post-Internet era. As a new technology, the blockchain is undoubtedly on the cusp, and its development prospects for the general public will eventually be positive. But at present, because the blockchain technology is in the early stage of development, there are some problems, such as technology maturity and limited application scenario, the Brother Education advises users to carefully consider and discern before choosing professional Go Language + blockchain training institution.


Programming can not be separated from time, time management, strictly speaking divided into two pieces, one is the current moment, corresponding to a point, there is a period of time interval, this article simply talk about the time-related programming of go, relatively simple.


Golang support for time is what the package does, and there are a lot of functions in it.

Familiarity with Linux under C programming is the return value of the time function:

#include


time_t now = time (NULL);

One of the most important representations in Golang is time, which is basically three member variables sec, nsec,location, and the detailed meaning can be noted in the comments.

Type Time struct {

SEC gives the number of seconds elapsed since

January 1, Year 1 00:00:00 UTC.

SEC Int64


NSEC Specifies a non-negative nanosecond

Offset within the second named by Seconds.

It must is in the range [0, 999999999].

nsec int32


LOC Specifies the location that should is used to

Determine the minute, hour, month, day, and year

That correspond to this time.

Only the zero time have a nil location.

In this case it's interpreted to mean UTC.

Loc *location

}

OK, how to access the Unix epoch time.

Now: = time. Now ()


The now () function is very important to get the current time information by using the Today () function in the Times package, which is the starting point for all subsequent conversions. From now () we get to time, from the type we take calmly to the Unix epoch time, which naturally gets to year, month, Day,weekday, Hour,minute,second,nanosecond.

Get the Unix epoch time:

var epoch_seconds int64 = Now. Unix ()

Get year

Func (T time) year () int


Cur_year: = Now. Year ()

Get month

Func (t time) month () month


Cur_month: = Now. Month ()


if Cur_month = = time. November {

...

}

Month is the int type, FMT. Printf ("%v") or FMT. Println can print out the November, and the month type has a string () function, which outputs "November" such as strings

Const (

January Month = 1 + Iota

February

March

April

May

June

July

August

September

October

November

December

)

Year Mon day, which can all be returned in the DATE function:

Func (t time) Date () (year int, month month, day int)


Year,mon,day = Now. Date ()

Get Hour

Func (t time) Hour () int

Cur_hour: = Now. Hour ()


Minute can be returned by minute (), second can be returned by second ().

Time also provides the simultaneous return of clock () Hour,minute,second = Now. Clock ().

The version of Golang is:

Package Main


Import "FMT"

Import "Time"


Func Main () {


Now: = time. Now ()

Year,mon,day: = Now. UTC (). Date ()

Hour,min,sec: = Now. UTC (). Clock ()

Zone,_: = Now. UTC (). Zone ()

Fmt. Printf ("UTC time is%d-%d-%d%02d:%02d:%02d%s\n",

Year,mon,day,hour,min,sec,zone)


Year,mon,day = Now. Date ()

Hour,min,sec = Now. Clock ()

Zone,_ = Now. Zone ()

Fmt. Printf ("local time is%d-%d-%d%02d:%02d:%02d%s\n",

Year,mon,day,hour,min,sec,zone)

}


Go version of output

---------------------

UTC time is 2013-11-22 15:51:22 UTC

local time is 2013-11-22 23:51:22 CST

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------

Another topic of concern to us is the time interval, for example, we profile a very time-consuming function, we will write down the time value before the function starts, after the function is finished, record the time value again, and then the difference between the two is the function run time.

This shows that time can be subtracted,

Start_time: = time. Now ()

Expensive_function

End_time: =time. Now ()


var duration duration = End_time. Sub (start_time)

Duration is a data type that is actually a int64 type, characterizing the number of nanoseconds between two moments.

Type Duration Int64


Const (

nanosecond Duration = 1

microsecond = + * nanosecond

Millisecond = + * microsecond

Second = + * Millisecond

Minute = * Second

Hour = * Minute

)

The duration type has minutes ()/second ()/nanoseconds (), which translates duration into minutes per second/nanosecond.

Now: = time. Now ()

Time. Sleep (3*time. Second);

End_time: = time. Now ()


var dur_time time. Duration = End_time. Sub (now)

var elapsed_min float64 = Dur_time. Minutes ()

var elapsed_sec float64 = Dur_time. Seconds ()

var Elapsed_nano int64 = Dur_time. Nanoseconds ()

Fmt. Printf ("elasped%f minutes or \nelapsed%f seconds or \nelapsed%d nanoseconds\n",

Elapsed_min,elapsed_sec,elapsed_nano)

The output is as follows:

elasped 0.050005 minutes or

Elapsed 3.000292 seconds or

Elapsed 3000292435 nanoseconds

--------------------------------------------------------------------------------------------------------------- ---------------------------------

The second section describes duration's apparent use of the sleep () function, which is in nanoseconds, equivalent to the Nanosleep () in C language.

#include

Nanosleep (): _posix_c_source >= 199309L


int nanosleep (const struct TIMESPEC *req, struct timespec *rem);


#include


unsigned int sleep (unsigned int seconds);


The time.sleep in Go are in nanoseconds, of course the duration type:

If you need to write in sleep for 3 seconds:

Time. Sleep (3000000000)

This is too inconvenient, so, Golang can write

Time. Sleep (3*time. Second);

high-energy early warning, brother even education blockchain live course August continues to hit the hot!

The original price of 1188 Yuan of 12 block chain advanced courses, now only need 1 Yuan!

Also can receive free "Go Language foundation actual combat project development" and "Go language Advanced Combat Project development" textbook Two!! Limited time limit!! First come first served!!

Http://www.ydma.cn/open/course/24

Follow brother Lian Blockchain technology public number get more technical dry Goods Oh!!!

Related Article

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.