Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date script

Status
Not open for further replies.

navink123

Programmer
Sep 2, 2003
21
US
I need a script to pull yesterday's date each time when I run it. Right now I have

mydate=`date '+%Y%m%d'`.

But it pulls todays date. How can I pull the date of the day before.

Thanks,
Navin
 
A Keyword Search on "yesterday" came up with some threads. This one thread822-445663 has some scripts. And there is an FAQ: faq80-953.
 
navink123,

The script I use is:

#!/bin/sh

# 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.

John
 


/*
* dateutil.c
*
* Manipulates dates
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

//#define DEBUG
//#define DEBUG2

char *Months[] = {
&quot;Jan&quot;, &quot;Feb&quot;, &quot;Mar&quot;, &quot;Apr&quot;, &quot;May&quot;, &quot;Jun&quot;,
&quot;Jul&quot;, &quot;Aug&quot;, &quot;Sep&quot;, &quot;Oct&quot;, &quot;Nov&quot;, &quot;Dec&quot; };

int Date_Format=0;

int get_year(int *yy)
{
struct tm *tm;
time_t ltime;

time( &ltime );
tm = localtime( &ltime );

*yy = tm->tm_year;
if (*yy < 1900)
*yy += 1900;

return 1;
}

int get_datetime(char *sdate, char *stime)
{
struct tm *tm;
time_t ltime;
int yy;

time( &ltime );
tm = localtime( &ltime );

yy = tm->tm_year;
if (yy < 1900)
yy += 1900;

sprintf(sdate, &quot;%02d/%02d/%04d&quot;,
tm->tm_mday, tm->tm_mon+1, yy);
sprintf(stime, &quot;%02d:%02d:%02d&quot;,
tm->tm_hour, tm->tm_min, tm->tm_sec);
return 1;
}

/*
* input data is in YYYYMMDD format. Returns y,m,d as integers
*/
int parse_normalized_date(char *sdate, int *y, int *m, int *d)
{
char str[9];

if (strlen(sdate) != 8)
return 0;

memcpy(str, sdate, 4);
str[4] = 0;
*y = atoi(str);

memcpy(str, &sdate[4], 2);
str[2] = 0;
*m = atoi(str);

memcpy(str, &sdate[6], 2);
str[2] = 0;
*d = atoi(str);

return 1;
}

int parse_date(char *dat_in, char *dat)
{
const char delm[] = &quot;-/. &quot;;
char s[32], *p;
int fmt_ymd, i, j, found, a, b, c, yy, mm, dd;

#ifdef DEBUG2
printf(&quot;parse_date(%s)\n&quot;, dat_in);
#endif
strncpy(s, dat_in, 31);
s[31] = 0;

/* check for date in the form &quot;Nov 19&quot; or &quot;19 Nov&quot; */
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], &quot;%d&quot;, &dd);
}
else
{
sscanf(s, &quot;%d&quot;, &dd);
mm = i+1;
sscanf(&p[4], &quot;%d&quot;, &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, &quot;%04d%02d%02d&quot;, yy, mm, dd);

#ifdef DEBUG2
printf(&quot;(%s)\n&quot;, dat);
#endif
return 1;
}

/*
* 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;

get_datetime(sdate, stime);
parse_date(sdate, sdate1);
parse_normalized_date(sdate1, &y, &m, &d);

if (Date_Format == 0)
sprintf(sdate2, &quot;%04d-%02d-%02d&quot;, y, m, d);
else
else
sprintf(sdate2, &quot;%02d/%02d/%04d&quot;, d, m, y);

return 1;
}

int date_add(char *sdate, int days, char *sdate2)
{
char norm_date[9];
int y, m, d;
struct tm when, *newtime;
time_t secs, secs2;

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;

secs2 = secs + days*1440*60;
newtime = localtime(&secs2);

if (Date_Format == 0)
sprintf(sdate2, &quot;%04d-%02d-%02d&quot;,
newtime->tm_year+1900, newtime->tm_mon+1, newtime->tm_mday);
else
sprintf(sdate2, &quot;%02d/%02d/%04d&quot;,
newtime->tm_mday, newtime->tm_mon+1, newtime->tm_year+1900);

return 1;

}


void usage(char *progname)
{
printf(&quot;usage: %s <operation> [<arguments> ...]\n&quot;, progname);
printf(&quot;operations:\n&quot;);
printf(&quot; unix <date> - returns unix date\n&quot;);
printf(&quot; add <date> <days> - adds days to given date\n&quot;);
printf(&quot; today [<days>] - adds days to today's date (if days not specified, returns today's date)\n&quot;);
printf(&quot;format:\n&quot;);
printf(&quot; <date> : YYYY-MM-DD or DD/MM/YY[YY] or DD.MM.YY[YY]\n&quot;);
printf(&quot; <days> : integer (may be negative)\n&quot;);
}
int main(int argc, char *argv[])
{
int udate, days;
char sdate[12], sdate0[12];

if (argc < 2)
goto err;

if (argc == 3 && strcmp(argv[1], &quot;unix&quot;) == 0)
{
udate = 0;
if (!str2unixdate(argv[2], &udate))
goto err;
fprintf(stdout, &quot;%d\n&quot;, udate);
}
else if (argc == 4 && strcmp(argv[1], &quot;add&quot;) == 0)
{
days = atoi(argv[3]);
if (!date_add(argv[2], days, sdate))
goto err;
fprintf(stdout, &quot;%s\n&quot;, sdate);
}
else if ((argc == 2 || argc == 3) && strcmp(argv[1], &quot;today&quot;) == 0)
{
if (argc == 3)
days = atoi(argv[2]);
else
days = 0;

if (!today(sdate0))
goto err;
if (!date_add(sdate0, days, sdate))
goto err;
fprintf(stdout, &quot;%s\n&quot;, sdate);
}
else
goto err;

return 0;

err:
usage(argv[0]);
return 1;
}












=-=-=-=-=-=-=-=-=-=-=-=
Unix Systems Engineer
=-=-=-=-=-=-=-=-=-=-=-=
 
or install GNU's version of &quot;date&quot; and use &quot;date yesterday&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top