Skip to content

Instantly share code, notes, and snippets.

@valvallow
Created December 14, 2011 11:32
Show Gist options
  • Save valvallow/1476229 to your computer and use it in GitHub Desktop.
Save valvallow/1476229 to your computer and use it in GitHub Desktop.
date2bin
#!/bin/sh
# date2bin
this_usage () {
echo 'Usage: date2bin [-ndf]'
echo ' n : number of days count as integer'
echo ' f : display flat line'
echo ' d : start date as integer (ex. $ date +%Y%m%d)'
echo ' h : display this usage'
exit 0
}
fmt_date (){
echo "obase=$1;"`date -d "$2" "$3"` | bc
}
VALUE_LIMIT=1
VALUE_START_DATE=`date +%Y%m%d`
FLG_FLAT="FALSE"
#
# parse options
#
while getopts hn:fd: OPT
do
case $OPT in
"n" ) VALUE_LIMIT="$OPTARG" ;;
"f" ) FLG_FLAT="TRUE" ;;
"d" ) VALUE_START_DATE="$OPTARG" ;;
"h" ) this_ussage ;;
esac
done
#
# body
#
i=0
CUR_DATE="$VALUE_START_DATE"
if [ $FLG_FLAT = "TRUE" ] ; then
echo 'count,ymd,epoch,ymd_bin,md_bin,epoch_bin,ymd_oct,md_oct,epoch_oct,ymd_hex,md_hex,epoch_hex'
else
echo '--------------------'
fi
while [ $i -ne $VALUE_LIMIT ] ;
do
CUR_DATE_YMD=`date -d $CUR_DATE +%Y-%m-%d`
CUR_DATE_EPOCH=`date -d $CUR_DATE +%s`
CUR_DATE_YMD_BIN=`fmt_date 2 "$CUR_DATE" '+%Y%m%d'`
CUR_DATE_MD_BIN=`fmt_date 2 "$CUR_DATE" '+%m%d'`
CUR_DATE_EPOCH_BIN=`fmt_date 2 "$CUR_DATE" '+%s'`
CUR_DATE_YMD_OCT=`fmt_date 8 "$CUR_DATE" '+%Y%m%d'`
CUR_DATE_MD_OCT=`fmt_date 8 "$CUR_DATE" '+%m%d'`
CUR_DATE_EPOCH_OCT=`fmt_date 8 "$CUR_DATE" '+%s'`
CUR_DATE_YMD_HEX=`fmt_date 16 "$CUR_DATE" '+%Y%m%d'`
CUR_DATE_MD_HEX=`fmt_date 16 "$CUR_DATE" '+%m%d'`
CUR_DATE_EPOCH_HEX=`fmt_date 16 "$CUR_DATE" '+%s'`
if [ $FLG_FLAT = "TRUE" ] ; then
echo "$i,$CUR_DATE_YMD,$CUR_DATE_EPOCH,$CUR_DATE_YMD_BIN,$CUR_DATE_MD_BIN,$CUR_DATE_EPOCH_BIN,$CUR_DATE_YMD_OCT,$CUR_DATE_MD_OCT,$CUR_DATE_EPOCH_OCT,$CUR_DATE_YMD_HEX,$CUR_DATE_MD_HEX,$CUR_DATE_EPOCH_HEX"
else
echo -n 'count : '; echo "$i"
echo -n '+%Y-%m-%d : '; echo "$CUR_DATE_YMD"
echo -n '+%s : '; echo "$CUR_DATE_EPOCH"
echo -n '+%Y%m%d -> bin : '; echo "$CUR_DATE_YMD_BIN"
echo -n '+%m%d -> bin : '; echo "$CUR_DATE_MD_BIN"
echo -n '+%s -> bin : '; echo "$CUR_DATE_EPOCH_BIN"
echo -n '+%Y%m%d -> oct : '; echo "$CUR_DATE_YMD_OCT"
echo -n '+%m%d -> oct : '; echo "$CUR_DATE_MD_OCT"
echo -n '+%s -> oct : '; echo "$CUR_DATE_EPOCH_OCT"
echo -n '+%Y%m%d -> hex : '; echo "$CUR_DATE_YMD_HEX"
echo -n '+%m%d -> hex : '; echo "$CUR_DATE_MD_HEX"
echo -n '+%s -> hex : '; echo "$CUR_DATE_EPOCH_HEX"
echo '--------------------'
fi
i=`expr "$i" + 1`
CUR_DATE=`date -d"$VALUE_START_DATE $i"day +%Y%m%d`
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment