You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

time.go 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package timeutils
  13. import (
  14. "time"
  15. )
  16. var timeTemplates = []string{
  17. "2006-01-02 15:04:05", //常规类型
  18. "2006/01/02 15:04:05",
  19. "2006-01-02",
  20. "2006/01/02",
  21. "15:04:05",
  22. }
  23. func GetTimeDurationString(startTime string, endTime string) string {
  24. end, _ := time.Parse("2006-01-02 15:04:05", endTime)
  25. start, _ := time.Parse("2006-01-02 15:04:05", startTime)
  26. difference := end.Sub(start)
  27. return difference.Truncate(time.Second).String()
  28. }
  29. func DurationToDateTime(timeData uint64) time.Time {
  30. timeString := time.Unix(int64(timeData), 0).Format("2006-01-02 15:04:05")
  31. return TimeStringToGoTime(timeString)
  32. }
  33. // TimeStringToGoTime 时间格式字符串转换
  34. func TimeStringToGoTime(tm string) time.Time {
  35. for i := range timeTemplates {
  36. t, err := time.ParseInLocation(timeTemplates[i], tm, time.Local)
  37. if nil == err && !t.IsZero() {
  38. return t
  39. }
  40. }
  41. return time.Time{}
  42. }
  43. func TimeStringRemoveZone(tm string) string {
  44. timeZone, _ := time.Parse("2006-01-02 15:04:05 -0700 MST", tm)
  45. return timeZone.Format("2006-01-02 15:04:05")
  46. }
  47. func TimeRemoveZone(tm time.Time) time.Time {
  48. parse, err := time.Parse("2006-01-02 15:04:05", tm.Format("2006-01-02 15:04:05"))
  49. if err != nil {
  50. return time.Time{}
  51. }
  52. return parse
  53. }
  54. func StringToUnixTime(str string) int64 {
  55. dt, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
  56. if err != nil {
  57. return 0
  58. }
  59. return dt.Unix()
  60. }
  61. func UnixTimeToString(ut int64) string {
  62. t := time.Unix(ut, 0)
  63. return t.Format("2006-01-02 15:04:05")
  64. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.