根据开始时间与结束时间,计算季度

青旅半醒 2022-01-13 07:13 453阅读 0赞
  1. #!/bin/bash
  2. #开始日期,时间格式形如:2019-02-28 08:20:30
  3. startTime="2018-05-01 00:00:00"
  4. startMonth=`date -d "${startTime}" +"%Y%m"`
  5. #结束日期,时间格式形如:2019-06-12 16:22:59
  6. endTime="2019-04-12 12:30:57"
  7. endMonth=`date -d "${endTime}" +"%Y%m"`
  8. echo "sync startTime[${startTime}], endTime[${endTime}]"
  9. #重置startMonth,如统计3,4月份的季度信息,则要统计2个季度:1~3月为第一个季度,4月为第二季度(4~6月)
  10. startMM=`date -d "${startTime}" +"%m"`
  11. if [ ${startMM} == 02 ] || [ ${startMM} == 03 ];then
  12. startMonth=`date -d "${startTime}" +"%Y01"`
  13. elif [ ${startMM} == 05 ] || [ ${startMM} == 06 ];then
  14. startMonth=`date -d "${startTime}" +"%Y04"`
  15. elif [ ${startMM} == 08 ] || [ ${startMM} == 09 ];then
  16. startMonth=`date -d "${startTime}" +"%Y07"`
  17. elif [ ${startMM} == 11 ] || [ ${startMM} == 12 ];then
  18. startMonth=`date -d "${startTime}" +"%Y10"`
  19. fi
  20. #根据开始月份与结束月份,获取季度信息等
  21. quarterOfYear=2000Y-1Q
  22. while [ ${startMonth} -le ${endMonth} ]
  23. do
  24. startMM=${startMonth:4:2}
  25. if [ ${startMM} == 01 ];then
  26. quarterOfYear=${startMonth:0:4}Y-1Q
  27. elif [ ${startMM} == 04 ];then
  28. quarterOfYear=${startMonth:0:4}Y-2Q
  29. elif [ ${startMM} == 07 ];then
  30. quarterOfYear=${startMonth:0:4}Y-3Q
  31. elif [ ${startMM} == 10 ];then
  32. quarterOfYear=${startMonth:0:4}Y-4Q
  33. fi
  34. tempFirstStartDay=${startMonth}01
  35. tempEndMonth=`date -d "${tempFirstStartDay} 2 months" +"%Y%m"`
  36. if [ ${tempEndMonth} -gt ${endMonth} ];then
  37. tempEndMonth=${endMonth}
  38. fi
  39. echo "cureent quarter[${quarterOfYear}], query date range[${startMonth},${tempEndMonth}]"
  40. tempFirstEndDay=${tempEndMonth}01
  41. startMonth=`date -d "${tempFirstEndDay} 1 months" +"%Y%m"`
  42. done

发表评论

表情:
评论列表 (有 0 条评论,453人围观)

还没有评论,来说两句吧...

相关阅读