timex.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package timex
  2. import (
  3. "speechAnalysis/constvar"
  4. "time"
  5. )
  6. func StringToTime(timeStr string) (time.Time, error) {
  7. return time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
  8. }
  9. func StringToTime2(timeStr string) (time.Time, error) {
  10. return time.ParseInLocation("2006-01-02", timeStr, time.Local)
  11. }
  12. func StringToTime3(timeStr string) (time.Time, error) {
  13. return time.ParseInLocation("2006-01-02 15:04", timeStr, time.Local)
  14. }
  15. func StringToClock(timeStr string) (time.Time, error) {
  16. return time.ParseInLocation("15:04", timeStr, time.Local)
  17. }
  18. func TimeToString(time time.Time) string {
  19. return time.Format("2006-01-02 15:04:05")
  20. }
  21. func TimeToString2(time time.Time) string {
  22. return time.Format("2006-01-02")
  23. }
  24. func TimeToString3(time time.Time) string {
  25. return time.Format("15:04")
  26. }
  27. func UnixTimeToString(_t int64) string {
  28. return time.Unix(_t, 0).In(time.Local).Format("2006-01-02 15:04:05")
  29. }
  30. func UnixTimeToDate(_t int64) string {
  31. return time.Unix(_t, 0).In(time.Local).Format("2006-01-02")
  32. }
  33. func DayStartTime(_t int64) int64 {
  34. l := time.Unix(_t, 0).In(time.Local)
  35. k := time.Date(l.Year(), l.Month(), l.Day(), 0, 0, 0, 0, time.Local).Unix()
  36. return k
  37. }
  38. func DayStartTimeDateStr(_t int64) string {
  39. l := time.Unix(_t, 0).In(time.Local)
  40. k := time.Date(l.Year(), l.Month(), l.Day(), 0, 0, 0, 0, time.Local)
  41. return TimeToString2(k)
  42. }
  43. const timeLayout = "2006-01-02 15:04:05"
  44. func GetCurrentTime() string {
  45. return time.Now().Format(timeLayout)
  46. }
  47. func NextDateTimestamp(base time.Time, unit constvar.InspectCycleUnit, cycle int) time.Time {
  48. var t time.Time
  49. switch unit {
  50. case constvar.InspectCycleUnitWeek:
  51. t = base.AddDate(0, 0, cycle*7)
  52. case constvar.InspectCycleUnitMonth:
  53. t = base.AddDate(0, cycle, 0)
  54. case constvar.InspectCycleUnitDay:
  55. t = base.AddDate(0, 0, cycle)
  56. }
  57. return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  58. }
  59. // Cycle2Seconds 周期换算成秒数
  60. func Cycle2Seconds(unit constvar.InspectCycleUnit, cycle int) int {
  61. var s int
  62. switch unit {
  63. case constvar.InspectCycleUnitWeek:
  64. s = cycle * 86400 * 7
  65. case constvar.InspectCycleUnitMonth:
  66. s = cycle * 86400 * 30
  67. case constvar.InspectCycleUnitDay:
  68. s = cycle * 86400
  69. }
  70. return s
  71. }