home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / d / difftime.zip / DIFFTIME.C next >
C/C++ Source or Header  |  1993-03-18  |  2KB  |  99 lines

  1.  
  2.  
  3.  
  4. /*    difftime.c
  5.  *
  6.  *    Calculates the difference in hours and minutes (HH:MM format)
  7.  *    between a starting time and finish time as given on the
  8.  *    command line.
  9.  *
  10.  *    Usage:      difftime start_time finish_time
  11.  *    Example:  difftime 2359 420
  12.  *          04:21
  13.  *
  14.  *    Author:   Gene Kwiecinski
  15.  *    Date:      03-18-93
  16.  */
  17.  
  18.  
  19. #undef UNIX    /*  controls file name display in error strings  */
  20.  
  21.  
  22. /*  included header files  */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26.  
  27.  
  28. /*  external function declarations  */
  29.  
  30. extern void    exit();
  31. extern char    *itoa();
  32. extern int    fprintf();
  33.  
  34.  
  35. /*  error messages  */
  36.  
  37. char    *eusage = "usage:  %s start_time finish_time\n",
  38.     *ebadts = "%s:  bad start_time\n",
  39.     *ebadtf = "%s:  bad finish_time\n";
  40.  
  41.  
  42. /*  output format string  */
  43.  
  44. char    *fmtstr = "%02d:%02d\n";
  45.  
  46.  
  47.  
  48. /*  main function  */
  49.  
  50. main( argc, argv )
  51.     int    argc;
  52.     char    *argv[];
  53.     {
  54.     char    *pname;
  55.     int    ts, tf, hh, mm;
  56.  
  57. #if UNIX
  58.     pname = argv[0];        /*  for Unix  */
  59. #else
  60.     pname = "difftime";        /*  for DOS   */
  61. #endif
  62.  
  63.     /*  check usage  */
  64.     if( argc != 3 ){
  65.         fprintf( stderr, eusage, pname );
  66.         exit(1);
  67.         }
  68.  
  69.     /*  get start_time and finish_time  */
  70.     ts = atoi( argv[1] );
  71.     tf = atoi( argv[2] );
  72.  
  73.     /*  bounds checking  */
  74.     if( ts < 0 || ts > 2359 || ts/100 > 59 ){
  75.         fprintf( stderr, ebadts, pname );
  76.         exit(1);
  77.         }
  78.     if( tf < 0 || tf > 2359 || tf/100 > 59 ){
  79.         fprintf( stderr, ebadtf, pname );
  80.         exit(1);
  81.         }
  82.  
  83.     /*  calculate difference and print  */
  84.     hh = ( tf/100 - ts/100 );
  85.     mm = ( tf%100 - ts%100 );
  86.     if( mm < 0 )
  87.         hh--, mm += 60;
  88.     if( mm > 59 )
  89.         hh++, mm -= 60;
  90.     if( hh < 0 )
  91.         hh += 24;
  92.     if( hh > 24 )
  93.         hh -= 24;
  94.     fprintf( stdout, fmtstr, hh, mm );
  95.  
  96.     exit(0);
  97.     }
  98.  
  99.