时间处理几个例子
-
// time_my project main.go
-
package main
-
-
// Copyright 2011 The Go Authors. All rights reserved.
-
// Use of this source code is governed by a BSD-style
-
// license that can be found in the LICENSE file.
-
-
import (
-
"fmt"
-
"time"
-
)
-
-
func expensiveCall() {
-
var sum = 0
-
for i := 0; i < 999999; i++ {
-
sum += i
-
}
-
}
-
-
func ExampleDuration() {
-
t0 := time.Now()
-
fmt.Println("t0", t0)
-
expensiveCall()
-
t1 := time.Now()
-
fmt.Println("t1", t1)
-
fmt.Printf("The call took %v to run.\n", t1.Sub(t0))
-
}
-
-
var c chan int
-
-
func handle(int) { fmt.Println("in handle") }
-
-
func ExampleAfter() {
-
select {
-
case m := <-c:
-
handle(m)
-
case <-time.After(5 * time.Millisecond):
-
fmt.Println("timed out")
-
}
-
}
-
-
func ExampleSleep() {
-
fmt.Println("before sleep:", time.Now().String())
-
time.Sleep(100 * time.Millisecond)
-
fmt.Println("after sleep:", time.Now().String())
-
}
-
-
func statusUpdate() string { return "" }
-
-
func ExampleTick() {
-
//c := time.Tick(1 * time.Minute)
-
c := time.Tick(1 * time.Second)
-
fmt.Println("c=", c)
-
for now := range c {
-
fmt.Printf("%v %s\n", now, statusUpdate())
-
}
-
}
-
-
func ExampleMonth() {
-
_, month, day := time.Now().Date()
-
if month == time.November && day == 10 {
-
fmt.Println("Happy Go day!")
-
} else {
-
fmt.Println("not 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())
-
// Output: Go launched at 2009-11-10 15:00:00 -0800 PST
-
}
-
-
func ExampleTime_Format() {
-
fmt.Println("time.UnixDate11=", time.UnixDate)
-
// Parse a time value from a string in the standard Unix format.
-
t, err := time.Parse(time.UnixDate, "Sat Mar 7 11:06:39 PST 2015")
-
if err != nil { // Always check errors even if they should not happen.
-
panic(err)
-
}
-
-
fmt.Println("time.UnixDate=", time.UnixDate)
-
-
// time.Time's Stringer method is useful without any format.
-
fmt.Println("default format:", t)
-
-
// Predefined constants in the package implement common layouts.
-
fmt.Println("Unix format:", t.Format(time.UnixDate))
-
-
// The time zone attached to the time value affects its output.
-
fmt.Println("Same, in UTC:", t.UTC().Format(time.UnixDate))
-
-
// Most uses of Format and Parse use constant layout strings such as
-
// the ones defined in this package, but the interface is flexible,
-
// as these examples show.
-
-
// Define a helper function to make the examples' output look nice.
-
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("%-15s %q gives %q\n", name, layout, got)
-
}
-
-
// Print a header in our output.
-
fmt.Printf("\nFormats:\n\n")
-
-
// A simple starter example.
-
do("Basic", "Mon Jan 2 15:04:05 MST 2006", "Sat Mar 7 11:06:39 PST 2015")
-
-
// For fixed-width printing of values, such as the date, that may be one or
-
// two characters (7 vs. 07), use an _ instead of a space in the layout string.
-
// Here we print just the day, which is 2 in our layout string and 7 in our
-
// value.
-
do("No pad", "<2>", "<7>")
-
-
// An underscore represents a zero pad, if required.
-
do("Spaces", "<_2>", "< 7>")
-
-
// Similarly, a 0 indicates zero padding.
-
do("Zeros", "<02>", "<07>")
-
-
// If the value is already the right width, padding is not used.
-
// For instance, the second (05 in the reference time) in our value is 39,
-
// so it doesn't need padding, but the minutes (04, 06) does.
-
do("Suppressed pad", "04:05", "06:39")
-
-
// The predefined constant Unix uses an underscore to pad the day.
-
// Compare with our simple starter example.
-
do("Unix", time.UnixDate, "Sat Mar 7 11:06:39 PST 2015")
-
-
// The hour of the reference time is 15, or 3PM. The layout can express
-
// it either way, and since our value is the morning we should see it as
-
// an AM time. We show both in one format string. Lower case too.
-
do("AM/PM", "3PM==3pm==15h", "11AM==11am==11h")
-
-
// When parsing, if the seconds value is followed by a decimal point
-
// and some digits, that is taken as a fraction of a second even if
-
// the layout string does not represent the fractional second.
-
// Here we add a fractional second to our time value used above.
-
t, err = time.Parse(time.UnixDate, "Sat Mar 7 11:06:39.1234 PST 2015")
-
if err != nil {
-
panic(err)
-
}
-
// It does not appear in the output if the layout string does not contain
-
// a representation of the fractional second.
-
do("No fraction", time.UnixDate, "Sat Mar 7 11:06:39 PST 2015")
-
-
// Fractional seconds can be printed by adding a run of 0s or 9s after
-
// a decimal point in the seconds value in the layout string.
-
// If the layout digits are 0s, the fractional second is of the specified
-
// width. Note that the output has a trailing zero.
-
do("0s for fraction", "15:04:05.00000", "11:06:39.12340")
-
-
// If the fraction in the layout is 9s, trailing zeros are dropped.
-
do("9s for fraction", "15:04:05.99999999", "11:06:39.1234")
-
-
}
-
-
func ExampleParse() {
-
// See the example for time.Format for a thorough description of how
-
// to define the layout string to parse a time.Time value; Parse and
-
// Format use the same model to describe their input and output.
-
-
// longForm shows by example how the reference time would be represented in
-
// the desired layout.
-
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
-
t, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
-
fmt.Println(t)
-
-
// shortForm is another way the reference time would be represented
-
// in the desired layout; it has no time zone present.
-
// Note: without explicit zone, returns time in UTC.
-
const shortForm = "2006-Jan-02"
-
t, _ = time.Parse(shortForm, "2013-Feb-03")
-
fmt.Println(t)
-
-
// Output:
-
// 2013-02-03 19:54:00 -0800 PST
-
// 2013-02-03 00:00:00 +0000 UTC
-
}
-
-
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)
-
-
// Note: without explicit zone, returns time in given location.
-
const shortForm = "2006-Jan-02"
-
t, _ = time.ParseInLocation(shortForm, "2012-Jul-09", loc)
-
fmt.Println(t)
-
-
// Output:
-
// 2012-07-09 05:02:00 +0200 CEST
-
// 2012-07-09 00:00:00 +0200 CEST
-
}
-
-
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"))
-
}
-
// Output:
-
// t.Round( 1ns) = 12:15:30.918273645
-
// t.Round( 1?s) = 12:15:30.918274
-
// t.Round( 1ms) = 12:15:30.918
-
// t.Round( 1s) = 12:15:31
-
// t.Round( 2s) = 12:15:30
-
// t.Round( 1m0s) = 12:16:00
-
// t.Round( 10m0s) = 12:20:00
-
// t.Round(1h0m0s) = 12:00:00
-
}
-
-
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"))
-
}
-
// 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
-
// t.Truncate( 1?s) = 12:15:30.918273
-
// t.Truncate( 1ms) = 12:15:30.918
-
// t.Truncate( 1s) = 12:15:30
-
// t.Truncate( 2s) = 12:15:30
-
// t.Truncate( 1m0s) = 12:15:00
-
// t.Truncate(10m0s) = 12:10:00
-
}
-
-
func main() {
-
ExampleDuration()
-
ExampleAfter()
-
//ExampleTick()
-
ExampleMonth()
-
ExampleDate()
-
-
fmt.Println("---begin test ExampleSleep---")
-
ExampleSleep()
-
fmt.Println("### end test ExampleSleep ###")
-
-
fmt.Println("---begin test ExampleTime_Format---")
-
ExampleTime_Format()
-
fmt.Println("### end test ExampleTime_Format ###\n")
-
-
fmt.Println("---begin test ExampleParse---")
-
ExampleParse()
-
fmt.Println("### end test ExampleParse ###\n")
-
-
fmt.Println("---begin test ExampleParseInLocation---")
-
ExampleParseInLocation()
-
fmt.Println("### end test ExampleParseInLocation ###\n")
-
-
fmt.Println("---begin test ExampleTime_Round---")
-
ExampleTime_Round()
-
fmt.Println("### end test ExampleTime_Round ###\n")
-
-
fmt.Println("---begin test ExampleTime_Truncate---")
-
ExampleTime_Truncate()
-
fmt.Println("### end test ExampleTime_Truncate ###\n")
-
}
输出结果:
t0 2018-01-30 11:15:39.3920716 +0800 CST m=+0.007021901
t1 2018-01-30 11:15:39.4171599 +0800 CST m=+0.032110201
The call took 25.0883ms to run.
timed out
not Happy go day
Go launched at 2009-11-11 07:00:00 +0800 CST
---begin test ExampleSleep---
before sleep: 2018-01-30 11:15:39.4221742 +0800 CST m=+0.037124501
after sleep: 2018-01-30 11:15:39.5337492 +0800 CST m=+0.148699501
### end test ExampleSleep ###
---begin test ExampleTime_Format---
time.UnixDate11= Mon Jan _2 15:04:05 MST 2006
time.UnixDate= Mon Jan _2 15:04:05 MST 2006
default format: 2015-03-07 11:06:39 +0000 PST
Unix format: Sat Mar 7 11:06:39 PST 2015
Same, in UTC: Sat Mar 7 11:06:39 UTC 2015
Formats:
Basic "Mon Jan 2 15:04:05 MST 2006" gives "Sat Mar 7 11:06:39 PST 2015"
No pad "<2>" gives "<7>"
Spaces "<_2>" gives "< 7>"
Zeros "<02>" gives "<07>"
Suppressed pad "04:05" gives "06:39"
Unix "Mon Jan _2 15:04:05 MST 2006" gives "Sat Mar 7 11:06:39 PST 2015"
AM/PM "3PM==3pm==15h" gives "11AM==11am==11h"
No fraction "Mon Jan _2 15:04:05 MST 2006" gives "Sat Mar 7 11:06:39 PST 2015"
0s for fraction "15:04:05.00000" gives "11:06:39.12340"
9s for fraction "15:04:05.99999999" gives "11:06:39.1234"
### end test ExampleTime_Format ###
---begin test ExampleParse---
2013-02-03 19:54:00 +0000 PST
2013-02-03 00:00:00 +0000 UTC
### end test ExampleParse ###
---begin test ExampleParseInLocation---
2012-07-09 05:02:00 +0200 CEST
2012-07-09 00:00:00 +0200 CEST
### end test ExampleParseInLocation ###
---begin test ExampleTime_Round---
t.Round( 1ns) = 12:15:30.918273645
t.Round( 1?s) = 12:15:30.918274
t.Round( 1ms) = 12:15:30.918
t.Round( 1s) = 12:15:31
t.Round( 2s) = 12:15:30
t.Round( 1m0s) = 12:16:00
t.Round( 10m0s) = 12:20:00
t.Round(1h0m0s) = 12:00:00
### end test ExampleTime_Round ###
---begin test ExampleTime_Truncate---
t.Truncate( 1ns) = 12:15:30.918273645
t.Truncate( 1?s) = 12:15:30.918273
t.Truncate( 1ms) = 12:15:30.918
t.Truncate( 1s) = 12:15:30
t.Truncate( 2s) = 12:15:30
t.Truncate( 1m0s) = 12:15:00
t.Truncate(10m0s) = 12:10:00
### end test ExampleTime_Truncate ###
成功: 进程退出代码 0.
阅读(381) | 评论(0) | 转发(0) |