時間區間內,線性產生時間點golang
阿新 • • 發佈:2021-10-25
package main import ( "bufio" "fmt" "log" "os" "time" ) func main() { //開始時間 t1 := time.Date(2020, 3, 1, 0, 0, 0, 0, time.Local) //結束時間 t2 := time.Date(2021, 10, 4, 0, 0, 0, 0, time.Local) //不是這個方法 // fmt.Println(t1.Second(), t2.Second()) fmt.Println(t1.Unix(), t2.Unix()) //從上述時間均勻產生 t1_unix := t1.Unix() t2_unix := t2.Unix() // step := (t2_unix - t1_unix) * 1.0 / 1e6 fmt.Println(step) // nctimes := make([]int64, 0, 1e6) // var i int64 for i = 0; i < 1e6+860; i++ { nctimes = append(nctimes, i*step+t1_unix) } // f, err := os.Create("nctime.csv") if err != nil { log.Fatalln(err) } bufioWriter := bufio.NewWriter(f) //轉換時間格式 for _, t := range nctimes { tt := time.Unix(t, 0) // fmt.Printf("%c", '.') // fmt.Println(tt) // fmt.Println(tt.Format("2006-01-02 15:04:05")) bufioWriter.WriteString(tt.Format(`'2006-01-02 15:04:05'`) + "\r\n") // break } // bufioWriter.Flush() }