[comp.unix.questions] "yesterdate" function?

mcr@hpficad.HP.COM (Matt Reprogle) (01/04/89)

I need a way to get yesterday's date in YYMMDD format in a Unix script and put
it in a file.   I don't want to write a C program implementing an entire
calendar to do it. 

Can anyone help?  

Matt Reprogle

{everywhere}!hplabs!hpfcla!mcr

arnold@emory.uucp (Arnold D. Robbins {EUCC}) (01/05/89)

In article <19100001@hpficad.HP.COM> mcr@hpficad.HP.COM (Matt Reprogle) writes:
>I need a way to get yesterday's date in YYMMDD format in a Unix script and put
>it in a file.   I don't want to write a C program implementing an entire
>calendar to do it. 
>
>Can anyone help?  

This is too hard to pass up.

	#include <time.h>
	#define ONEDAY	(24 * 60 * 60)	/* seconds in a day */

	main ()
	{
		long yesterday;
		struct tm* then;

		(void) time (& yesterday);
		yesterday -= ONEDAY;
		then = localtime (& yesterday);
		(void) printf ("%d%02d%02d\n", then->tm_year,
			then->tm_mon, then->tm_mday);
		return 0;
	}
-- 
"Unix is a Registered  | Arnold Robbins -- Emory University Computing Center
Bell of AT&T Trademark | DOMAIN: arnold@unix.cc.emory.edu		
Laboratories."         | UUCP: gatech!emory!arnold	PHONE:	+1 404 727-7636
        -- Donn Seeley | BITNET: arnold@emoryu1		FAX:	+1 404 727-2599

kjk@pbhyf.PacBell.COM (Ken Keirnan) (01/05/89)

In article <19100001@hpficad.HP.COM> mcr@hpficad.HP.COM (Matt Reprogle) writes:
>
>I need a way to get yesterday's date in YYMMDD format in a Unix script and put
>it in a file.   I don't want to write a C program implementing an entire
>calendar to do it. 
>
>Can anyone help?  
>
>Matt Reprogle
>
>{everywhere}!hplabs!hpfcla!mcr


If you are using System V, this little program will do what you want.  If
you are using BSD (or some other variant :-)) you are on your own.


-------- CUT HERE -------- CUT HERE -------- CUT HERE -------- CUT HERE 
/*
 * Print yesterdays date in format YYMMDD
 */

#include <time.h>

#define ONEDAY	86400L	/* seconds in a day */

main()
{
	register struct tm *tp;
	long t, time();

	t = time((long *)0) - ONEDAY;
	tp = localtime(&t);

	printf("%.2d%.2d%.2d\n", tp->tm_year, tp->tm_mon+1, tp->tm_mday);
	return(0);
}

-------- CUT HERE -------- CUT HERE -------- CUT HERE -------- CUT HERE 

Ken Keirnan


-- 

Ken Keirnan - Pacific Bell - {att,bellcore,sun,ames,pyramid}!pacbell!pbhyf!kjk
  San Ramon, California	                    kjk@pbhyf.PacBell.COM

mcr@hpficad.HP.COM (Matt Reprogle) (01/05/89)

To all who emailed me a response, thanks.   Someone sent me a program that
does even more than I asked for and it works great.  No more responses are
necessary.

Matt Reprogle

stu@jpusa1.UUCP (Stu Heiss) (01/05/89)

In article <3562@emory.uucp> arnold@emory.UUCP (Arnold D. Robbins {EUCC}) writes:
-In article <19100001@hpficad.HP.COM> mcr@hpficad.HP.COM (Matt Reprogle) writes:
->I need a way to get yesterday's date in YYMMDD format in a Unix script and put
->it in a file.   I don't want to write a C program implementing an entire
->calendar to do it. 
->
->Can anyone help?  
-
-This is too hard to pass up.
-[short c program deleted]

add 24 to your tz offset (example 6 for cdt; 6+24=30) and do
	$ TZ=30 date +%y%m%d
if you don't know your tz offset do:
	$ echo $TZ
-- 
Stu Heiss {gargoyle.uchicago.edu,uunet.uu.net,spl1,ddsw1}!jpusa1.uucp!stu

guy@auspex.UUCP (Guy Harris) (01/05/89)

>If you are using System V, this little program will do what you want.  If
>you are using BSD (or some other variant :-)) you are on your own.

If you are using BSD, that little program will do what you want.

bill@bilver.UUCP (bill vermillion) (01/05/89)

In article <19100001@hpficad.HP.COM> mcr@hpficad.HP.COM (Matt Reprogle) writes:
>
>I need a way to get yesterday's date in YYMMDD format in a Unix script and put
>it in a file.   I don't want to write a C program implementing an entire
>calendar to do it. 
>
>Can anyone help?  
>
>Matt Reprogle
>
>{everywhere}!hplabs!hpfcla!mcr

This came by the net awhile back. It might help.

############################################################################
# whenis : find out date relative to today : Andrew Beattie 20/5/87        #
# mcvax!ukc!reading!riddle!andrew                                          #
# andrew@sphinx.co.uk                                                      #
############################################################################
# I place this program in the public domain but please keep this header    #
# on it - I want the fame and glory!                                       #
############################################################################

if [ $# = 0 ]
then
	echo 'whenis today'
	echo 'whenis [next|last|this] sunday|monday|tuesday|wednesday|th...'
	exit
fi

# store current daylight adjustment
light=`echo $TZ | cut -c4`
# find day of week and put '-' in front of it
now=`date '+-%w'`
for i 
do
	case $i in
	today) now=0 ;;
	next) now="$now +7" ;;
	last) now="$now -7" ;;
	tomorrow) now="1" ;;
	yesterday) now="-1" ;;
	sunday) now="$now +0" ;;
	monday) now="$now +1" ;;
	tuesday) now="$now +2" ;;
	wednesday) now="$now +3" ;;
	thursday) now="$now +4" ;;
	friday) now="$now +5" ;;
	saturday) now="$now +6" ;;
	esac
done
# work out number of hours adjustment
adj=`echo "$light + ( -24 * ( $now ) )" |bc`
# now for the magic - adjust the time zone by the given number
# of hours and let date do the hard work.
TZ=GMT$adj date


-- 
Bill Vermillion - UUCP: {uiucuxc,hoptoad,petsd}!peora!rtmvax!bilver!bill
                      : bill@bilver.UUCP

kent@ssbell.UUCP (Kent Landfield) (01/06/89)

In article <19100001@hpficad.HP.COM> mcr@hpficad.HP.COM (Matt Reprogle) writes:
>
>I need a way to get yesterday's date in YYMMDD format in a Unix script and put
>it in a file.   I don't want to write a C program implementing an entire
>calendar to do it. 
>
>Can anyone help?  
>
I think that the following code will serve your needs. And I didn't
need to an entire calendar to do it. ;-)

------------------------- yesterdate.c ----------------------
#include <time.h>
 
#define SEC_PER_DAY 86400L            /* total seconds per day */

main()
{
    long clock, time();
    struct tm *yesterday, *localtime();

    clock = time((long *) 0);   /* get the current time */

    /*
    ** subtract 24 hours worth of seconds from 
    ** the total seconds since the epoch.
    */
    clock -= SEC_PER_DAY;

    /*
    ** convert yesterday's second value into a 
    ** tm structure in preparation for output.
    */
    yesterday = localtime(&clock);

    /*
    ** print the string containing in the format 
    ** requested YYMMDD and zero padded if single 
    ** digit returned.   
    */
    (void) printf("%.2d%.2d%.2d\n",
        yesterday->tm_year, yesterday->tm_mon + 1,
        yesterday->tm_mday);

    return(0);
}
---------------------------------------------------- 
Kent Landfield               Phone:    (402) 291-8300 
Sterling Software FSG/IMD    UUCP:   kent@ssbell
1404 Ft. Crook Rd. South     INTERNET: kent%ssbell.uucp@uunet.uu.net
Bellevue, NE. 68005-2969     FAX:    (402) 291-4362

andrew@riddle.UUCP (Andrew Beattie) (01/06/89)

In article <19100001@hpficad.HP.COM> mcr@hpficad.HP.COM (Matt Reprogle) writes:
>
>I need a way to get yesterday's date in YYMMDD format in a Unix script 

Yes, I posted a script which does this a few months ago - If enough other 
people tell me that they missed it then I will post again.
(I'm mailing you anyway)
+---------------------------------+-----------------+
| Andrew Beattie                  | Sphinx Limited  |
| Internal Systems Engineer       | Foundation Park |
| mcvax!ukc!reading!riddle!andrew | Maidenhead      |
| andrew@sphinx.co.uk             | England         |
| +44 62882 2266                  | SL6 3UD         |
+---------------------------------+-----------------+

pss@unh.UUCP (Paul S. Sawyer) (01/07/89)

In article <1033@jpusa1.UUCP>, stu@jpusa1.UUCP (Stu Heiss) writes:
> 
> add 24 to your tz offset (example 6 for cdt; 6+24=30) and do
> 	$ TZ=30 date +%y%m%d
> Stu Heiss {gargoyle.uchicago.edu,uunet.uu.net,spl1,ddsw1}!jpusa1.uucp!stu

On SysV.2, anyway, you need:

 	$ TZ=CST30 date +%y%m%d
or
 	$ TZ=CST30CDT date +%y%m%d	# covering daylight savings periods

(Which is probably what you meant anyway  B-)
Is there ANY (reasonable) function that CAN'T be (reasonably) implemented in
/bin/sh (programming) "without writing a [insert language here] program"?

-- 
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Paul S. Sawyer              uunet!unh!unhtel!paul     paul@unhtel.UUCP
UNH Telecommunications
Durham, NH  03824-3523      VOX: 603-862-3262         FAX: 603-862-2030

rdm2@nvuxr.UUCP (R McBurnett) (01/13/89)

In article <353@bilver.UUCP> bill@bilver.UUCP (bill vermillion) writes:
>In article <19100001@hpficad.HP.COM> mcr@hpficad.HP.COM (Matt Reprogle) writes:
>>
>>I need a way to get yesterday's date in YYMMDD format in a Unix script and put
>>it in a file.   I don't want to write a C program implementing an entire
>>calendar to do it. 
>>
>>Can anyone help?  
>>
>>Matt Reprogle
>>
>>{everywhere}!hplabs!hpfcla!mcr

try this

	$ date
	Thu Jan 12 13:21:36 EST 1989
	$ TZ=EST29EDT date
	Wed Jan 11 13:21:42 EST 1989

putting in your normal TZ parameter (5 for east coast) +24 goes back 24
hours.

Yes this works for tomorrow too

	$ TZ=EST-19EDT date
	Fri Jan 13 13:22:44 EST 1989

you can use the date command to get the YYMMDD also

	$ date "+%y%m%d"
	890112

and in combination with the TZ stuff.

-- 
Roe D McBurnett III	Bellcore		|these are my own
(201)758-2333	 rdm2@nvuxr.cc.bellcore.com	|rantings not Bellcore's

jpr@dasys1.UUCP (Jean-Pierre Radley) (01/14/89)

In article <19100001@hpficad.HP.COM> mcr@hpficad.HP.COM (Matt Reprogle) writes:
>I need a way to get yesterday's date in YYMMDD format in a Unix script and put
>it in a file.   I don't want to write a C program implementing an entire
>calendar to do it. 

The C program exists: "datecalc", written by Fred Buck, and available
in the LIBraries of the TANGENT Forum on Compuserve. It'll solve that
problem, and do dozens of other little date routines. Anyone not on CIS,
E-mail me.

But to just solve the one little problem posed by Matt:

	GMTDIFF=`echo $TZ | sed 's/[^0-9]*//g'`
	YESTGMTDIFF=`expr $GMTDIFF + 24`
	TZY=`echo $TZ | sed 's/[0-9][0-9]*/'$YESTGMTDIFF'/'`
	YESTERDATE=`TZ=$TZY date +%y%m%d`

-- 
Jean-Pierre Radley		Honi soit		jpr@dasys1.UUCP
New York, New York		qui mal			...!hombre!jpradley!jpr
CIS: 76120,1341			y pense			...!hombre!trigere!jpr