pathutils.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package pathutils
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "github.com/opencost/opencost/core/pkg/opencost"
  8. )
  9. // FormatEpochRange returns a string representation of the given start and end times in the form:
  10. // <start-epoch>-<end-epoch>
  11. func FormatEpochRange(start, end time.Time) string {
  12. startStr := strconv.FormatInt(start.Unix(), 10)
  13. endStr := strconv.FormatInt(end.Unix(), 10)
  14. return fmt.Sprintf("%s-%s", startStr, endStr)
  15. }
  16. // FormatEpochWindow returns a string representation of the given window in the form:
  17. // <start-epoch>-<end-epoch>
  18. //
  19. // If the window is not closed, an error is returned.
  20. func FormatEpochWindow(window opencost.Window) (string, error) {
  21. start := window.Start()
  22. end := window.End()
  23. if start == nil || end == nil {
  24. return "", fmt.Errorf("illegal window: %s", window)
  25. }
  26. return FormatEpochRange(*start, *end), nil
  27. }
  28. // EpochFormatToWindow converts an epoch formatted file name to a Window.
  29. func EpochFormatToWindow(fileName string) (opencost.Window, error) {
  30. var window opencost.Window
  31. tokens := strings.Split(fileName, "-")
  32. if len(tokens) != 2 {
  33. return window, fmt.Errorf("invalid path format")
  34. }
  35. startUnix, err := strconv.ParseInt(tokens[0], 10, 64)
  36. if err != nil {
  37. return window, fmt.Errorf("Failed to Parse start(%s): %s\n", tokens[0], err.Error())
  38. }
  39. endUnix, err := strconv.ParseInt(tokens[1], 10, 64)
  40. if err != nil {
  41. return window, fmt.Errorf("Failed to Parse end(%s): %s\n", tokens[1], err.Error())
  42. }
  43. start := time.Unix(startUnix, 0)
  44. end := time.Unix(endUnix, 0)
  45. return opencost.NewWindow(&start, &end), nil
  46. }
  47. // FormatUTFRange returns a string representation of the given start and end times in the form:
  48. // <start-utf>-<end-utf>
  49. func FormatUTFRange(start, end time.Time) string {
  50. startStr := start.Format(time.RFC3339)
  51. endStr := end.Format(time.RFC3339)
  52. return fmt.Sprintf("%s-%s", startStr, endStr)
  53. }
  54. // FormatUTFWindow returns a string representation of the given window in the form:
  55. // <start-epoch>-<end-epoch>
  56. //
  57. // If the window is not closed, an error is returned.
  58. func FormatUTFWindow(window opencost.Window) (string, error) {
  59. start := window.Start()
  60. end := window.End()
  61. if start == nil || end == nil {
  62. return "", fmt.Errorf("illegal window: %s", window)
  63. }
  64. return FormatEpochRange(*start, *end), nil
  65. }
  66. // UTFFormatToWindow converts an epoch UTF file name to a Window.
  67. func UTFFormatToWindow(fileName string) (opencost.Window, error) {
  68. var window opencost.Window
  69. tokens := strings.Split(fileName, "-")
  70. if len(tokens) != 2 {
  71. return window, fmt.Errorf("invalid path format")
  72. }
  73. start, err := time.Parse(time.RFC3339, tokens[0])
  74. if err != nil {
  75. return window, fmt.Errorf("Failed to Parse start(%s): %s\n", tokens[0], err.Error())
  76. }
  77. end, err := time.Parse(time.RFC3339, tokens[1])
  78. if err != nil {
  79. return window, fmt.Errorf("Failed to Parse end(%s): %s\n", tokens[1], err.Error())
  80. }
  81. return opencost.NewWindow(&start, &end), nil
  82. }