home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume20 / pegboard / tfix.c < prev   
C/C++ Source or Header  |  1989-10-26  |  4KB  |  195 lines

  1. /*
  2.     tfix.c
  3. */
  4.  
  5. #include "peg.h"
  6.  
  7. /*
  8.     Convert two longs representing a target hour and minute into seconds
  9.     from the day boundary, and update the running count for that time.
  10.  
  11. */
  12. long fixhm( h, m )
  13. long h;    /* Hours    */
  14. long m;    /* Minutes    */
  15. {
  16.     extern struct tm today;
  17.     extern long duback;
  18.  
  19.     while( m >= 60 )    {
  20.         h += 1;
  21.         m -= 60;
  22.     }
  23.     /*
  24.         If the specified time has gone by assume invoker means tomorrow
  25.     */
  26.     if(( today.tm_year == localtime( &duback )->tm_year )
  27.     && ( today.tm_yday == localtime( &duback )->tm_yday )
  28.     && ( today.tm_hour > h ))
  29.         h += 24;
  30.  
  31.     dround( &duback );
  32.     return(( duback += (( m * 60 ) + ( h * 3600 ))));
  33. }
  34.  
  35. void dround( t )
  36. long *t;
  37. {
  38.     while( (*t) % 3600 )    /* Round back to hour    */
  39.         (*t)--;
  40.     while( localtime( t )->tm_hour )    /* Then to the day    */
  41.         (*t) -= 3600;
  42. }
  43.  
  44. /*
  45.     Convert 3 arguments (yy/mm/dd) into an offset in seconds from
  46.     the current date, and update the running count.
  47.  
  48.     Bugs: Ignores leap seconds
  49. */
  50. long fixymd( y, m, d )
  51. long y;    /* Year    */
  52. long m;    /* Month    */
  53. long d;    /* Day    */
  54. {
  55.     extern struct tm today;
  56.     extern long duback;
  57.     static int modays[] = { 31, 28, 31, 30,  31,  30,  31,  31,  30,  31,
  58.         30,  31 };
  59.     int i;
  60.  
  61.     /*
  62.         Deal with months from 0-11 (caller passes 1-12), check against bounds.
  63.     */
  64.     if( --m < 0 || m > 11 )
  65.         specerr( "illegal month" );
  66.     /*
  67.         Check if date gone by
  68.     */
  69.     if( y < today.tm_year
  70.     || ( y == today.tm_year && (( m == today.tm_mon && d < today.tm_mday ) ||
  71.        ( m < today.tm_mon))))
  72.         specerr( "illegal date" );
  73.     /*
  74.         Check day against bounds
  75.     */
  76.     if(((( ! ( y % 4 )) && ( y % 100 )) || ( ! ( y % 400 ))))
  77.             modays[1] = 29;
  78.     if( d < 0 || d > modays[m] )
  79.             specerr( "illegal day" );
  80.     /*
  81.         Convert mm/yy for target year into tm_yday
  82.     */
  83.     if((( ! ( y % 4 )) && ( y % 100 ))
  84.     || ( ! ( y % 400 )))
  85.         modays[1] = 29;
  86.     else
  87.         modays[1] = 28;
  88.     for( i = 0 ; i < m ; i++ )
  89.         d += modays[i];
  90.     d -= today.tm_yday + 1 ;
  91.  
  92.     /*
  93.         Count number of leap years between next year and the
  94.         target year.
  95.     */
  96.     for( i = today.tm_year + 1 ; i < y ; i++ )    {
  97.         if((( ! ( i % 4 )) && ( i % 100 )) || ( ! ( i % 400 )))
  98.             ++d;
  99.     }
  100.     /*
  101.         Get the years offset
  102.     */
  103.     y=(long)abs((int)( today.tm_year - y ));
  104.  
  105.     /*
  106.         If this is a leap year and the target year is not this year
  107.         then we need another day...
  108.     */
  109.     if(((( ! ( today.tm_year % 4 )) &&
  110.     ( today.tm_year % 100 )) || ( ! ( today.tm_year % 400 ))) && y )
  111.         ++d;
  112.     /*
  113.         Convert it to days then seconds
  114.     */
  115.     return( fixday((long)( y * 365 ) + d ));
  116. }
  117.  
  118. /*
  119.     Return some number of days converted to seconds, added to the
  120.     running total in duback.
  121.  
  122.     Externals: duback
  123.     Returns: duback
  124. */
  125. long fixday( d )
  126. long d;
  127. {
  128.     extern long duback;
  129.  
  130.     return( duback += ( d * 86400 ));
  131. }
  132.  
  133. /*
  134.     Check see if string starts with a valid month name.  Return
  135.     0 if not,  1-12 if so.
  136. */
  137. ismonth( s )
  138. char *s;
  139. {
  140.     extern int eod;
  141.     static char *months[] = { "jan", "feb", "mar", "apr", "may", "jun",
  142.                               "jul", "aug", "sep", "oct", "nov", "dec", "" };
  143.     int i;
  144.  
  145.     for( i = 0 ; *months[i] ; i++ )    {
  146.         if( ! strncmp( s, months[i], 3 ))
  147.             return( ++i );
  148.     }
  149.     return( 0 );
  150. }
  151.  
  152. /*
  153.     Check see if string starts with a valid day name.  Return
  154.     -1 if not, day of week (0 = sun) if so.
  155. */
  156. isday( s )
  157. char * s;
  158. {
  159.     static char *days[] = { "sun", "mon", "tue", "wed", "thu",
  160.         "fri", "sat", "" };
  161.     int i;
  162.  
  163.     for( i = 0 ; *days[i] ; i++ )    {
  164.         if( ! strncmp( s, days[i], 3 ))
  165.             return( i );
  166.     }
  167.     return( -1 );
  168. }
  169.  
  170. void copytime( t, n )
  171. struct tm *t;
  172. struct tm *n;
  173. {
  174.     n->tm_sec    = t->tm_sec;
  175.     n->tm_min    = t->tm_min;
  176.     n->tm_hour    = t->tm_hour;
  177.     n->tm_mday    = t->tm_mday;
  178.     n->tm_mon    = t->tm_mon;
  179.     n->tm_year    = t->tm_year;
  180.     n->tm_wday    = t->tm_wday;
  181.     n->tm_yday    = t->tm_yday;
  182.     n->tm_isdst    = t->tm_isdst;
  183. #ifndef    BSD
  184.     n->tm_tzadj    = t->tm_tzadj;
  185.     strcpy( n->tm_name, t->tm_name );
  186. #endif    /* BSD */
  187. }
  188.  
  189. showtime( t )
  190. long *t;
  191. {
  192.     printf( "%s", ctime( t ) );
  193. }
  194.  
  195.