secfloat.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package tfelem
  2. import (
  3. "git.swzry.com/zry/YAGTF/yagtf/utils"
  4. "time"
  5. )
  6. type SecondFloatPartPrecision uint8
  7. const (
  8. SFPP_Millisecond SecondFloatPartPrecision = 0
  9. SFPP_Microsecond SecondFloatPartPrecision = 1
  10. SFPP_Nanosecond SecondFloatPartPrecision = 2
  11. )
  12. type SecondFloatPartElement struct {
  13. Precision SecondFloatPartPrecision
  14. }
  15. func (this *SecondFloatPartElement) ExpectedSize() int {
  16. switch this.Precision {
  17. case SFPP_Millisecond:
  18. return 3
  19. case SFPP_Microsecond:
  20. return 6
  21. case SFPP_Nanosecond:
  22. return 9
  23. default:
  24. return 0
  25. }
  26. }
  27. func (this *SecondFloatPartElement) PrintElement(t time.Time) string {
  28. l := t.Nanosecond()
  29. switch this.Precision {
  30. case SFPP_Nanosecond:
  31. return utils.GetFilledNumberWithTruncate(l, 9)
  32. case SFPP_Microsecond:
  33. l = utils.IntDivWithRound(l, 1000)
  34. return utils.GetFilledNumberWithTruncate(l, 6)
  35. case SFPP_Millisecond:
  36. l = utils.IntDivWithRound(l, 1000000)
  37. return utils.GetFilledNumberWithTruncate(l, 3)
  38. default:
  39. return ""
  40. }
  41. }
  42. func NewSecondFloatPartElement(precision SecondFloatPartPrecision) *SecondFloatPartElement {
  43. return &SecondFloatPartElement{
  44. Precision: precision,
  45. }
  46. }
  47. func NewSecondFloatPartElementWithMilliSec() *SecondFloatPartElement {
  48. return &SecondFloatPartElement{
  49. Precision: SFPP_Millisecond,
  50. }
  51. }
  52. func NewSecondFloatPartElementWithMicroSec() *SecondFloatPartElement {
  53. return &SecondFloatPartElement{
  54. Precision: SFPP_Microsecond,
  55. }
  56. }
  57. func NewSecondFloatPartElementWithNanoSec() *SecondFloatPartElement {
  58. return &SecondFloatPartElement{
  59. Precision: SFPP_Nanosecond,
  60. }
  61. }