home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / backups / convdate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-31  |  1.2 KB  |  54 lines

  1. #ifndef lint
  2.     static char RCSid[] = "$Header: convdate.c,v 1.1 85/05/22 15:53:36 scooter Exp $";
  3. #endif lint
  4.  
  5. /*
  6.  * Module to convert date from mm/dd/yy format to a "tm" structure
  7.  */
  8.  
  9.  
  10. #include    <stdio.h>
  11. #include    <sys/time.h>
  12. #include    <tzfile.h>
  13.  
  14. static int    dmsize[13] =
  15.     { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
  16.  
  17. struct tm *
  18. convdate(month,day,year)
  19. int month,day,year;
  20. {
  21.     int i;
  22.     struct tm *today;
  23.     struct timeval tv;
  24.     struct timezone tz;
  25.  
  26.     gettimeofday(&tv,&tz);
  27.     today = localtime(&tv.tv_sec);
  28.  
  29.     if (year == 0)year = today->tm_year;
  30.     if (month == 0)month = today->tm_mon+1;
  31.     if (day == 0)day = today->tm_mday;
  32.  
  33.     if (year < 1000)year += 1900;
  34.  
  35.     tv.tv_sec = 0;
  36.  
  37.     if (isleap(year) && month > 2)
  38.         ++tv.tv_sec;
  39.     for (--year;year >= EPOCH_YEAR;--year)
  40.         tv.tv_sec += isleap(year) ? DAYS_PER_LYEAR : DAYS_PER_NYEAR;
  41.     while (--month)
  42.         tv.tv_sec += dmsize[month];
  43.     tv.tv_sec += day - 1;
  44.     tv.tv_sec = HOURS_PER_DAY * tv.tv_sec;
  45.     tv.tv_sec = MINS_PER_HOUR * tv.tv_sec;
  46.     tv.tv_sec = SECS_PER_MIN * tv.tv_sec;
  47.     
  48.     /* Now convert to local timezone */
  49.     tv.tv_sec += (long)tz.tz_minuteswest*SECS_PER_MIN;
  50.     if (localtime(&tv.tv_sec)->tm_isdst) tv.tv_sec -= SECS_PER_HOUR;
  51.  
  52.     return(localtime(&tv.tv_sec));
  53. }
  54.