Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # 2017-03-10: algorithm to correct for leap years in logsum program
  3. # 2017-03-11: Converted leap_test_algorithm to leap_test_function which is called in build_epoch_list
  4. # 2017-03-12: Replaced original work with leap_test_function code with working leap_test_function
  5. # code and tweaked to return leaps and the corrected leap for printing.
  6.  
  7. function format_date () {
  8. # Retruns the date format requested in $1
  9. case "$1" in
  10. as_epoch)
  11. date -d "$2" +%s
  12. ;;
  13. as_day)
  14. date -d @"$2" +%F
  15. ;;
  16. as_day_time)
  17. date +%F_%T
  18. ;;
  19. get_year)
  20. date -d @"$2" +%Y;;
  21. esac
  22. return 0
  23. }
  24.  
  25. reg_date='([0-9]{2}(([02468][048])|([13579][26]))-((02-((0[1-9])|([12][0-9])))|((0[469]|11)-((0[1-9])|([12][0-9])|30))|((0[13578]|1[02])-((0[1-9])|([12][0-9])|3[01]))))|([0-9]{4}-((02-((0[1-9])|(1[0-9])|(2[0-8])))|((0[469]|11)-((0[1-9])|([12][0-9])|30))|((0[13578]|1[02])-((0[1-9])|([12][0-9])|3[01]))))'
  26. default_ranges="30 60 90 180 365 730 1460 1825 3650 7300"
  27. RECENT_RANGES=( $default_ranges )
  28. _SEC_IN_DAY=$((60 * 60 * 24))
  29. leap_list=()
  30.  
  31. function leap_test () {
  32. # Determines the number of leap days in a RECENT_RANGES epoch and corrects epoch accordingly.
  33. # Returns leaps and leap_corrected_epoch.
  34.  
  35. test_epoch="$1"
  36. year_test_epoch=$(format_date get_year "$test_epoch")
  37. year_epoch_now=$(format_date get_year "$_EPOCH_NOW")
  38.  
  39. leaps=0
  40. for (( test_year="$year_epoch_now"; test_year>="$year_test_epoch"; test_year-- )); do
  41. if [[ "$test_year-02-29" =~ $reg_date ]]; then
  42. test_year_leap_day_epoch=$(format_date as_epoch "$test_year-02-29")
  43.  
  44. if [[ "$_EPOCH_NOW" -ge "$test_year_leap_day_epoch" && "$test_epoch" -le "$test_year_leap_day_epoch" ]]; then
  45. let leaps leaps+=1
  46. fi
  47. fi
  48. done
  49.  
  50. echo "$leaps" "$((test_epoch - leaps * _SEC_IN_DAY))"
  51. return 0
  52. }
  53.  
  54. # ENTER A DATE TO CHECK BELOW
  55. my_date="2016-02-27"
  56. _EPOCH_NOW=$(format_date as_epoch "$my_date")
  57. function build_epoch_list () {
  58. # Calculate epoch_value for day-range & add to EPOCH_LIST array
  59. for range in "${RECENT_RANGES[@]}"; do
  60. if_leap=( $(leap_test $((_EPOCH_NOW - range * _SEC_IN_DAY )) ) )
  61. leap_list+=( "${if_leap[0]}" )
  62. EPOCH_LIST+=( "${if_leap[1]}" )
  63. done
  64. return 0
  65. }
  66. build_epoch_list
  67.  
  68. printf "%s\n" "Current Search Start Date: $(format_date as_day "$_EPOCH_NOW")"
  69. printf "%-10s %-9s %-10s\n" "Range" "Leaps" "Start Date"
  70. printf "%s\n" "--------------------------------------"
  71. for i in "${!RECENT_RANGES[@]}"; do
  72. printf " %-10s %-9s %-12s\n" "${RECENT_RANGES[$i]}" "${leap_list[$i]}" "$(format_date as_day "${EPOCH_LIST[$i]}")"
  73. done
  74. printf "%s\n\n\n" "--------------------------------------"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement