Time Packet of Golang

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

Let's see what kind of

Time

Time types, including seconds and nanoseconds, and location

Month

Type month int month. Constants defined for 12 months

Weekday

Type Weekday int week, defined for seven days of the week

Duration

Type Duration Int64 duration. The following duration types are defined. Multiple use of time to add and subtract is required to pass in the Duration as a parameter. You can directly pass in the Time.second

Const (nanosecond  Duration = 1Microsecond          = $ * Nanosecondmillisecond          = $ * Microsecondsecond               = * Millisecondminute               = * Secondhour = *                 Minute)

Location

There are two timezone variables in the time package:

    • Time. UTC UTC time
    • Time. local time

Fixedzone (name string, offset int) *location
Sets the time zone name, as well as the timing deviation from the UTC0. Back to Location

Time formatting

Format (layout string) string
Incoming target template (Mon Jan 02 15:04:05-0700 2006). The time is subject to this.
P (T.format ("3:04pm")) p (T.format ("Mon Jan _2 15:04:05 2006")) p (T.format ("2006-01-02t15:04:05.999999-07:00")) p (t. Format ("2006-01-02t15:04:05z07:00")) fmt. Printf ("%d-%02d-%02dt%02d:%02d:%02d-00:00\n", T.year (), T.month (), T.day (), T.hour (), T.minute (), T.second ())
Parse (layout, value string) (Time, error)
Converts the character channeling to the time type.
P: = Fmt. Printlnwithnanos: = "2006-01-02 15:04:05" T, _: = time. Parse (Withnanos, "2013-10-05 18:30:50") p (t.year ())
Parseduration (S string) (Duration, error)
Duration character channeling ("ns", "Us" (or "s"), "Ms", "s", "M", "H".) Convert to duration type. It's nanosecond.
P: = Fmt. Printlnt, _: = time. Parseduration ("1h") p (t.seconds ())

Time related

Time Common functions

Now () time
Gets the current time, which returns the type
Unix (sec Int64, nsec Int64) time
Returns the time type based on the number of seconds and nanoseconds
Date (year int, month month, day, hour, Min, sec, nsec int, loc *location) time
Set Month day return, time type
Since (t time) Duration
returns the difference from the current time

Time Common methods

After (U time) bool
Time Type comparison, whether after
Before (U time) bool
Time Type comparison, whether before
Equal (U time) bool
compare two times for equality
Iszero () bool
Determines whether the time is a zero value, if the SEC and nsec two properties are 0, then the time type is 0
Date () (year int, month month, day int)
return month day, three parameters
Year () int
return year
Month () month
returns the month. is the month type
Day () int
How many numbers are returned
Weekday () Weekday
returns the day of the week, is the weekday type
Isoweek () (year, week int)
returns the year, and the fill is the week ordinal of the year.
Clock () (Hour, min, sec int)
returns hours, minutes, seconds
Hour () int
Return hours
Minute () int
return minutes
Second () int
returns the number of seconds
nanosecond () int
return nanosecond
ADD (d Duration) time
for one time, the added time type is duration. more accurate to nanoseconds. than Adddate
Sub (U time) Duration
calculates the difference of two time. return type duration
Adddate (years int, months int, days int) time
add time. By month date as the parameter
UTC () Time
set the location to UTC, and then return the time. The UTC is 0. Eight hours late than China.
Local () Time
set location to local time. It's computer time.
In (Loc *location) time
set location to designated
location
Location () *location
Gets the location of
the time, if it is a NIC, returns UTC, or if it is empty, represents the local
Zone () (name string, offset int)
returns the time zone, as well as the temporal deviation from UTC
Unix () Int64
return timestamp, since January 1, 1970 to now
Unixnano () Int64
returns the timestamp. Contains nanoseconds
Func Main () {now    : = time. Now ()    secs: = Now. Unix ()    Nanos: = Now. Unixnano ()    FMT. Println (now)    Millis: = nanos/1000000    fmt. PRINTLN (secs)    FMT. Println (Millis)    FMT. Println (Nanos)    FMT. Println (time. Unix (secs, 0))    FMT. Println (time. Unix (0, Nanos))}
Gobencode () ([]byte, error)
encoded as Gob
Gobdecode (buf []byte) error
decoding from Gob
Marshaljson () ([]byte, error)
column as JSON
Unmarshaljson (data []byte) (Err Error)
Decode to JSON
 Func Main () {p: = fmt. Printlnnow: = time. Now () p (now) d: = time. Duration (7200 * + * +) p (d) Then: = time. Date (1, 7, 651387237, time. UTC) P (then) p (then. Year ()) p (then. Month ()) p (then. Day ()) p (then. Hour ()) p (then. Minute ()) p (then. Second ()) p (then. Nanosecond ()) p (then. Location ()) p (then. Weekday ()) p (then. Before (now)) p (then. After (now) p (then.) Equal (now)) p (then. Date ()) p (then. Isoweek ()) p ("----------") p (now. UTC ()) p (now. Local ()) p (now. Location ()) p (now. Zone ()) p (now. Unix ()) p (time. Unix (now. Unix (), 0)) p (now. Unixnano ()) p (time. Unix (0, now.) Unixnano ())) p (now. Gobencode ()) p (now. Marshaljson ()) p (time. Since (now), p ("----------") diff: = now. Sub (then) p (diff) p (diff. Hours ()) p (diff. Minutes ()) p (diff. Seconds ()) p (diff. nanoseconds ()) p (then. Add (diff)) p (then. ADD (-diff)) p (d) p (d.hours ()) p (d.minutes ()) p (D.seconds ()) p (D.nanoseconds ()) p (then. ADD (d))} 
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.