| |
| |
| |
|
|
| package time_test |
|
|
| import ( |
| "fmt" |
| "math" |
| "time" |
| ) |
|
|
| func expensiveCall() {} |
|
|
| func ExampleDuration() { |
| t0 := time.Now() |
| expensiveCall() |
| t1 := time.Now() |
| fmt.Printf("The call took %v to run.\n", t1.Sub(t0)) |
| } |
|
|
| func ExampleDuration_Round() { |
| d, err := time.ParseDuration("1h15m30.918273645s") |
| if err != nil { |
| panic(err) |
| } |
|
|
| round := []time.Duration{ |
| time.Nanosecond, |
| time.Microsecond, |
| time.Millisecond, |
| time.Second, |
| 2 * time.Second, |
| time.Minute, |
| 10 * time.Minute, |
| time.Hour, |
| } |
|
|
| for _, r := range round { |
| fmt.Printf("d.Round(%6s) = %s\n", r, d.Round(r).String()) |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
|
|
| func ExampleDuration_String() { |
| fmt.Println(1*time.Hour + 2*time.Minute + 300*time.Millisecond) |
| fmt.Println(300 * time.Millisecond) |
| |
| |
| |
| } |
|
|
| func ExampleDuration_Truncate() { |
| d, err := time.ParseDuration("1h15m30.918273645s") |
| if err != nil { |
| panic(err) |
| } |
|
|
| trunc := []time.Duration{ |
| time.Nanosecond, |
| time.Microsecond, |
| time.Millisecond, |
| time.Second, |
| 2 * time.Second, |
| time.Minute, |
| 10 * time.Minute, |
| time.Hour, |
| } |
|
|
| for _, t := range trunc { |
| fmt.Printf("d.Truncate(%6s) = %s\n", t, d.Truncate(t).String()) |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
|
|
| func ExampleParseDuration() { |
| hours, _ := time.ParseDuration("10h") |
| complex, _ := time.ParseDuration("1h10m10s") |
| micro, _ := time.ParseDuration("1µs") |
| |
| micro2, _ := time.ParseDuration("1us") |
|
|
| fmt.Println(hours) |
| fmt.Println(complex) |
| fmt.Printf("There are %.0f seconds in %v.\n", complex.Seconds(), complex) |
| fmt.Printf("There are %d nanoseconds in %v.\n", micro.Nanoseconds(), micro) |
| fmt.Printf("There are %6.2e seconds in %v.\n", micro2.Seconds(), micro2) |
| |
| |
| |
| |
| |
| |
| } |
|
|
| func ExampleSince() { |
| start := time.Now() |
| expensiveCall() |
| elapsed := time.Since(start) |
| fmt.Printf("The call took %v to run.\n", elapsed) |
| } |
|
|
| func ExampleUntil() { |
| futureTime := time.Now().Add(5 * time.Second) |
| durationUntil := time.Until(futureTime) |
| fmt.Printf("Duration until future time: %.0f seconds", math.Ceil(durationUntil.Seconds())) |
| |
| } |
|
|
| func ExampleDuration_Abs() { |
| positiveDuration := 5 * time.Second |
| negativeDuration := -3 * time.Second |
| minInt64CaseDuration := time.Duration(math.MinInt64) |
|
|
| absPositive := positiveDuration.Abs() |
| absNegative := negativeDuration.Abs() |
| absSpecial := minInt64CaseDuration.Abs() == time.Duration(math.MaxInt64) |
|
|
| fmt.Printf("Absolute value of positive duration: %v\n", absPositive) |
| fmt.Printf("Absolute value of negative duration: %v\n", absNegative) |
| fmt.Printf("Absolute value of MinInt64 equal to MaxInt64: %t\n", absSpecial) |
|
|
| |
| |
| |
| |
| } |
|
|
| func ExampleDuration_Hours() { |
| h, _ := time.ParseDuration("4h30m") |
| fmt.Printf("I've got %.1f hours of work left.", h.Hours()) |
| |
| } |
|
|
| func ExampleDuration_Microseconds() { |
| u, _ := time.ParseDuration("1s") |
| fmt.Printf("One second is %d microseconds.\n", u.Microseconds()) |
| |
| |
| } |
|
|
| func ExampleDuration_Milliseconds() { |
| u, _ := time.ParseDuration("1s") |
| fmt.Printf("One second is %d milliseconds.\n", u.Milliseconds()) |
| |
| |
| } |
|
|
| func ExampleDuration_Minutes() { |
| m, _ := time.ParseDuration("1h30m") |
| fmt.Printf("The movie is %.0f minutes long.", m.Minutes()) |
| |
| } |
|
|
| func ExampleDuration_Nanoseconds() { |
| u, _ := time.ParseDuration("1µs") |
| fmt.Printf("One microsecond is %d nanoseconds.\n", u.Nanoseconds()) |
| |
| |
| } |
|
|
| func ExampleDuration_Seconds() { |
| m, _ := time.ParseDuration("1m30s") |
| fmt.Printf("Take off in t-%.0f seconds.", m.Seconds()) |
| |
| } |
|
|
| var c chan int |
|
|
| func handle(int) {} |
|
|
| func ExampleAfter() { |
| select { |
| case m := <-c: |
| handle(m) |
| case <-time.After(10 * time.Second): |
| fmt.Println("timed out") |
| } |
| } |
|
|
| func ExampleSleep() { |
| time.Sleep(100 * time.Millisecond) |
| } |
|
|
| func statusUpdate() string { return "" } |
|
|
| func ExampleTick() { |
| c := time.Tick(5 * time.Second) |
| for next := range c { |
| fmt.Printf("%v %s\n", next, statusUpdate()) |
| } |
| } |
|
|
| func ExampleMonth() { |
| _, month, day := time.Now().Date() |
| if month == time.November && day == 10 { |
| fmt.Println("Happy Go day!") |
| } |
| } |
|
|
| func ExampleDate() { |
| t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) |
| fmt.Printf("Go launched at %s\n", t.Local()) |
| |
| } |
|
|
| func ExampleNewTicker() { |
| ticker := time.NewTicker(time.Second) |
| defer ticker.Stop() |
| done := make(chan bool) |
| go func() { |
| time.Sleep(10 * time.Second) |
| done <- true |
| }() |
| for { |
| select { |
| case <-done: |
| fmt.Println("Done!") |
| return |
| case t := <-ticker.C: |
| fmt.Println("Current time: ", t) |
| } |
| } |
| } |
|
|
| func ExampleTime_Format() { |
| |
| t, err := time.Parse(time.UnixDate, "Wed Feb 25 11:06:39 PST 2015") |
| if err != nil { |
| panic(err) |
| } |
|
|
| tz, err := time.LoadLocation("Asia/Shanghai") |
| if err != nil { |
| panic(err) |
| } |
|
|
| |
| fmt.Println("default format:", t) |
|
|
| |
| fmt.Println("Unix format:", t.Format(time.UnixDate)) |
|
|
| |
| fmt.Println("Same, in UTC:", t.UTC().Format(time.UnixDate)) |
|
|
| fmt.Println("in Shanghai with seconds:", t.In(tz).Format("2006-01-02T15:04:05 -070000")) |
|
|
| fmt.Println("in Shanghai with colon seconds:", t.In(tz).Format("2006-01-02T15:04:05 -07:00:00")) |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| do := func(name, layout, want string) { |
| got := t.Format(layout) |
| if want != got { |
| fmt.Printf("error: for %q got %q; expected %q\n", layout, got, want) |
| return |
| } |
| fmt.Printf("%-16s %q gives %q\n", name, layout, got) |
| } |
|
|
| |
| fmt.Printf("\nFormats:\n\n") |
|
|
| |
| do("Basic full date", "Mon Jan 2 15:04:05 MST 2006", "Wed Feb 25 11:06:39 PST 2015") |
| do("Basic short date", "2006/01/02", "2015/02/25") |
|
|
| |
| |
| |
| do("AM/PM", "3PM==3pm==15h", "11AM==11am==11h") |
|
|
| |
| |
| |
| |
| t, err = time.Parse(time.UnixDate, "Wed Feb 25 11:06:39.1234 PST 2015") |
| if err != nil { |
| panic(err) |
| } |
| |
| |
| do("No fraction", time.UnixDate, "Wed Feb 25 11:06:39 PST 2015") |
|
|
| |
| |
| |
| |
| do("0s for fraction", "15:04:05.00000", "11:06:39.12340") |
|
|
| |
| do("9s for fraction", "15:04:05.99999999", "11:06:39.1234") |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| } |
|
|
| func ExampleTime_Format_pad() { |
| |
| t, err := time.Parse(time.UnixDate, "Sat Mar 7 11:06:39 PST 2015") |
| if err != nil { |
| panic(err) |
| } |
|
|
| |
| do := func(name, layout, want string) { |
| got := t.Format(layout) |
| if want != got { |
| fmt.Printf("error: for %q got %q; expected %q\n", layout, got, want) |
| return |
| } |
| fmt.Printf("%-16s %q gives %q\n", name, layout, got) |
| } |
|
|
| |
| do("Unix", time.UnixDate, "Sat Mar 7 11:06:39 PST 2015") |
|
|
| |
| |
| |
| |
| do("No pad", "<2>", "<7>") |
|
|
| |
| do("Spaces", "<_2>", "< 7>") |
|
|
| |
| do("Zeros", "<02>", "<07>") |
|
|
| |
| |
| |
| do("Suppressed pad", "04:05", "06:39") |
|
|
| |
| |
| |
| |
| |
| |
|
|
| } |
|
|
| func ExampleTime_GoString() { |
| t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) |
| fmt.Println(t.GoString()) |
| t = t.Add(1 * time.Minute) |
| fmt.Println(t.GoString()) |
| t = t.AddDate(0, 1, 0) |
| fmt.Println(t.GoString()) |
| t, _ = time.Parse("Jan 2, 2006 at 3:04pm (MST)", "Feb 3, 2013 at 7:54pm (UTC)") |
| fmt.Println(t.GoString()) |
|
|
| |
| |
| |
| |
| |
| } |
|
|
| func ExampleParse() { |
| |
| |
| |
|
|
| |
| |
| const longForm = "Jan 2, 2006 at 3:04pm (MST)" |
| t, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)") |
| fmt.Println(t) |
|
|
| |
| |
| |
| const shortForm = "2006-Jan-02" |
| t, _ = time.Parse(shortForm, "2013-Feb-03") |
| fmt.Println(t) |
|
|
| |
| |
| |
| |
| |
| |
| t, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") |
| fmt.Println(t) |
| t, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05+07:00") |
| fmt.Println(t) |
| _, err := time.Parse(time.RFC3339, time.RFC3339) |
| fmt.Println("error", err) |
|
|
| |
| |
| |
| |
| |
| |
| } |
|
|
| func ExampleParseInLocation() { |
| loc, _ := time.LoadLocation("Europe/Berlin") |
|
|
| |
| const longForm = "Jan 2, 2006 at 3:04pm (MST)" |
| t, _ := time.ParseInLocation(longForm, "Jul 9, 2012 at 5:02am (CEST)", loc) |
| fmt.Println(t) |
|
|
| |
| const shortForm = "2006-Jan-02" |
| t, _ = time.ParseInLocation(shortForm, "2012-Jul-09", loc) |
| fmt.Println(t) |
|
|
| |
| |
| |
| } |
|
|
| func ExampleUnix() { |
| unixTime := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) |
| fmt.Println(unixTime.Unix()) |
| t := time.Unix(unixTime.Unix(), 0).UTC() |
| fmt.Println(t) |
|
|
| |
| |
| |
| } |
|
|
| func ExampleUnixMicro() { |
| umt := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) |
| fmt.Println(umt.UnixMicro()) |
| t := time.UnixMicro(umt.UnixMicro()).UTC() |
| fmt.Println(t) |
|
|
| |
| |
| |
| } |
|
|
| func ExampleUnixMilli() { |
| umt := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) |
| fmt.Println(umt.UnixMilli()) |
| t := time.UnixMilli(umt.UnixMilli()).UTC() |
| fmt.Println(t) |
|
|
| |
| |
| |
| } |
|
|
| func ExampleTime_Unix() { |
| |
| fmt.Println(time.Unix(1e9, 0).UTC()) |
| fmt.Println(time.Unix(0, 1e18).UTC()) |
| fmt.Println(time.Unix(2e9, -1e18).UTC()) |
|
|
| t := time.Date(2001, time.September, 9, 1, 46, 40, 0, time.UTC) |
| fmt.Println(t.Unix()) |
| fmt.Println(t.UnixNano()) |
|
|
| |
| |
| |
| |
| |
| |
| } |
|
|
| func ExampleTime_Round() { |
| t := time.Date(0, 0, 0, 12, 15, 30, 918273645, time.UTC) |
| round := []time.Duration{ |
| time.Nanosecond, |
| time.Microsecond, |
| time.Millisecond, |
| time.Second, |
| 2 * time.Second, |
| time.Minute, |
| 10 * time.Minute, |
| time.Hour, |
| } |
|
|
| for _, d := range round { |
| fmt.Printf("t.Round(%6s) = %s\n", d, t.Round(d).Format("15:04:05.999999999")) |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
|
|
| func ExampleTime_Truncate() { |
| t, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 12:15:30.918273645") |
| trunc := []time.Duration{ |
| time.Nanosecond, |
| time.Microsecond, |
| time.Millisecond, |
| time.Second, |
| 2 * time.Second, |
| time.Minute, |
| 10 * time.Minute, |
| } |
|
|
| for _, d := range trunc { |
| fmt.Printf("t.Truncate(%5s) = %s\n", d, t.Truncate(d).Format("15:04:05.999999999")) |
| } |
| |
| midnight := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local) |
| _ = midnight |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| } |
|
|
| func ExampleLoadLocation() { |
| location, err := time.LoadLocation("America/Los_Angeles") |
| if err != nil { |
| panic(err) |
| } |
|
|
| timeInUTC := time.Date(2018, 8, 30, 12, 0, 0, 0, time.UTC) |
| fmt.Println(timeInUTC.In(location)) |
| |
| } |
|
|
| func ExampleLocation() { |
| |
| secondsEastOfUTC := int((8 * time.Hour).Seconds()) |
| beijing := time.FixedZone("Beijing Time", secondsEastOfUTC) |
|
|
| |
| |
| |
|
|
| |
| timeInUTC := time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC) |
| sameTimeInBeijing := time.Date(2009, 1, 1, 20, 0, 0, 0, beijing) |
|
|
| |
| |
| timesAreEqual := timeInUTC.Equal(sameTimeInBeijing) |
| fmt.Println(timesAreEqual) |
|
|
| |
| |
| } |
|
|
| func ExampleTime_Add() { |
| start := time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC) |
| afterTenSeconds := start.Add(time.Second * 10) |
| afterTenMinutes := start.Add(time.Minute * 10) |
| afterTenHours := start.Add(time.Hour * 10) |
| afterTenDays := start.Add(time.Hour * 24 * 10) |
|
|
| fmt.Printf("start = %v\n", start) |
| fmt.Printf("start.Add(time.Second * 10) = %v\n", afterTenSeconds) |
| fmt.Printf("start.Add(time.Minute * 10) = %v\n", afterTenMinutes) |
| fmt.Printf("start.Add(time.Hour * 10) = %v\n", afterTenHours) |
| fmt.Printf("start.Add(time.Hour * 24 * 10) = %v\n", afterTenDays) |
|
|
| |
| |
| |
| |
| |
| |
| } |
|
|
| func ExampleTime_AddDate() { |
| start := time.Date(2023, 03, 25, 12, 0, 0, 0, time.UTC) |
| oneDayLater := start.AddDate(0, 0, 1) |
| dayDuration := oneDayLater.Sub(start) |
| oneMonthLater := start.AddDate(0, 1, 0) |
| oneYearLater := start.AddDate(1, 0, 0) |
|
|
| zurich, err := time.LoadLocation("Europe/Zurich") |
| if err != nil { |
| panic(err) |
| } |
| |
| startZurich := time.Date(2023, 03, 25, 12, 0, 0, 0, zurich) |
| oneDayLaterZurich := startZurich.AddDate(0, 0, 1) |
| dayDurationZurich := oneDayLaterZurich.Sub(startZurich) |
|
|
| fmt.Printf("oneDayLater: start.AddDate(0, 0, 1) = %v\n", oneDayLater) |
| fmt.Printf("oneMonthLater: start.AddDate(0, 1, 0) = %v\n", oneMonthLater) |
| fmt.Printf("oneYearLater: start.AddDate(1, 0, 0) = %v\n", oneYearLater) |
| fmt.Printf("oneDayLaterZurich: startZurich.AddDate(0, 0, 1) = %v\n", oneDayLaterZurich) |
| fmt.Printf("Day duration in UTC: %v | Day duration in Zürich: %v\n", dayDuration, dayDurationZurich) |
|
|
| |
| |
| |
| |
| |
| |
| } |
|
|
| func ExampleTime_After() { |
| year2000 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) |
| year3000 := time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC) |
|
|
| isYear3000AfterYear2000 := year3000.After(year2000) |
| isYear2000AfterYear3000 := year2000.After(year3000) |
|
|
| fmt.Printf("year3000.After(year2000) = %v\n", isYear3000AfterYear2000) |
| fmt.Printf("year2000.After(year3000) = %v\n", isYear2000AfterYear3000) |
|
|
| |
| |
| |
| } |
|
|
| func ExampleTime_Before() { |
| year2000 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) |
| year3000 := time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC) |
|
|
| isYear2000BeforeYear3000 := year2000.Before(year3000) |
| isYear3000BeforeYear2000 := year3000.Before(year2000) |
|
|
| fmt.Printf("year2000.Before(year3000) = %v\n", isYear2000BeforeYear3000) |
| fmt.Printf("year3000.Before(year2000) = %v\n", isYear3000BeforeYear2000) |
|
|
| |
| |
| |
| } |
|
|
| func ExampleTime_Date() { |
| d := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC) |
| year, month, day := d.Date() |
|
|
| fmt.Printf("year = %v\n", year) |
| fmt.Printf("month = %v\n", month) |
| fmt.Printf("day = %v\n", day) |
|
|
| |
| |
| |
| |
| } |
|
|
| func ExampleTime_Day() { |
| d := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC) |
| day := d.Day() |
|
|
| fmt.Printf("day = %v\n", day) |
|
|
| |
| |
| } |
|
|
| func ExampleTime_Equal() { |
| secondsEastOfUTC := int((8 * time.Hour).Seconds()) |
| beijing := time.FixedZone("Beijing Time", secondsEastOfUTC) |
|
|
| |
| |
| d1 := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC) |
| d2 := time.Date(2000, 2, 1, 20, 30, 0, 0, beijing) |
|
|
| datesEqualUsingEqualOperator := d1 == d2 |
| datesEqualUsingFunction := d1.Equal(d2) |
|
|
| fmt.Printf("datesEqualUsingEqualOperator = %v\n", datesEqualUsingEqualOperator) |
| fmt.Printf("datesEqualUsingFunction = %v\n", datesEqualUsingFunction) |
|
|
| |
| |
| |
| } |
|
|
| func ExampleTime_String() { |
| timeWithNanoseconds := time.Date(2000, 2, 1, 12, 13, 14, 15, time.UTC) |
| withNanoseconds := timeWithNanoseconds.String() |
|
|
| timeWithoutNanoseconds := time.Date(2000, 2, 1, 12, 13, 14, 0, time.UTC) |
| withoutNanoseconds := timeWithoutNanoseconds.String() |
|
|
| fmt.Printf("withNanoseconds = %v\n", withNanoseconds) |
| fmt.Printf("withoutNanoseconds = %v\n", withoutNanoseconds) |
|
|
| |
| |
| |
| } |
|
|
| func ExampleTime_Sub() { |
| start := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) |
| end := time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC) |
|
|
| difference := end.Sub(start) |
| fmt.Printf("difference = %v\n", difference) |
|
|
| |
| |
| } |
|
|
| func ExampleTime_AppendBinary() { |
| t := time.Date(2025, 4, 1, 15, 30, 45, 123456789, time.UTC) |
|
|
| var buffer []byte |
| buffer, err := t.AppendBinary(buffer) |
| if err != nil { |
| panic(err) |
| } |
|
|
| var parseTime time.Time |
| err = parseTime.UnmarshalBinary(buffer[:]) |
| if err != nil { |
| panic(err) |
| } |
|
|
| fmt.Printf("t: %v\n", t) |
| fmt.Printf("parseTime: %v\n", parseTime) |
| fmt.Printf("equal: %v\n", parseTime.Equal(t)) |
|
|
| |
| |
| |
| |
| } |
|
|
| func ExampleTime_AppendFormat() { |
| t := time.Date(2017, time.November, 4, 11, 0, 0, 0, time.UTC) |
| text := []byte("Time: ") |
|
|
| text = t.AppendFormat(text, time.Kitchen) |
| fmt.Println(string(text)) |
|
|
| |
| |
| } |
|
|
| func ExampleTime_AppendText() { |
| t := time.Date(2025, 4, 1, 15, 30, 45, 123456789, time.UTC) |
|
|
| buffer := []byte("t: ") |
|
|
| buffer, err := t.AppendText(buffer) |
| if err != nil { |
| panic(err) |
| } |
|
|
| fmt.Printf("%s\n", buffer) |
|
|
| |
| |
| } |
| func ExampleFixedZone() { |
| loc := time.FixedZone("UTC-8", -8*60*60) |
| t := time.Date(2009, time.November, 10, 23, 0, 0, 0, loc) |
| fmt.Println("The time is:", t.Format(time.RFC822)) |
| |
| } |
|
|