Last active
January 10, 2020 00:58
-
-
Save lampholder/c3ae96e3c73bd735d4c04cfef4cfcee3 to your computer and use it in GitHub Desktop.
Very quick and dirty bash script to do something I need on a regular basis - outputing a sequence of days between an (inclusive) start date and an (exclusive) end date.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if (( $# < 2 )); then | |
echo "Usage:" | |
echo | |
echo "$0 1983-02-20 1983-03-13 [OUTPUT FORMAT] [--inclusive-end]" | |
echo "Generates sequential dates in the specified format between the start and end dates provided." | |
echo | |
echo "$0 1983-02-20 20 [OUTPUT FORMAT] [--inclusive-end]" | |
echo "Generates sequential dates in the specified format starting at the start date and continuing for the specified number of days." | |
echo | |
echo "In both modes of operation the generated dates are inclusive of the start date and exclusive of the end date, unless --inclusive-end is specified." | |
exit 1 | |
fi | |
function format_date() { | |
local START=$1 | |
local X=$2 | |
local FORMAT=$3 | |
if [[ $(uname -s) = "Darwin" ]]; then | |
echo `date -j -v +${X}d -f "%Y-%m-%d" ${START} +${FORMAT}` | |
else | |
echo `date -d "${START} ${X} days" +${FORMAT}` | |
fi | |
} | |
function days_between() { | |
local START=`format_date $1 0 +%s` | |
local END=`format_date $2 0 +%s` | |
local DAYS_BETWEEN=$((( $END - $START) / (60*60*24))) | |
echo $DAYS_BETWEEN | |
} | |
START=$1 | |
if echo $2 | egrep -q '^[0-9]+$'; then | |
DAYS=$2 | |
else | |
DAYS=`days_between $START $2` | |
fi | |
shift | |
shift | |
INCLUSIVE_END=0 | |
if [[ "$1" = "--inclusive-end" ]]; then | |
INCLUSIVE_END=1 | |
shift | |
elif [[ "$2" = "--inclusive-end" ]]; then | |
INCLUSIVE_END=1 | |
fi | |
FORMAT=${1:-%Y-%m-%d} | |
for (( i = 0; i < $DAYS + $INCLUSIVE_END; i++ )); do | |
echo `format_date $START $i $FORMAT` | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment