Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334164
  • 博文数量: 214
  • 博客积分: 4258
  • 博客等级: 上校
  • 技术积分: 2021
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-02 09:16
个人简介

http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net

文章分类

全部博文(214)

文章存档

2018年(16)

2015年(1)

2014年(2)

2012年(22)

2011年(173)

分类: C/C++

2018-01-30 11:18:48

时间处理几个例子

点击(此处)折叠或打开

  1. // time_my project main.go
  2. package main

  3. // Copyright 2011 The Go Authors. All rights reserved.
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file.

  6. import (
  7.     "fmt"
  8.     "time"
  9. )

  10. func expensiveCall() {
  11.     var sum = 0
  12.     for i := 0; i < 999999; i++ {
  13.         sum += i
  14.     }
  15. }

  16. func ExampleDuration() {
  17.     t0 := time.Now()
  18.     fmt.Println("t0", t0)
  19.     expensiveCall()
  20.     t1 := time.Now()
  21.     fmt.Println("t1", t1)
  22.     fmt.Printf("The call took %v to run.\n", t1.Sub(t0))
  23. }

  24. var c chan int

  25. func handle(int) { fmt.Println("in handle") }

  26. func ExampleAfter() {
  27.     select {
  28.     case m := <-c:
  29.         handle(m)
  30.     case <-time.After(5 * time.Millisecond):
  31.         fmt.Println("timed out")
  32.     }
  33. }

  34. func ExampleSleep() {
  35.     fmt.Println("before sleep:", time.Now().String())
  36.     time.Sleep(100 * time.Millisecond)
  37.     fmt.Println("after sleep:", time.Now().String())
  38. }

  39. func statusUpdate() string { return "" }

  40. func ExampleTick() {
  41.     //c := time.Tick(1 * time.Minute)
  42.     c := time.Tick(1 * time.Second)
  43.     fmt.Println("c=", c)
  44.     for now := range c {
  45.         fmt.Printf("%v %s\n", now, statusUpdate())
  46.     }
  47. }

  48. func ExampleMonth() {
  49.     _, month, day := time.Now().Date()
  50.     if month == time.November && day == 10 {
  51.         fmt.Println("Happy Go day!")
  52.     } else {
  53.         fmt.Println("not Happy go day")
  54.     }
  55. }

  56. func ExampleDate() {
  57.     t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  58.     fmt.Printf("Go launched at %s\n", t.Local())
  59.     // Output: Go launched at 2009-11-10 15:00:00 -0800 PST
  60. }

  61. func ExampleTime_Format() {
  62.     fmt.Println("time.UnixDate11=", time.UnixDate)
  63.     // Parse a time value from a string in the standard Unix format.
  64.     t, err := time.Parse(time.UnixDate, "Sat Mar 7 11:06:39 PST 2015")
  65.     if err != nil { // Always check errors even if they should not happen.
  66.         panic(err)
  67.     }

  68.     fmt.Println("time.UnixDate=", time.UnixDate)

  69.     // time.Time's Stringer method is useful without any format.
  70.     fmt.Println("default format:", t)

  71.     // Predefined constants in the package implement common layouts.
  72.     fmt.Println("Unix format:", t.Format(time.UnixDate))

  73.     // The time zone attached to the time value affects its output.
  74.     fmt.Println("Same, in UTC:", t.UTC().Format(time.UnixDate))

  75.     // Most uses of Format and Parse use constant layout strings such as
  76.     // the ones defined in this package, but the interface is flexible,
  77.     // as these examples show.

  78.     // Define a helper function to make the examples' output look nice.
  79.     do := func(name, layout, want string) {
  80.         got := t.Format(layout)
  81.         if want != got {
  82.             fmt.Printf("error: for %q got %q; expected %q\n", layout, got, want)
  83.             return
  84.         }
  85.         fmt.Printf("%-15s %q gives %q\n", name, layout, got)
  86.     }

  87.     // Print a header in our output.
  88.     fmt.Printf("\nFormats:\n\n")

  89.     // A simple starter example.
  90.     do("Basic", "Mon Jan 2 15:04:05 MST 2006", "Sat Mar 7 11:06:39 PST 2015")

  91.     // For fixed-width printing of values, such as the date, that may be one or
  92.     // two characters (7 vs. 07), use an _ instead of a space in the layout string.
  93.     // Here we print just the day, which is 2 in our layout string and 7 in our
  94.     // value.
  95.     do("No pad", "<2>", "<7>")

  96.     // An underscore represents a zero pad, if required.
  97.     do("Spaces", "<_2>", "< 7>")

  98.     // Similarly, a 0 indicates zero padding.
  99.     do("Zeros", "<02>", "<07>")

  100.     // If the value is already the right width, padding is not used.
  101.     // For instance, the second (05 in the reference time) in our value is 39,
  102.     // so it doesn't need padding, but the minutes (04, 06) does.
  103.     do("Suppressed pad", "04:05", "06:39")

  104.     // The predefined constant Unix uses an underscore to pad the day.
  105.     // Compare with our simple starter example.
  106.     do("Unix", time.UnixDate, "Sat Mar 7 11:06:39 PST 2015")

  107.     // The hour of the reference time is 15, or 3PM. The layout can express
  108.     // it either way, and since our value is the morning we should see it as
  109.     // an AM time. We show both in one format string. Lower case too.
  110.     do("AM/PM", "3PM==3pm==15h", "11AM==11am==11h")

  111.     // When parsing, if the seconds value is followed by a decimal point
  112.     // and some digits, that is taken as a fraction of a second even if
  113.     // the layout string does not represent the fractional second.
  114.     // Here we add a fractional second to our time value used above.
  115.     t, err = time.Parse(time.UnixDate, "Sat Mar 7 11:06:39.1234 PST 2015")
  116.     if err != nil {
  117.         panic(err)
  118.     }
  119.     // It does not appear in the output if the layout string does not contain
  120.     // a representation of the fractional second.
  121.     do("No fraction", time.UnixDate, "Sat Mar 7 11:06:39 PST 2015")

  122.     // Fractional seconds can be printed by adding a run of 0s or 9s after
  123.     // a decimal point in the seconds value in the layout string.
  124.     // If the layout digits are 0s, the fractional second is of the specified
  125.     // width. Note that the output has a trailing zero.
  126.     do("0s for fraction", "15:04:05.00000", "11:06:39.12340")

  127.     // If the fraction in the layout is 9s, trailing zeros are dropped.
  128.     do("9s for fraction", "15:04:05.99999999", "11:06:39.1234")

  129. }

  130. func ExampleParse() {
  131.     // See the example for time.Format for a thorough description of how
  132.     // to define the layout string to parse a time.Time value; Parse and
  133.     // Format use the same model to describe their input and output.

  134.     // longForm shows by example how the reference time would be represented in
  135.     // the desired layout.
  136.     const longForm = "Jan 2, 2006 at 3:04pm (MST)"
  137.     t, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
  138.     fmt.Println(t)

  139.     // shortForm is another way the reference time would be represented
  140.     // in the desired layout; it has no time zone present.
  141.     // Note: without explicit zone, returns time in UTC.
  142.     const shortForm = "2006-Jan-02"
  143.     t, _ = time.Parse(shortForm, "2013-Feb-03")
  144.     fmt.Println(t)

  145.     // Output:
  146.     // 2013-02-03 19:54:00 -0800 PST
  147.     // 2013-02-03 00:00:00 +0000 UTC
  148. }

  149. func ExampleParseInLocation() {
  150.     loc, _ := time.LoadLocation("Europe/Berlin")

  151.     const longForm = "Jan 2, 2006 at 3:04pm (MST)"
  152.     t, _ := time.ParseInLocation(longForm, "Jul 9, 2012 at 5:02am (CEST)", loc)
  153.     fmt.Println(t)

  154.     // Note: without explicit zone, returns time in given location.
  155.     const shortForm = "2006-Jan-02"
  156.     t, _ = time.ParseInLocation(shortForm, "2012-Jul-09", loc)
  157.     fmt.Println(t)

  158.     // Output:
  159.     // 2012-07-09 05:02:00 +0200 CEST
  160.     // 2012-07-09 00:00:00 +0200 CEST
  161. }

  162. func ExampleTime_Round() {
  163.     t := time.Date(0, 0, 0, 12, 15, 30, 918273645, time.UTC)
  164.     round := []time.Duration{
  165.         time.Nanosecond,
  166.         time.Microsecond,
  167.         time.Millisecond,
  168.         time.Second,
  169.         2 * time.Second,
  170.         time.Minute,
  171.         10 * time.Minute,
  172.         time.Hour,
  173.     }

  174.     for _, d := range round {
  175.         fmt.Printf("t.Round(%6s) = %s\n", d, t.Round(d).Format("15:04:05.999999999"))
  176.     }
  177.     // Output:
  178.     // t.Round( 1ns) = 12:15:30.918273645
  179.     // t.Round( 1?s) = 12:15:30.918274
  180.     // t.Round( 1ms) = 12:15:30.918
  181.     // t.Round( 1s) = 12:15:31
  182.     // t.Round( 2s) = 12:15:30
  183.     // t.Round( 1m0s) = 12:16:00
  184.     // t.Round( 10m0s) = 12:20:00
  185.     // t.Round(1h0m0s) = 12:00:00
  186. }

  187. func ExampleTime_Truncate() {
  188.     t, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 12:15:30.918273645")
  189.     trunc := []time.Duration{
  190.         time.Nanosecond,
  191.         time.Microsecond,
  192.         time.Millisecond,
  193.         time.Second,
  194.         2 * time.Second,
  195.         time.Minute,
  196.         10 * time.Minute,
  197.     }

  198.     for _, d := range trunc {
  199.         fmt.Printf("t.Truncate(%5s) = %s\n", d, t.Truncate(d).Format("15:04:05.999999999"))
  200.     }
  201.     // To round to the last midnight in the local timezone, create a new Date.
  202.     midnight := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
  203.     _ = midnight

  204.     // Output:
  205.     // t.Truncate( 1ns) = 12:15:30.918273645
  206.     // t.Truncate( 1?s) = 12:15:30.918273
  207.     // t.Truncate( 1ms) = 12:15:30.918
  208.     // t.Truncate( 1s) = 12:15:30
  209.     // t.Truncate( 2s) = 12:15:30
  210.     // t.Truncate( 1m0s) = 12:15:00
  211.     // t.Truncate(10m0s) = 12:10:00
  212. }

  213. func main() {
  214.     ExampleDuration()
  215.     ExampleAfter()
  216.     //ExampleTick()
  217.     ExampleMonth()
  218.     ExampleDate()

  219.     fmt.Println("---begin test ExampleSleep---")
  220.     ExampleSleep()
  221.     fmt.Println("### end test ExampleSleep ###")

  222.     fmt.Println("---begin test ExampleTime_Format---")
  223.     ExampleTime_Format()
  224.     fmt.Println("### end test ExampleTime_Format ###\n")

  225.     fmt.Println("---begin test ExampleParse---")
  226.     ExampleParse()
  227.     fmt.Println("### end test ExampleParse ###\n")

  228.     fmt.Println("---begin test ExampleParseInLocation---")
  229.     ExampleParseInLocation()
  230.     fmt.Println("### end test ExampleParseInLocation ###\n")

  231.     fmt.Println("---begin test ExampleTime_Round---")
  232.     ExampleTime_Round()
  233.     fmt.Println("### end test ExampleTime_Round ###\n")

  234.     fmt.Println("---begin test ExampleTime_Truncate---")
  235.     ExampleTime_Truncate()
  236.     fmt.Println("### end test ExampleTime_Truncate ###\n")
  237. }
输出结果:
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) |
给主人留下些什么吧!~~