"yesterdate" function?

Kent Landfield kent at ssbell.UUCP
Fri Jan 6 15:00:52 AEST 1989


In article <19100001 at hpficad.HP.COM> mcr at 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 at ssbell
1404 Ft. Crook Rd. South     INTERNET: kent%ssbell.uucp at uunet.uu.net
Bellevue, NE. 68005-2969     FAX:    (402) 291-4362



More information about the Comp.unix.questions mailing list