diff options
author | Kevin Burke <kev@inburke.com> | 2017-06-27 11:36:32 -0700 |
---|---|---|
committer | Kevin Burke <kev@inburke.com> | 2017-06-28 18:12:46 +0000 |
commit | e1ced3219506938daf404bb2373333cd3352f350 (patch) | |
tree | 3e9b45c19c95d338cfd050ea8ad995d535a39cb0 /src/time/example_test.go | |
parent | 4e9c86ab8e9faf6da9864db6ad81d396f3f55379 (diff) | |
download | go-e1ced3219506938daf404bb2373333cd3352f350.tar.gz go-e1ced3219506938daf404bb2373333cd3352f350.zip |
time: show how to get midnight on the current day
A common task is trying to get today's date in the local time zone
with zero values for the hour, minute, second, and nanosecond fields.
I tried this recently and incorrectly used Truncate(24*time.Hour),
which truncates based on a UTC clock, and gave me 5pm Pacific time
instead of midnight Pacific.
I thought it would be helpful to show a "correct" way to do this.
Change-Id: I479e6b0cc56367068530981ca69882b34febf945
Reviewed-on: https://go-review.googlesource.com/46833
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/time/example_test.go')
-rw-r--r-- | src/time/example_test.go | 3 |
1 files changed, 3 insertions, 0 deletions
diff --git a/src/time/example_test.go b/src/time/example_test.go index 7dc2bb5e7e..aeb63caa55 100644 --- a/src/time/example_test.go +++ b/src/time/example_test.go @@ -256,6 +256,9 @@ func ExampleTime_Truncate() { for _, d := range trunc { fmt.Printf("t.Truncate(%5s) = %s\n", d, t.Truncate(d).Format("15:04:05.999999999")) } + // To round to the last midnight in the local timezone, create a new Date. + midnight := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local) + _ = midnight // Output: // t.Truncate( 1ns) = 12:15:30.918273645 |