Time and date related functions


In programming, the programmer will often use a function related to the date, for example: a piece of code execution time statistics of calls and so on.

1) the time and date related functions, packets need to import time

2) time.Time type, for indicating time

3) to get the current time method:
now: = Time.now () // type now is time.Time

main FUNC () {
  .. 1 // get the current time
  now: = Time.now ()
  fmt.Printf ( "V now =% =% T type now \ n-", now, now)
}

 

 

4) How to get to the other date information

main FUNC () {
  now: = Time.now ()

  // 2 can obtain a date by now, minutes and seconds.
  fmt.Printf ( "% v in = \ the n-", now.Year ())
  fmt.Printf ( "month =% v \ n", now.Month ())
  fmt.Printf ( "month =% v \ n", int (now.Month ()))
  fmt.Printf ( "day =% v \ n" , now.Day ())
  fmt.Printf ( "V =% \ n-time", now.Hour ())
  fmt.Printf ( "% V min = \ n-", now.Minute ())
  fmt.Printf ( " % V s = \ n-", now.Second ())
}

 

5) the date and time format
a first embodiment (1) Format: that use Printf or Sprintf

func main() {

  // format the date and time
  now: = Time.now ()
  fmt.Printf ( "current date 02d-% 02d-% 02d%% 02d:% 02d:% 02d \ the n-", now.Year (),
  now .Month (), now.Day (), now.Hour (), now.Minute (), now.Second ())

  fmt.Printf("当前年月日 %d-%d-%d %d:%d:%d \n", now.Year(),
  now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())

  dateStr := fmt.Sprintf("当前年月日 %02d-%02d-%02d %02d:%02d:%02d \n", now.Year(),
  now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
  fmt.Printf("dateStr=%v \n", dateStr )

}

 

The second way (2) Format: Use time.Format () method is completed.

package main
import (
  "fmt"
  "time"
)

func main() {
  now := time.Now()
  fmt.Printf(now.Format("2006-01-02 15:04:05"))
  fmt.Println()
  fmt.Printf(now.Format("2006-01-02"))
  fmt.Println()
  fmt.Printf(now.Format("15:04:05"))
  fmt.Println()
  fmt.Printf(now.Format("2006")) //打印年
  fmt.Println()
  fmt.Printf(now.Format("01")) //打印月
  fmt.Println()
  fmt.Printf(now.Format("02")) //打印日
  fmt.Println()
  fmt.Printf(now.Format("15")) //打印小时
  fmt.Println()
  fmt.Printf(now.Format("04")) //打印分
  fmt.Println()
  fmt.Printf(now.Format("05")) //打印秒
  fmt.Println()
}

The result is:

 

The above description of the code:
"2006/01/02 15:04:05" is the respective number string is fixed, must write.
"2006/01/02 15:04:05" This combination of each string numbers are free, so you can program according to the needs of the time and date to return


Constant 6) Time

const (
  Nanosecond the Duration =. 1 // ns
  Microsecond = 1000 * Nanosecond // sec
  Millisecond = 1000 * Microsecond // ms
  second = 1000 * Millisecond // sec
  Minute = 60 * Second // min
  Hour = 60 * Minute // hours
)

The effects of constants: the program can be used to obtain a given time period of time such as 100 ms 100 * time.Millisecond wants


7) Sleep
func Sleep (d Duration)
Case: time.Sleep (100 * time.Millisecond) // 100 msec Sleep


6 and 7 in combination with case:

main FUNC () {
  // needs to print a digital intervals of one second, when printing 100 to exit
  @ 2 Demand: every 0.1 seconds, a digital print, when printing to the exit 100
  I: = 0
  for {
    ++ I
    fmt.Println (I)
    // sleep
    //time.Sleep(time.Second)
    time.sleep (time.Millisecond 100 *)
    IF I == 100 {
      BREAK
    }
  }

}


8) Get the current unix timestamp and unixnano stamp. (Role is to get a random number)

Case:

main FUNC () {
  now: = Time.now ()
  fmt.Printf ( "UNIX timestamp =% v unixnano timestamp% V = \ n-", now.Unix (), now.UnixNano ())
}

The result is:

 

Best Practice:

Write a piece of code to perform the statistical function of time test03

package main
import (
  "fmt"
  "time"
  "strconv"
)

TEST03 FUNC () {
  STR: = ""
  for I: = 0; I <100000; I ++ {
    STR + = "Hello" + strconv.Itoa (I) is I // int type, needs to be converted into a string type.
  }
}

main FUNC () {
  // before performing test03, to obtain the current timestamp unix,
  Start:. = Time.now () the Unix ()
  Start1:. = Time.now () UnixNano ()
  TEST03 ()
  End: Time.now = () the Unix ().
  END1: = Time.now () UnixNano ().
  fmt.Printf ( "execution TEST03 () is a time-consuming% v seconds \ n-", End-Start)
  fmt.Printf ( " performing TEST03 ()% v nanosecond time-consuming \ n-", END1-Start1)
}

The resulting output is:

 

 

Guess you like

Origin www.cnblogs.com/green-frog-2019/p/11355260.html