Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # 2017-03-10: algorithm to correct for leap years in logsum program
- # 2017-03-11: Converted leap_test_algorithm to leap_test_function which is called in build_epoch_list
- # 2017-03-12: Replaced original work with leap_test_function code with working leap_test_function
- # code and tweaked to return leaps and the corrected leap for printing.
- function format_date () {
- # Retruns the date format requested in $1
- case "$1" in
- as_epoch)
- date -d "$2" +%s
- ;;
- as_day)
- date -d @"$2" +%F
- ;;
- as_day_time)
- date +%F_%T
- ;;
- get_year)
- date -d @"$2" +%Y;;
- esac
- return 0
- }
- 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]))))'
- default_ranges="30 60 90 180 365 730 1460 1825 3650 7300"
- RECENT_RANGES=( $default_ranges )
- _SEC_IN_DAY=$((60 * 60 * 24))
- leap_list=()
- function leap_test () {
- # Determines the number of leap days in a RECENT_RANGES epoch and corrects epoch accordingly.
- # Returns leaps and leap_corrected_epoch.
- test_epoch="$1"
- year_test_epoch=$(format_date get_year "$test_epoch")
- year_epoch_now=$(format_date get_year "$_EPOCH_NOW")
- leaps=0
- for (( test_year="$year_epoch_now"; test_year>="$year_test_epoch"; test_year-- )); do
- if [[ "$test_year-02-29" =~ $reg_date ]]; then
- test_year_leap_day_epoch=$(format_date as_epoch "$test_year-02-29")
- if [[ "$_EPOCH_NOW" -ge "$test_year_leap_day_epoch" && "$test_epoch" -le "$test_year_leap_day_epoch" ]]; then
- let leaps leaps+=1
- fi
- fi
- done
- echo "$leaps" "$((test_epoch - leaps * _SEC_IN_DAY))"
- return 0
- }
- # ENTER A DATE TO CHECK BELOW
- my_date="2016-02-27"
- _EPOCH_NOW=$(format_date as_epoch "$my_date")
- function build_epoch_list () {
- # Calculate epoch_value for day-range & add to EPOCH_LIST array
- for range in "${RECENT_RANGES[@]}"; do
- if_leap=( $(leap_test $((_EPOCH_NOW - range * _SEC_IN_DAY )) ) )
- leap_list+=( "${if_leap[0]}" )
- EPOCH_LIST+=( "${if_leap[1]}" )
- done
- return 0
- }
- build_epoch_list
- printf "%s\n" "Current Search Start Date: $(format_date as_day "$_EPOCH_NOW")"
- printf "%-10s %-9s %-10s\n" "Range" "Leaps" "Start Date"
- printf "%s\n" "--------------------------------------"
- for i in "${!RECENT_RANGES[@]}"; do
- printf " %-10s %-9s %-12s\n" "${RECENT_RANGES[$i]}" "${leap_list[$i]}" "$(format_date as_day "${EPOCH_LIST[$i]}")"
- done
- printf "%s\n\n\n" "--------------------------------------"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement