GNU date for shell script lovers

GNU date has intersting functionality which are similar to localtime(3) and mktime(3).

Well,
    date -d @_number_

is to parse _number_ as seconds from the epoch, on the other hand
    date +%s

it to obtain seconds from the epoch. If you combine these two, you can make date based loop in BASH script easily:
# $1: start date
# $2: end date
# $3: date format (optional)
print_date_range () {
for (( d=$(date -d $1 +%s);
d<=$(date -d $2 +%s);
d+=86400)); do
date -d @$d ${3++"$3"}
done
}

print_date_range is a function to display date between specified range. You may specify date format in strftime(3) format. e.g.:
$ print_date_range now tomorrow
Wed Mar 7 12:06:32 JST 2007
Thu Mar 8 12:06:32 JST 2007

$ print_date_range "2007-03-07" "2007-03-08" "%Y/%m/%d"
2007/03/07
2007/03/08
コメントを投稿

このブログの人気の投稿

シェル操作課題への回答

連想配列 - Bash Advent Calendar - Day 5

今、永福町のピザ戦争が熱い!