github.com/szq-123/codingpractice@v0.0.0-20240430111904-2778dfaf7994/golang/time_test.go (about) 1 package golang 2 3 import ( 4 "fmt" 5 "syscall" 6 "testing" 7 "time" 8 ) 9 10 // reference: https://zhuanlan.zhihu.com/p/324922044 11 12 const ( 13 timeFormatDay = "2006-01-02" 14 15 TimeFormatSecond = "2006-01-02 15:04:05" 16 17 TimeFormatLog = "2006/01/02 15:04:05" 18 19 TimeFormatMillisecond = "2006-01-02 15:04:05.000" 20 21 TimeFormatISO = "2006-01-02T15:04:05Z" 22 ) 23 24 func TestTime(t *testing.T) { 25 // Parse current time. 26 ti := time.Now() 27 t1 := ti.Format(TimeFormatMillisecond) 28 fmt.Printf("Current Time: %s\n", t1) 29 30 // Assign location. 31 loc, _ := time.LoadLocation("Local") // UTC, or other valid location name 32 // another way to get local time zone is time.Local. 33 // UTC is accessible if using time.UTC. 34 t2, _ := time.ParseInLocation(TimeFormatLog, "2021/11/02 15:04:05", loc) 35 fmt.Printf("Parse string as time.Time: %s\n", t2) 36 } 37 38 func TestGetCTime(t *testing.T) { 39 var st syscall.Stat_t 40 fileFullPath := "/root/demo.txt" 41 err := syscall.Stat(fileFullPath, &st) 42 if err != nil { 43 fmt.Println(err) 44 return 45 } 46 createTime := time.Unix(st.Ctim.Sec, 0) 47 fmt.Println("file is created at ", createTime) 48 }