# ydate: A Bourne shell script that
# prints yestarday's date
# Output Form: Month Day Year
# Set the current month day and year.
month=`date +%m`
day=`date +%d`
year=`date +%y`
# Add 0 to month. This is a
# trick to make month an unpadded integer.
month=`expr $month + 0`
# Subtract one from the current day.
day=`expr $day - 1`
# If the day is 0 then determine the last
# day of the previous month.
if [ $day -eq 0 ]; then
# Find the preivous month.
month=`expr $month - 1`
# If the month is 0 then it is Dec 31 of
# the previous year.
if [ $month -eq 0 ]; then
month=12
day=31
year=`expr $year - 1`
# If the month is not zero we need to find
# the last day of the month.
else
case $month in
1|3|5|7|8|10|12) day=31;;
4|6|9|11) day=30;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=29
elif [ `expr $year % 100` -eq 0 ]; then
day=28
else
day=29
fi
else
day=28
fi
;;
esac
fi
fi
MNTH=$month
if [ $MNTH -lt 10 ]
then
month=0$month
fi
DAY=$day
if [ $DAY -lt 10 ]
then
day=0$day
fi
# Print the month day and year.
echo $month"/"$day"/"$year
exit 0
I believe I actually found this from a post at Tek-Tips, but I cannot remember who it was from. I made a few modifications to get the output to be the way I wanted it to be.
/* check for date in the form "Nov 19" or "19 Nov" */
found = 0;
for (i=0; i<12; i++)
{
if ((p = strstr(s, Months)) != NULL)
{
found = 1;
break;
}
}
if (found)
{
j = p - s;
if (j == 0)
{
mm = i+1;
sscanf(&p[4], "%d", &dd);
}
else
{
sscanf(s, "%d", &dd);
mm = i+1;
sscanf(&p[4], "%d", &yy);
}
if (yy == 0)
{
get_year(&yy);
}
goto done;
}
/* date is in numerical form YYYY-MM-DD */
if (strchr(s, '-') != NULL)
fmt_ymd = 1;
else
fmt_ymd = 0;
/* date is in numerical form MM/DD/YYYY */
p = strtok(s, delm);
if (!p) return 0;
a = atoi(p);
p = strtok(NULL, delm);
if (!p) return 0;
b = atoi(p);
p = strtok(NULL, delm);
if (!p) return 0;
c = atoi(p);
if (fmt_ymd || a > 1900)
{
yy = a;
mm = b;
dd = c;
}
else
{
dd = a;
mm = b;
yy = c;
}
done:
if (yy < 100 && yy >= 70)
yy += 1900;
else if (yy < 70)
yy += 2000;
sprintf(dat, "%04d%02d%02d", yy, mm, dd);
/*
* converts date from human format to Unixdate format
*/
int str2unixdate(char *sdate, int *udate)
{
char norm_date[9];
int y, m, d;
struct tm when;
time_t secs;
if (!parse_date(sdate, norm_date))
return 0;
if (!parse_normalized_date(norm_date, &y, &m, &d))
return 0;
memset(&when, 0, sizeof(when));
when.tm_year = y - 1900;
when.tm_mon = m - 1;
when.tm_mday = d;
if ((secs = mktime(&when)) == (time_t)-1)
return 0;
*udate = secs;
return 1;
}
int today(char *sdate2)
{
char sdate[12], stime[12], sdate1[12];
int y, m, d;
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.