« Changing a lightbulb.... | Main | Promotion by Merit? »

February 22, 2007

Yesterday / Tomorrow (in perl)

#!/usr/bin/perl
$one_day = 60*60*24 ;
$today = localtime(time) ;
print "today: $today\n" ;
$tomorrow = localtime(time+$one_day) ;
print "tomorrow: $tomorrow\n" ;
$yesterday = localtime(time-$one_day) ;
print "yesterday: $yesterday\n" ;

Gnu date makes this easy:
date -d "tomorrow"

Posted by Ozguru at February 22, 2007 06:00 AM

Comments

More information at The UNIX Forums FAQ.

Posted by: Ozguru [TypeKey Profile Page] at February 23, 2007 07:29 AM

here is yesterday in shell, not sure if its the most elegent way but it works!!


#!/bin/sh

DAY=`date +%d`
MONTH=`date +%m`
YEAR=`date +%Y`

DAY=$((DAY - 1))
if (( DAY <= 0 )) ;then
MONTH=$((MONTH - 1))
if (( MONTH == 0 )) ;then
YEAR=$((YEAR - 1))
MONTH=12
fi
set -A days `cal $MONTH $YEAR`
xday=${days[$(( ${#days[*]}-1 ))]}
DAY=$((xday + DAY))
fi

if [ $DAY -lt 10 ]; then
DAY="0$DAY"
fi

echo "${YEAR}${MONTH}${DAY}"

Posted by: daniel at March 3, 2007 05:39 AM