github.com/embeddedgo/x@v0.0.6-0.20191217015414-d79a36f562e7/time/example_test.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package time_test
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/embeddedgo/x/time"
    11  	"github.com/embeddedgo/x/time/tz"
    12  )
    13  
    14  func expensiveCall() {}
    15  
    16  func ExampleDuration() {
    17  	t0 := time.Now()
    18  	expensiveCall()
    19  	t1 := time.Now()
    20  	fmt.Printf("The call took %v to run.\n", t1.Sub(t0))
    21  }
    22  
    23  func ExampleDuration_Round() {
    24  	d, err := time.ParseDuration("1h15m30.918273645s")
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  
    29  	round := []time.Duration{
    30  		time.Nanosecond,
    31  		time.Microsecond,
    32  		time.Millisecond,
    33  		time.Second,
    34  		2 * time.Second,
    35  		time.Minute,
    36  		10 * time.Minute,
    37  		time.Hour,
    38  	}
    39  
    40  	for _, r := range round {
    41  		fmt.Printf("d.Round(%6s) = %s\n", r, d.Round(r).String())
    42  	}
    43  	// Output:
    44  	// d.Round(   1ns) = 1h15m30.918273645s
    45  	// d.Round(   1µs) = 1h15m30.918274s
    46  	// d.Round(   1ms) = 1h15m30.918s
    47  	// d.Round(    1s) = 1h15m31s
    48  	// d.Round(    2s) = 1h15m30s
    49  	// d.Round(  1m0s) = 1h16m0s
    50  	// d.Round( 10m0s) = 1h20m0s
    51  	// d.Round(1h0m0s) = 1h0m0s
    52  }
    53  
    54  func ExampleDuration_String() {
    55  	t1 := time.Date(2016, time.August, 15, 0, 0, 0, 0, time.UTC)
    56  	t2 := time.Date(2017, time.February, 16, 0, 0, 0, 0, time.UTC)
    57  	fmt.Println(t2.Sub(t1).String())
    58  	// Output: 4440h0m0s
    59  }
    60  
    61  func ExampleDuration_Truncate() {
    62  	d, err := time.ParseDuration("1h15m30.918273645s")
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  
    67  	trunc := []time.Duration{
    68  		time.Nanosecond,
    69  		time.Microsecond,
    70  		time.Millisecond,
    71  		time.Second,
    72  		2 * time.Second,
    73  		time.Minute,
    74  		10 * time.Minute,
    75  		time.Hour,
    76  	}
    77  
    78  	for _, t := range trunc {
    79  		fmt.Printf("d.Truncate(%6s) = %s\n", t, d.Truncate(t).String())
    80  	}
    81  	// Output:
    82  	// d.Truncate(   1ns) = 1h15m30.918273645s
    83  	// d.Truncate(   1µs) = 1h15m30.918273s
    84  	// d.Truncate(   1ms) = 1h15m30.918s
    85  	// d.Truncate(    1s) = 1h15m30s
    86  	// d.Truncate(    2s) = 1h15m30s
    87  	// d.Truncate(  1m0s) = 1h15m0s
    88  	// d.Truncate( 10m0s) = 1h10m0s
    89  	// d.Truncate(1h0m0s) = 1h0m0s
    90  }
    91  
    92  func ExampleParseDuration() {
    93  	hours, _ := time.ParseDuration("10h")
    94  	complex, _ := time.ParseDuration("1h10m10s")
    95  	micro, _ := time.ParseDuration("1µs")
    96  	// The package also accepts the incorrect but common prefix u for micro.
    97  	micro2, _ := time.ParseDuration("1us")
    98  
    99  	fmt.Println(hours)
   100  	fmt.Println(complex)
   101  	fmt.Printf("There are %.0f seconds in %v.\n", complex.Seconds(), complex)
   102  	fmt.Printf("There are %d nanoseconds in %v.\n", micro.Nanoseconds(), micro)
   103  	fmt.Printf("There are %6.2e seconds in %v.\n", micro2.Seconds(), micro)
   104  	// Output:
   105  	// 10h0m0s
   106  	// 1h10m10s
   107  	// There are 4210 seconds in 1h10m10s.
   108  	// There are 1000 nanoseconds in 1µs.
   109  	// There are 1.00e-06 seconds in 1µs.
   110  }
   111  
   112  func ExampleDuration_Hours() {
   113  	h, _ := time.ParseDuration("4h30m")
   114  	fmt.Printf("I've got %.1f hours of work left.", h.Hours())
   115  	// Output: I've got 4.5 hours of work left.
   116  }
   117  
   118  func ExampleDuration_Minutes() {
   119  	m, _ := time.ParseDuration("1h30m")
   120  	fmt.Printf("The movie is %.0f minutes long.", m.Minutes())
   121  	// Output: The movie is 90 minutes long.
   122  }
   123  
   124  func ExampleDuration_Nanoseconds() {
   125  	u, _ := time.ParseDuration("1µs")
   126  	fmt.Printf("One microsecond is %d nanoseconds.\n", u.Nanoseconds())
   127  	// Output:
   128  	// One microsecond is 1000 nanoseconds.
   129  }
   130  
   131  func ExampleDuration_Seconds() {
   132  	m, _ := time.ParseDuration("1m30s")
   133  	fmt.Printf("Take off in t-%.0f seconds.", m.Seconds())
   134  	// Output: Take off in t-90 seconds.
   135  }
   136  
   137  var c chan int
   138  
   139  func handle(int) {}
   140  
   141  func ExampleAfter() {
   142  	select {
   143  	case m := <-c:
   144  		handle(m)
   145  	case <-time.After(10 * time.Second):
   146  		fmt.Println("timed out")
   147  	}
   148  }
   149  
   150  func ExampleSleep() {
   151  	time.Sleep(100 * time.Millisecond)
   152  }
   153  
   154  func statusUpdate() string { return "" }
   155  
   156  func ExampleTick() {
   157  	c := time.Tick(5 * time.Second)
   158  	for now := range c {
   159  		fmt.Printf("%v %s\n", now, statusUpdate())
   160  	}
   161  }
   162  
   163  func ExampleMonth() {
   164  	_, month, day := time.Now().Date()
   165  	if month == time.November && day == 10 {
   166  		fmt.Println("Happy Go day!")
   167  	}
   168  }
   169  
   170  func ExampleDate() {
   171  	t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
   172  	fmt.Printf("Go launched at %s\n", t.Local())
   173  	// Output: Go launched at 2009-11-10 15:00:00 -0800 PST
   174  }
   175  
   176  func ExampleNewTicker() {
   177  	ticker := time.NewTicker(time.Second)
   178  	defer ticker.Stop()
   179  	done := make(chan bool)
   180  	go func() {
   181  		time.Sleep(10 * time.Second)
   182  		done <- true
   183  	}()
   184  	for {
   185  		select {
   186  		case <-done:
   187  			fmt.Println("Done!")
   188  			return
   189  		case t := <-ticker.C:
   190  			fmt.Println("Current time: ", t)
   191  		}
   192  	}
   193  }
   194  
   195  func ExampleTime_Format() {
   196  	// Parse a time value from a string in the standard Unix format.
   197  	t, err := time.Parse(time.UnixDate, "Sat Mar  7 11:06:39 PST 2015")
   198  	if err != nil { // Always check errors even if they should not happen.
   199  		panic(err)
   200  	}
   201  
   202  	// time.Time's Stringer method is useful without any format.
   203  	fmt.Println("default format:", t)
   204  
   205  	// Predefined constants in the package implement common layouts.
   206  	fmt.Println("Unix format:", t.Format(time.UnixDate))
   207  
   208  	// The time zone attached to the time value affects its output.
   209  	fmt.Println("Same, in UTC:", t.UTC().Format(time.UnixDate))
   210  
   211  	// The rest of this function demonstrates the properties of the
   212  	// layout string used in the format.
   213  
   214  	// The layout string used by the Parse function and Format method
   215  	// shows by example how the reference time should be represented.
   216  	// We stress that one must show how the reference time is formatted,
   217  	// not a time of the user's choosing. Thus each layout string is a
   218  	// representation of the time stamp,
   219  	//	Jan 2 15:04:05 2006 MST
   220  	// An easy way to remember this value is that it holds, when presented
   221  	// in this order, the values (lined up with the elements above):
   222  	//	  1 2  3  4  5    6  -7
   223  	// There are some wrinkles illustrated below.
   224  
   225  	// Most uses of Format and Parse use constant layout strings such as
   226  	// the ones defined in this package, but the interface is flexible,
   227  	// as these examples show.
   228  
   229  	// Define a helper function to make the examples' output look nice.
   230  	do := func(name, layout, want string) {
   231  		got := t.Format(layout)
   232  		if want != got {
   233  			fmt.Printf("error: for %q got %q; expected %q\n", layout, got, want)
   234  			return
   235  		}
   236  		fmt.Printf("%-15s %q gives %q\n", name, layout, got)
   237  	}
   238  
   239  	// Print a header in our output.
   240  	fmt.Printf("\nFormats:\n\n")
   241  
   242  	// A simple starter example.
   243  	do("Basic", "Mon Jan 2 15:04:05 MST 2006", "Sat Mar 7 11:06:39 PST 2015")
   244  
   245  	// For fixed-width printing of values, such as the date, that may be one or
   246  	// two characters (7 vs. 07), use an _ instead of a space in the layout string.
   247  	// Here we print just the day, which is 2 in our layout string and 7 in our
   248  	// value.
   249  	do("No pad", "<2>", "<7>")
   250  
   251  	// An underscore represents a space pad, if the date only has one digit.
   252  	do("Spaces", "<_2>", "< 7>")
   253  
   254  	// A "0" indicates zero padding for single-digit values.
   255  	do("Zeros", "<02>", "<07>")
   256  
   257  	// If the value is already the right width, padding is not used.
   258  	// For instance, the second (05 in the reference time) in our value is 39,
   259  	// so it doesn't need padding, but the minutes (04, 06) does.
   260  	do("Suppressed pad", "04:05", "06:39")
   261  
   262  	// The predefined constant Unix uses an underscore to pad the day.
   263  	// Compare with our simple starter example.
   264  	do("Unix", time.UnixDate, "Sat Mar  7 11:06:39 PST 2015")
   265  
   266  	// The hour of the reference time is 15, or 3PM. The layout can express
   267  	// it either way, and since our value is the morning we should see it as
   268  	// an AM time. We show both in one format string. Lower case too.
   269  	do("AM/PM", "3PM==3pm==15h", "11AM==11am==11h")
   270  
   271  	// When parsing, if the seconds value is followed by a decimal point
   272  	// and some digits, that is taken as a fraction of a second even if
   273  	// the layout string does not represent the fractional second.
   274  	// Here we add a fractional second to our time value used above.
   275  	t, err = time.Parse(time.UnixDate, "Sat Mar  7 11:06:39.1234 PST 2015")
   276  	if err != nil {
   277  		panic(err)
   278  	}
   279  	// It does not appear in the output if the layout string does not contain
   280  	// a representation of the fractional second.
   281  	do("No fraction", time.UnixDate, "Sat Mar  7 11:06:39 PST 2015")
   282  
   283  	// Fractional seconds can be printed by adding a run of 0s or 9s after
   284  	// a decimal point in the seconds value in the layout string.
   285  	// If the layout digits are 0s, the fractional second is of the specified
   286  	// width. Note that the output has a trailing zero.
   287  	do("0s for fraction", "15:04:05.00000", "11:06:39.12340")
   288  
   289  	// If the fraction in the layout is 9s, trailing zeros are dropped.
   290  	do("9s for fraction", "15:04:05.99999999", "11:06:39.1234")
   291  
   292  	// Output:
   293  	// default format: 2015-03-07 11:06:39 -0800 PST
   294  	// Unix format: Sat Mar  7 11:06:39 PST 2015
   295  	// Same, in UTC: Sat Mar  7 19:06:39 UTC 2015
   296  	//
   297  	// Formats:
   298  	//
   299  	// Basic           "Mon Jan 2 15:04:05 MST 2006" gives "Sat Mar 7 11:06:39 PST 2015"
   300  	// No pad          "<2>" gives "<7>"
   301  	// Spaces          "<_2>" gives "< 7>"
   302  	// Zeros           "<02>" gives "<07>"
   303  	// Suppressed pad  "04:05" gives "06:39"
   304  	// Unix            "Mon Jan _2 15:04:05 MST 2006" gives "Sat Mar  7 11:06:39 PST 2015"
   305  	// AM/PM           "3PM==3pm==15h" gives "11AM==11am==11h"
   306  	// No fraction     "Mon Jan _2 15:04:05 MST 2006" gives "Sat Mar  7 11:06:39 PST 2015"
   307  	// 0s for fraction "15:04:05.00000" gives "11:06:39.12340"
   308  	// 9s for fraction "15:04:05.99999999" gives "11:06:39.1234"
   309  
   310  }
   311  
   312  func ExampleParse() {
   313  	// See the example for Time.Format for a thorough description of how
   314  	// to define the layout string to parse a time.Time value; Parse and
   315  	// Format use the same model to describe their input and output.
   316  
   317  	// longForm shows by example how the reference time would be represented in
   318  	// the desired layout.
   319  	const longForm = "Jan 2, 2006 at 3:04pm (MST)"
   320  	t, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
   321  	fmt.Println(t)
   322  
   323  	// shortForm is another way the reference time would be represented
   324  	// in the desired layout; it has no time zone present.
   325  	// Note: without explicit zone, returns time in UTC.
   326  	const shortForm = "2006-Jan-02"
   327  	t, _ = time.Parse(shortForm, "2013-Feb-03")
   328  	fmt.Println(t)
   329  
   330  	// Some valid layouts are invalid time values, due to format specifiers
   331  	// such as _ for space padding and Z for zone information.
   332  	// For example the RFC3339 layout 2006-01-02T15:04:05Z07:00
   333  	// contains both Z and a time zone offset in order to handle both valid options:
   334  	// 2006-01-02T15:04:05Z
   335  	// 2006-01-02T15:04:05+07:00
   336  	t, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
   337  	fmt.Println(t)
   338  	t, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05+07:00")
   339  	fmt.Println(t)
   340  	_, err := time.Parse(time.RFC3339, time.RFC3339)
   341  	fmt.Println("error", err) // Returns an error as the layout is not a valid time value
   342  
   343  	// Output:
   344  	// 2013-02-03 19:54:00 -0800 PST
   345  	// 2013-02-03 00:00:00 +0000 UTC
   346  	// 2006-01-02 15:04:05 +0000 UTC
   347  	// 2006-01-02 15:04:05 +0700 +0700
   348  	// error parsing time "2006-01-02T15:04:05Z07:00": extra text: 07:00
   349  }
   350  
   351  func ExampleParseInLocation() {
   352  	loc := &tz.EuropeBerlin
   353  
   354  	const longForm = "Jan 2, 2006 at 3:04pm (MST)"
   355  	t, _ := time.ParseInLocation(longForm, "Jul 9, 2012 at 5:02am (CEST)", loc)
   356  	fmt.Println(t)
   357  
   358  	// Note: without explicit zone, returns time in given location.
   359  	const shortForm = "2006-Jan-02"
   360  	t, _ = time.ParseInLocation(shortForm, "2012-Jul-09", loc)
   361  	fmt.Println(t)
   362  
   363  	// Output:
   364  	// 2012-07-09 05:02:00 +0200 CEST
   365  	// 2012-07-09 00:00:00 +0200 CEST
   366  }
   367  
   368  func ExampleTime_Unix() {
   369  	// 1 billion seconds of Unix, three ways.
   370  	fmt.Println(time.Unix(1e9, 0).UTC())     // 1e9 seconds
   371  	fmt.Println(time.Unix(0, 1e18).UTC())    // 1e18 nanoseconds
   372  	fmt.Println(time.Unix(2e9, -1e18).UTC()) // 2e9 seconds - 1e18 nanoseconds
   373  
   374  	t := time.Date(2001, time.September, 9, 1, 46, 40, 0, time.UTC)
   375  	fmt.Println(t.Unix())     // seconds since 1970
   376  	fmt.Println(t.UnixNano()) // nanoseconds since 1970
   377  
   378  	// Output:
   379  	// 2001-09-09 01:46:40 +0000 UTC
   380  	// 2001-09-09 01:46:40 +0000 UTC
   381  	// 2001-09-09 01:46:40 +0000 UTC
   382  	// 1000000000
   383  	// 1000000000000000000
   384  }
   385  
   386  func ExampleTime_Round() {
   387  	t := time.Date(0, 0, 0, 12, 15, 30, 918273645, time.UTC)
   388  	round := []time.Duration{
   389  		time.Nanosecond,
   390  		time.Microsecond,
   391  		time.Millisecond,
   392  		time.Second,
   393  		2 * time.Second,
   394  		time.Minute,
   395  		10 * time.Minute,
   396  		time.Hour,
   397  	}
   398  
   399  	for _, d := range round {
   400  		fmt.Printf("t.Round(%6s) = %s\n", d, t.Round(d).Format("15:04:05.999999999"))
   401  	}
   402  	// Output:
   403  	// t.Round(   1ns) = 12:15:30.918273645
   404  	// t.Round(   1µs) = 12:15:30.918274
   405  	// t.Round(   1ms) = 12:15:30.918
   406  	// t.Round(    1s) = 12:15:31
   407  	// t.Round(    2s) = 12:15:30
   408  	// t.Round(  1m0s) = 12:16:00
   409  	// t.Round( 10m0s) = 12:20:00
   410  	// t.Round(1h0m0s) = 12:00:00
   411  }
   412  
   413  func ExampleTime_Truncate() {
   414  	t, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 12:15:30.918273645")
   415  	trunc := []time.Duration{
   416  		time.Nanosecond,
   417  		time.Microsecond,
   418  		time.Millisecond,
   419  		time.Second,
   420  		2 * time.Second,
   421  		time.Minute,
   422  		10 * time.Minute,
   423  	}
   424  
   425  	for _, d := range trunc {
   426  		fmt.Printf("t.Truncate(%5s) = %s\n", d, t.Truncate(d).Format("15:04:05.999999999"))
   427  	}
   428  	// To round to the last midnight in the local timezone, create a new Date.
   429  	midnight := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
   430  	_ = midnight
   431  
   432  	// Output:
   433  	// t.Truncate(  1ns) = 12:15:30.918273645
   434  	// t.Truncate(  1µs) = 12:15:30.918273
   435  	// t.Truncate(  1ms) = 12:15:30.918
   436  	// t.Truncate(   1s) = 12:15:30
   437  	// t.Truncate(   2s) = 12:15:30
   438  	// t.Truncate( 1m0s) = 12:15:00
   439  	// t.Truncate(10m0s) = 12:10:00
   440  }
   441  
   442  func ExampleLocation() {
   443  	// China doesn't have daylight saving. It uses a fixed 8 hour offset from UTC.
   444  	secondsEastOfUTC := int((8 * time.Hour).Seconds())
   445  	beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)
   446  
   447  	// If the system has a timezone database present, it's possible to load a location
   448  	// from that, e.g.:
   449  	//    newYork, err := time.LoadLocation("America/New_York")
   450  
   451  	// Creating a time requires a location. Common locations are time.Local and time.UTC.
   452  	timeInUTC := time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
   453  	sameTimeInBeijing := time.Date(2009, 1, 1, 20, 0, 0, 0, beijing)
   454  
   455  	// Although the UTC clock time is 1200 and the Beijing clock time is 2000, Beijing is
   456  	// 8 hours ahead so the two dates actually represent the same instant.
   457  	timesAreEqual := timeInUTC.Equal(sameTimeInBeijing)
   458  	fmt.Println(timesAreEqual)
   459  
   460  	// Output:
   461  	// true
   462  }
   463  
   464  func ExampleTime_Add() {
   465  	start := time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
   466  	afterTenSeconds := start.Add(time.Second * 10)
   467  	afterTenMinutes := start.Add(time.Minute * 10)
   468  	afterTenHours := start.Add(time.Hour * 10)
   469  	afterTenDays := start.Add(time.Hour * 24 * 10)
   470  
   471  	fmt.Printf("start = %v\n", start)
   472  	fmt.Printf("start.Add(time.Second * 10) = %v\n", afterTenSeconds)
   473  	fmt.Printf("start.Add(time.Minute * 10) = %v\n", afterTenMinutes)
   474  	fmt.Printf("start.Add(time.Hour * 10) = %v\n", afterTenHours)
   475  	fmt.Printf("start.Add(time.Hour * 24 * 10) = %v\n", afterTenDays)
   476  
   477  	// Output:
   478  	// start = 2009-01-01 12:00:00 +0000 UTC
   479  	// start.Add(time.Second * 10) = 2009-01-01 12:00:10 +0000 UTC
   480  	// start.Add(time.Minute * 10) = 2009-01-01 12:10:00 +0000 UTC
   481  	// start.Add(time.Hour * 10) = 2009-01-01 22:00:00 +0000 UTC
   482  	// start.Add(time.Hour * 24 * 10) = 2009-01-11 12:00:00 +0000 UTC
   483  }
   484  
   485  func ExampleTime_AddDate() {
   486  	start := time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC)
   487  	oneDayLater := start.AddDate(0, 0, 1)
   488  	oneMonthLater := start.AddDate(0, 1, 0)
   489  	oneYearLater := start.AddDate(1, 0, 0)
   490  
   491  	fmt.Printf("oneDayLater: start.AddDate(0, 0, 1) = %v\n", oneDayLater)
   492  	fmt.Printf("oneMonthLater: start.AddDate(0, 1, 0) = %v\n", oneMonthLater)
   493  	fmt.Printf("oneYearLater: start.AddDate(1, 0, 0) = %v\n", oneYearLater)
   494  
   495  	// Output:
   496  	// oneDayLater: start.AddDate(0, 0, 1) = 2009-01-02 00:00:00 +0000 UTC
   497  	// oneMonthLater: start.AddDate(0, 1, 0) = 2009-02-01 00:00:00 +0000 UTC
   498  	// oneYearLater: start.AddDate(1, 0, 0) = 2010-01-01 00:00:00 +0000 UTC
   499  }
   500  
   501  func ExampleTime_After() {
   502  	year2000 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
   503  	year3000 := time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)
   504  
   505  	isYear3000AfterYear2000 := year3000.After(year2000) // True
   506  	isYear2000AfterYear3000 := year2000.After(year3000) // False
   507  
   508  	fmt.Printf("year3000.After(year2000) = %v\n", isYear3000AfterYear2000)
   509  	fmt.Printf("year2000.After(year3000) = %v\n", isYear2000AfterYear3000)
   510  
   511  	// Output:
   512  	// year3000.After(year2000) = true
   513  	// year2000.After(year3000) = false
   514  }
   515  
   516  func ExampleTime_Before() {
   517  	year2000 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
   518  	year3000 := time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)
   519  
   520  	isYear2000BeforeYear3000 := year2000.Before(year3000) // True
   521  	isYear3000BeforeYear2000 := year3000.Before(year2000) // False
   522  
   523  	fmt.Printf("year2000.Before(year3000) = %v\n", isYear2000BeforeYear3000)
   524  	fmt.Printf("year3000.Before(year2000) = %v\n", isYear3000BeforeYear2000)
   525  
   526  	// Output:
   527  	// year2000.Before(year3000) = true
   528  	// year3000.Before(year2000) = false
   529  }
   530  
   531  func ExampleTime_Date() {
   532  	d := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC)
   533  	year, month, day := d.Date()
   534  
   535  	fmt.Printf("year = %v\n", year)
   536  	fmt.Printf("month = %v\n", month)
   537  	fmt.Printf("day = %v\n", day)
   538  
   539  	// Output:
   540  	// year = 2000
   541  	// month = February
   542  	// day = 1
   543  }
   544  
   545  func ExampleTime_Day() {
   546  	d := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC)
   547  	day := d.Day()
   548  
   549  	fmt.Printf("day = %v\n", day)
   550  
   551  	// Output:
   552  	// day = 1
   553  }
   554  
   555  func ExampleTime_Equal() {
   556  	secondsEastOfUTC := int((8 * time.Hour).Seconds())
   557  	beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)
   558  
   559  	// Unlike the equal operator, Equal is aware that d1 and d2 are the
   560  	// same instant but in different time zones.
   561  	d1 := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC)
   562  	d2 := time.Date(2000, 2, 1, 20, 30, 0, 0, beijing)
   563  
   564  	datesEqualUsingEqualOperator := d1 == d2
   565  	datesEqualUsingFunction := d1.Equal(d2)
   566  
   567  	fmt.Printf("datesEqualUsingEqualOperator = %v\n", datesEqualUsingEqualOperator)
   568  	fmt.Printf("datesEqualUsingFunction = %v\n", datesEqualUsingFunction)
   569  
   570  	// Output:
   571  	// datesEqualUsingEqualOperator = false
   572  	// datesEqualUsingFunction = true
   573  }
   574  
   575  func ExampleTime_String() {
   576  	timeWithNanoseconds := time.Date(2000, 2, 1, 12, 13, 14, 15, time.UTC)
   577  	withNanoseconds := timeWithNanoseconds.String()
   578  
   579  	timeWithoutNanoseconds := time.Date(2000, 2, 1, 12, 13, 14, 0, time.UTC)
   580  	withoutNanoseconds := timeWithoutNanoseconds.String()
   581  
   582  	fmt.Printf("withNanoseconds = %v\n", string(withNanoseconds))
   583  	fmt.Printf("withoutNanoseconds = %v\n", string(withoutNanoseconds))
   584  
   585  	// Output:
   586  	// withNanoseconds = 2000-02-01 12:13:14.000000015 +0000 UTC
   587  	// withoutNanoseconds = 2000-02-01 12:13:14 +0000 UTC
   588  }
   589  
   590  func ExampleTime_Sub() {
   591  	start := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
   592  	end := time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC)
   593  
   594  	difference := end.Sub(start)
   595  	fmt.Printf("difference = %v\n", difference)
   596  
   597  	// Output:
   598  	// difference = 12h0m0s
   599  }
   600  
   601  func ExampleTime_AppendFormat() {
   602  	t := time.Date(2017, time.November, 4, 11, 0, 0, 0, time.UTC)
   603  	text := []byte("Time: ")
   604  
   605  	text = t.AppendFormat(text, time.Kitchen)
   606  	fmt.Println(string(text))
   607  
   608  	// Output:
   609  	// Time: 11:00AM
   610  }
   611  
   612  func ExampleFixedZone() {
   613  	loc := time.FixedZone("UTC-8", -8*60*60)
   614  	t := time.Date(2009, time.November, 10, 23, 0, 0, 0, loc)
   615  	fmt.Println("The time is:", t.Format(time.RFC822))
   616  	// Output: The time is: 10 Nov 09 23:00 UTC-8
   617  }