GAAS GFrame项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.3 KiB

3 years ago
3 years ago
  1. package schedule
  2. import (
  3. "time"
  4. )
  5. // 绝对时间线段
  6. type TimeLineSrv struct {
  7. StartTime time.Time // 时间曲线的起始时间点
  8. EndTime time.Time // 时间曲线的结束时间点
  9. Duration time.Duration
  10. EffFactor float64 // 效率因子
  11. WorkShiftNr int // 人员班组编号
  12. }
  13. // 初始化,获取时间差值 = (结束时间 - 开始时间) * 效率因子
  14. func (tlsrv *TimeLineSrv)Init(){
  15. var seconds float64
  16. if tlsrv.EffFactor <= 0 {
  17. tlsrv.EffFactor = 1.0
  18. }
  19. if tlsrv.StartTime.Unix() > tlsrv.EndTime.Unix() {
  20. tlsrv.Duration = 0
  21. }else{
  22. seconds = tlsrv.EndTime.Sub(tlsrv.StartTime).Seconds() * tlsrv.EffFactor
  23. tlsrv.Duration = time.Duration(seconds) * time.Second
  24. }
  25. }
  26. // 获取时间线的Duration,不考虑效率因子
  27. func (tlsrv *TimeLineSrv)GetDuration()(duration time.Duration){
  28. if tlsrv.StartTime.Unix() > tlsrv.EndTime.Unix() {
  29. duration = 0
  30. return
  31. }else{
  32. duration = tlsrv.EndTime.Sub(tlsrv.StartTime)
  33. return
  34. }
  35. }
  36. // 获取时间线的Duration,考虑效率因子
  37. func (tlsrv *TimeLineSrv)GetEffDuration()(effDuration time.Duration){
  38. var seconds float64
  39. if tlsrv.StartTime.Unix() > tlsrv.EndTime.Unix() {
  40. effDuration = 0
  41. return
  42. }else{
  43. if tlsrv.EffFactor <= 0 {
  44. tlsrv.EffFactor = 1.0
  45. }
  46. seconds = tlsrv.EndTime.Sub(tlsrv.StartTime).Seconds() * tlsrv.EffFactor
  47. effDuration = time.Duration(seconds) * time.Second
  48. return
  49. }
  50. }
  51. // 基于线段的起始时间和持续时间计算在该线段上的结束时间
  52. func (tlsrv *TimeLineSrv)GetEndTimeByDuration(duration time.Duration)(endTime time.Time){
  53. var seconds float64
  54. if tlsrv.Duration <= duration{
  55. endTime = tlsrv.EndTime
  56. return
  57. }else{
  58. if tlsrv.EffFactor <= 0 {
  59. tlsrv.EffFactor = 1.0
  60. }
  61. seconds = duration.Seconds() / tlsrv.EffFactor
  62. endTime = tlsrv.EndTime.Add(-time.Duration(seconds) * time.Second)
  63. return
  64. }
  65. return
  66. }
  67. // 基于落在线段内的起始时间计算到线段结束的Duration
  68. func (tlsrv *TimeLineSrv)GetDurationByStartTime(startTime time.Time)(duration time.Duration){
  69. var seconds float64
  70. if startTime.Unix() >= tlsrv.EndTime.Unix(){
  71. duration = 0
  72. return
  73. }
  74. if startTime.Unix() < tlsrv.StartTime.Unix(){
  75. duration = tlsrv.Duration
  76. return
  77. }
  78. seconds = tlsrv.EndTime.Sub(startTime).Seconds() * tlsrv.EffFactor
  79. duration = time.Duration(seconds) * time.Second
  80. return
  81. }
  82. // 基于落在线段的起始时间计算到指定结束时间的Duration
  83. func (tlsrv *TimeLineSrv)GetDurationByEndTime(endTime time.Time)(duration time.Duration){
  84. var seconds float64
  85. if endTime.Unix() <= tlsrv.StartTime.Unix(){
  86. duration = 0
  87. return
  88. }
  89. if endTime.Unix() >= tlsrv.EndTime.Unix(){
  90. duration = tlsrv.Duration
  91. return
  92. }
  93. seconds = endTime.Sub(tlsrv.StartTime).Seconds() * tlsrv.EffFactor
  94. duration = time.Duration(seconds) * time.Second
  95. return
  96. }
  97. // 获取线段上两个时间点之间的Duration
  98. func (tlsrv *TimeLineSrv)GetDurationByStartEndTime(startTime, endTime time.Time)(duration time.Duration){
  99. var seconds float64
  100. if startTime.Unix() < tlsrv.StartTime.Unix(){
  101. startTime = tlsrv.StartTime
  102. }
  103. if endTime.Unix() > tlsrv.EndTime.Unix(){
  104. endTime = tlsrv.EndTime
  105. }
  106. if endTime.Unix() <= startTime.Unix(){
  107. duration = 0
  108. return
  109. }
  110. seconds = endTime.Sub(startTime).Seconds() * tlsrv.EffFactor
  111. duration = time.Duration(seconds) * time.Second
  112. return
  113. }