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.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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-02T15:04:05Z07:00", //RFC3339
  18. "2006-01-02 15:04:05", //常规类型
  19. "2006/01/02T15:04:05Z07:00", //RFC3339
  20. "2006/01/02 15:04:05",
  21. "2006-01-02",
  22. "2006/01/02",
  23. "15:04:05",
  24. }
  25. func GetTimeDurationString(startTime string, endTime string) string {
  26. end, _ := time.Parse("2006-01-02 15:04:05", endTime)
  27. start, _ := time.Parse("2006-01-02 15:04:05", startTime)
  28. difference := end.Sub(start)
  29. return difference.Truncate(time.Second).String()
  30. }
  31. func DurationToDateTime(timeData uint64) time.Time {
  32. timeString := time.Unix(int64(timeData), 0).Format("2006-01-02 15:04:05")
  33. return TimeStringToGoTime(timeString)
  34. }
  35. // TimeStringToGoTime 时间格式字符串转换
  36. func TimeStringToGoTime(tm string) time.Time {
  37. for i := range timeTemplates {
  38. t, err := time.ParseInLocation(timeTemplates[i], tm, time.Local)
  39. if nil == err && !t.IsZero() {
  40. return t
  41. }
  42. }
  43. return time.Time{}
  44. }
  45. func TimeStringRemoveZone(tm string) string {
  46. timeZone, _ := time.Parse("2006-01-02 15:04:05 -0700 MST", tm)
  47. return timeZone.Format("2006-01-02 15:04:05")
  48. }
  49. func TimeRemoveZone(tm time.Time) time.Time {
  50. parse, err := time.Parse("2006-01-02 15:04:05", tm.Format("2006-01-02 15:04:05"))
  51. if err != nil {
  52. return time.Time{}
  53. }
  54. return parse
  55. }
  56. func StringToUnixTime(str string) int64 {
  57. dt, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
  58. if err != nil {
  59. return 0
  60. }
  61. return dt.Unix()
  62. }
  63. func UnixTimeToString(ut int64) string {
  64. t := time.Unix(ut, 0)
  65. return t.Format("2006-01-02 15:04:05")
  66. }

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.