The use of time function and timer in go language

Source: Internet
Author: User
Tags local time set time

The implementation and use of functions such as time function and timer, hibernation in go language, the code is as follows, the need of small partners to take directly

Package Mainimport ("Time" "FMT") func main () {//Set time zone, if name is "" or "UTC", return UTC;//If Name is "local", return to local;    /Otherwise name should be a recorded location name in the IANA time zone database (the database records the location and the corresponding time zone), such as "America/new_york". Location,err: = time. Loadlocation ("America/new_york") if err! = Nil {panic (ERR)}//Creation time, sequentially: year, month, day, hour, minute, second, nanosecond (1 seconds =1000 milliseconds =1000000 microseconds =1000000000 nanoseconds), timezone t1: = time. Date (2018, 7, 7, 500000000, location) fmt. PRINTLN (T1)//2018-07-07 12:12:12.5-0400 EDT//convert string to time, time format string: "2006-01-02 15:04:05" (Go language) T2,err: = times. Parse ("2006-01-02 15:04:05", "2018-07-07 09:10:05") fmt. PRINTLN (T2)//2018-07-07 09:10:05 +0000 UTC//convert string to time, need to pass in timezone t3,err: = time. Parseinlocation ("20060102", "20180707", time. UTC) fmt. PRINTLN (T3)//2018-07-07 00:00:00 +0000 UTC//Get current time T4: = times. Now () Fmt. PRINTLN (T4)//2018-05-23 20:50:08.9873106 +0800 CST m=+0.012895501//formatted output FMT. Println (T4. Format ("2006-01-02 15:04:05"))//2018-05-23 20:50:08 FMT. Println (T4. Format ("02/01/2006 15:04:05"))//23/05/2018 20:50:08 FMT. Println (T4. Format ("2006-01-02"))//2018-05-23 FMT. Println (T4. Format ("15:04:05"))//20:50:08 FMT. Println (T4. Format ("January 2,2006"))//May 23,2018//Get World Unity time T5: = T4. UTC () fmt. PRINTLN (T5)//2018-05-23 12:50:08.9873106 +0000 UTC//Get local time T6: = T5. Local () fmt. PRINTLN (T6)//2018-05-23 20:50:08.9873106 +0800 CST//Get time for the specified time zone t7: = T6. In (location) fmt. PRINTLN (T7)//2018-05-23 08:50:08.9873106-0400 EDT//Get UNIX timestamp, units: seconds, that is, time times elapsed from point-in-time 1970-01-01 00:00:00 UTC to Point-in-time t Tamp: = T7. Unix () fmt. PRINTLN (timestamp)//1527080185//Get UNIX timestamp, unit: nanosecond, commonly used as Rand's random number seed timestamp = T7. Unixnano () fmt. PRINTLN (timestamp)//1527080185738346000//To determine whether two time is equal, will determine the time zone and other information, not the same zone can also be used to compare FMT. Println (T7. Equal (T6))//TRUE//Determine if T4 is in FMT before T3. Println (T4. Before (T3))//TRUE//Determine if T4 is after T3 fmt. Println (T4. After (T3))//False//return time of year, month, day y,m,d: = T4. Dat(e) Fmt. Printf ("Year:%d, month:%d, day:%d\n", Y, M, D)//year: 2018, Month: 5, Day: 23//return time, minutes, seconds h,minute,s: = T4. Clock () fmt. Printf ("Time:%d, min:%d, sec:%d\n", H, minute, s)//When: 21, min: 5, S: 41//Get year, month, day, time, minute, second, and week FMT separately. Printf ("Year:%d, month:%d, day:%d, when:%d, min:%d, sec:%d, week:%d\n", T4. Year (), T4. Month (), T4. Day (), T4. Hour (), T4. Minute (), T4. Second (), T4. Weekday ())///year: 2018, Month: 5, Day: 23, Hours: 21, minute: 9, second: 56, Week: 3 t8,err: = time. Parse ("2006-01-02 15:04:05", "2018-01-01 00:00:00")//Add 100 seconds, time. The duration is in nanoseconds, time. Second=1000 000 000. The parameter can be negative or decrease T9: = T8. ADD (time. Duration (+) * time. Second) fmt. PRINTLN (T9)//2018-01-01 00:01:40 +0000 UTC//increase or decrease year, month, day T10: = T8. Adddate (1, 1,-1) fmt. PRINTLN (T10)//2019-01-31 00:00:00 +0000 UTC//Calculates the difference between two time dur: = T8. Sub (T9) fmt. Println (dur. Seconds ())//-100//======================================================================= timer, hibernation etc.//timer, single time event , after the specified time, sends the time to channel C timer: = times. Newtimer (time. Duration (1) * time. Second) fmt. Println (<-timeR.C)//can also be used with select timer = time. Newtimer (time. Duration (1) * time. Second) Select {Case <-timer. C:fmt. Println ("Perform ...")}//Use timer to implement timers = time. Newtimer (time. Duration (1) * time. Second) for {select {case <-timer. C:fmt. PRINTLN ("Timer timer ...") timer. Reset (time. Duration (1) * time. Second)//Restart Timer}}//Start a new process to execute a given function after a specified time, so the main process needs to be dormant for a few seconds to see the execution result time when testing. Afterfunc (time. Duration (1) * time. Second, func () {fmt. Println ("Afterfunc ...")})//Current process hibernate time specified. Sleep (2 * time. Second)//After the specified time, send the time to channel C TT: = <-. After (time. Duration (1) * time. Second) fmt.    PRINTLN (TT)//ticker takes care of a channel and passes "tick" to it at intervals. Ticker: = time. Newticker (time. Duration (1) * time. Second)//implement timer for {select {case <-ticker with ticker. C:fmt. Println ("Ticker ...")}}}

Use of time functions and timers in the go language

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.