//=================================================== file = clktosec.c =====
//=  Program to convert HH:MM:SS cumulative timestamps to seconds           =
//=   - Works for both military and regular times                           =
//===========================================================================
//=  Notes:                                                                 =
//=    1) Input from input file "in.dat" to stdin (see example below)       =
//=        * Comments are bounded by "&" characters at the beginning and    =
//=          end of the comment block                                       =
//=    2) Output is to stdout                                               =
//=    3) Outputs delta or cumulative time                                  =
//=    3) Must manually set MILITARY_TIME and DELTA_TIME constants          =
//=-------------------------------------------------------------------------=
//= Example "in.dat" file (with MILITARY_TIME and DELTA_TIME = TRUE)        =
//=                                                                         =
//=   & --- Sample input file of clock value time stamps --- &              =
//=   12:44:00                                                              =
//=   12:45:00                                                              =
//=   12:55:10                                                              =
//=   1:05:00                                                               =
//=   11:10:30                                                              =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat"):                                    =
//=                                                                         =
//=   & ----------------------------------------- clktosec.c ----- &        =
//=   45840                                                                 =
//=   60                                                                    =
//=   610                                                                   =
//=   590                                                                   =
//=   36330                                                                 =
//=   & ---------------------------------------------------------- &        =
//=-------------------------------------------------------------------------=
//=  Build:  bcc32 clktosec.c                                               =
//=-------------------------------------------------------------------------=
//=  Execute: clktosec < in.dat                                             =
//=-------------------------------------------------------------------------=
//=  Author: Kenneth J. Christensen                                         =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (06/03/02) - Genesis                                      =
//===========================================================================

//----- Include files -------------------------------------------------------
#include <stdio.h>                 // Needed for printf() and feof()
#include <stdlib.h>                // Needed for atof()
#include <string.h>                // Needed for strcmp()

//----- Defines -------------------------------------------------------------
#define FALSE                0     // Boolean false
#define TRUE                 1     // Boolean true
#define MILITARY_TIME    FALSE     // Set to TRUE if 24-hour time input
#define DELTA_TIME        TRUE     // Set to TRUE if delta time output

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  int       time;                  // Current time in int seconds from midnight
  int       time_last;             // Last time
  int       diff_time;             // Time between this and last time
  int       sum_time;              // Sum of time
  char      temp_string[256];      // Temporary string variable

  // Output banner
  printf("& ----------------------------------------- clktosec.c ----- & \n");

  // Loop for entire input file
  sum_time = time_last = 0;
  while(1)
  {
    // Input a value
    scanf("%s", temp_string);

    // This handles a comment bounded by "&" symbols
    if (strcmp(temp_string, "&") == 0)
    {
      do
      {
        scanf("%s", temp_string);
      } while (strcmp(temp_string, "&") != 0);
      scanf("%s", temp_string);
    }

    // Check if end-of-file
    if (feof(stdin)) break;


    // Convert the clock value to integer seconds since midnight
    time = 3600 * atoi(strtok(temp_string, ":")) +
             60 * atoi(strtok(NULL, ":")) +
                  atoi(strtok(NULL, ":"));

    // Compute time difference (with adjustments as needed)
    if (time > time_last)
      diff_time = time - time_last;
    else if ((time < time_last) && (MILITARY_TIME == FALSE))
      diff_time = (time + 3600 * 12) - time_last;
    else if ((time < time_last) && (MILITARY_TIME == TRUE))
      diff_time = (time + 3600 * 24) - time_last;
    time_last = time;

    // Update cumulative time
    sum_time = sum_time + diff_time;

    // Ouput delta or cumulative time
    if (DELTA_TIME == TRUE)
      printf("%d \n", diff_time);
    else
      printf("%d \n", sum_time);
  }

  // Output closing message
  printf("& ---------------------------------------------------------- & \n");
}

