github.com/vc42/parquet-go@v0.0.0-20240320194221-1a9adb5f23f5/utils/utils.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 ) 8 9 func StringToTime(ts string) *time.Time { 10 layout := "2006-01-02 15:04:05.000-07" 11 if strings.Contains(ts, "T") { 12 layout = time.RFC3339Nano 13 } 14 if len(ts) > len(layout) { 15 fmt.Printf("time string '%s' longer than layout '%s', not converted\n", ts, layout) 16 return nil 17 } 18 t, err := time.Parse(layout[0:len(ts)], ts) 19 if err != nil { 20 fmt.Printf("failed to convert time: %s %v\n", ts, err) 21 return nil 22 } 23 return &t 24 } 25 26 func StringToTimeMs(ts string) int64 { 27 layout := "2006-01-02 15:04:05.000-07" 28 if strings.Contains(ts, "T") { 29 layout = time.RFC3339Nano 30 } 31 if len(ts) > len(layout) { 32 fmt.Printf("time string '%s' longer than layout '%s', not converted\n", ts, layout) 33 return 0 34 } 35 t, err := time.Parse(layout[0:len(ts)], ts) 36 if err != nil { 37 fmt.Printf("failed to convert time: %s %v\n", ts, err) 38 return 0 39 } 40 return t.UnixNano() / 1000000 41 } 42 43 func TimeMsToString(millis int64) string { 44 layout := "2006-01-02 15:04:05.000-07" 45 time.Unix(0, millis*int64(time.Millisecond)) 46 return time.Unix(0, millis*int64(time.Millisecond)).Format(layout) 47 } 48 49 func StringToDate(ts string) int32 { 50 layout := "2006-01-02 15:04:05.000-07" 51 if strings.Contains(ts, "T") { 52 layout = time.RFC3339Nano 53 } 54 if len(ts) > len(layout) { 55 fmt.Printf("time string '%s' longer than layout '%s', not converted\n", ts, layout) 56 return 0 57 } 58 t, err := time.Parse(layout[0:len(ts)], ts) 59 if err != nil { 60 fmt.Println("failed to convert time: %s %v\n", ts, err) 61 return 0 62 } 63 daysInSec := t.Unix() + 1 64 days := daysInSec / 3600 / 24 65 if days*3600*24 < daysInSec { 66 days += 1 67 } 68 return int32(days) 69 } 70 71 func DateToString(date int32) string { 72 t := time.Unix(int64(date*3600*24), 0) 73 return t.Format("2006-01-02") 74 }