Go Language Time Pack learning (time,location,duration,timer,ticker)

Source: Internet
Author: User
Tags local time

Package Main;import ("Time" "FMT") func main () {//time. Time stands for a nanosecond-precision point var t. time;//returns the current time t = times. Now (); Fmt. Printf ("%v\n", t);//reverse back to the time zone FMT. Printf ("%v\n", T.location ());//returns the UTC time and UTC timezone fmt.printf ("%v%v\n", T.UTC (), T.UTC (). Location ());//Ibid, in () returns the time of the specified time zone for the FMT. Printf ("%v%v\n", t.in (time. UTC), t.in (time. UTC). Location ());//returns to the local time zone in FMT. Printf ("%v%v\n", t.local (), t.local (). Location ());//Returns the local time based on the timestamp//The parameter represents the number of seconds and nanoseconds, respectively t2: = time. Unix (1487780010, 0); Fmt. Println (T2);//returns time as specified. time//Specify the year, month, day, time, minute, second, nanosecond, timezone t3: = time. Date, time. Month (5), N, 0, T.location ()), FMT. Println (T3);//Format output time T4: = times. Now (); Fmt. Println (T4. Format ("2006-01-02 15:04:05"));//Get time information t5: = times. Now ();//return date FMT. Println (T5. Date ());//returns to the year FMT. Println (T5. Year ());//Return month FMT. Println (T5. Month ());//return date FMT. Println (T5. Day ());//Return to the week FMT. Println (T5. Weekday ());//returns the year and week numbers under the ISO 9601 standard FMT. Println (T5. Isoweek ());//The FMT is returned on seconds. Println (T5. Clock ());//Return hour FMT. Println (T5. Hour ());//Return minute FMT. Println (T5. Minute ());//returns the second FMT. Println (T5. Second ());Returns the Nanosecond FMT. Println (T5. Nanosecond ());//returns the corresponding day of the year in the FMT. Println (T5. Yearday ());//Returns the time zone FMT. Println (T5. Location ());//returns the canonical name of the time zone, the time offset (in seconds) of the TimeZone relative to UTC, FMT. Println (T5. Zone ());//return timestamp FMT. Println (T5. Unix ());//return nanosecond timestamp FMT. Println (T5. Unixnano ());//Time comparison and calculation t6: = times. Now ();//Whether 0 o'clock time fmt. Println (T6. Iszero ());//t6 time after T5 time, return to true FMT. Println (T6. After (T5));//t5 time returns True FMT before T6 time. Println (T5. Before (T6));//time is the same as FMT. Println (T6. Equal (T6));//returns T6 plus nanosecond time FMT. Println (T6. ADD (10000));//Returns the nanosecond number of two time difference fmt. Println (T6. Sub (T5));//return T6 plus 1 years, January, 1 days of time FMT. Println (T6. Adddate (1, 1, 1));//The serialization of the time T7: = times. Now ();//serialization of Binary bin, _: = T7. Marshalbinary ();//Deserialize binary T7. Unmarshalbinary (BIN) fmt. PRINTLN (T7);//Serialization Jsonjson, _: = T7. Marshaljson (); fmt. Println (String (JSON));//deserialization of jsont7. Unmarshaljson (JSON); fmt. PRINTLN (T7);//serialization of text txt, _: = T7. Marshaltext (); fmt. Println (String (TXT));//deserialization of text T7. Unmarshaltext (TXT); fmt. PRINTLN (T7);//gob encoding Gob, _: = T7. Gobencode (); T7. Gobdecode (GOB); fmt. PRINTLN (T7);//time period. Durationdur: = time. Duration (6666666600000);//Returns a String representing the FMT. Println (Dur. String ());//Returns the hour to indicate FMT. Println (dur. Hours ());//Return minute indicates FMT. Println (dur. Minutes ());//returns seconds to indicate FMT. Println (dur. Seconds ());//returns nanoseconds to indicate FMT. Println (dur. Nanoseconds ());//TimeZone time. location//returns the time zone name FMT. Println (time. Local.string ());//The time zone FMT is returned by the name of the place and the temporal offset. Println (time. Fixedzone ("Shanghai", 800));//Returns the time zone, LOC, by the given time zone name, _: =. Loadlocation ("Asia/shanghai"); fmt. Println (loc);//block the current process for 3 seconds. Sleep (time. Second * 3);//Timer time. timer//creates a trigger timer after 1 seconds timer1: = time. Newtimer (time. Second * 1); <-timer1. C;fmt. Println ("Timer1 end");//1 seconds to run the function time. Afterfunc (time. Second * 1, func () {fmt. Println ("Wait 1 Second");}); Time. Sleep (time. Second * 3);//RBI time. TICKER//creates a RBI that repeats the Ticker in a fixed 1-second period: = time. Newticker (time. Second) num: = 1;for {if num > 5 {//greater than 5 times to close the RBI ticker. Stop (); break;} Otherwise get Chanselect {case <-ticker from the RBI. C:num++;fmt. Println ("1 second ...");}}


Go Language Time Pack learning (time,location,duration,timer,ticker)

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.