/************************************************************************* * Copyright (C) 1995 Embbedded Solutions * All rights reserved * * file: src\times.c * * purpose: This module contains routines associated with the * time calculations * * As the storage and display of a time is machine dependent so * these routines may need to be altered * * functions * time_a - Convert time to ascii * time_fa - Convert time to ascii * conv_time - Convert hh,mm,ss to time * get_time - Read/Edit time from user * * programmer: David Purdie * * revision date by reason * 17-Oct-88 DDP Added new routine time_fa() * 00.0 27/01/95 DDP Tidies up the program and formatted the file * 01.0 18/09/2005 DDP Allow editing of time fields * Simplied the conversion of text to time * **************************************************************************/ #include #include #include "consts.h" #include "structs.h" #include "proto.h" /*======================================================================== * * Convert time to ascii * * Purpose: * This routine will convert the time ( as held in tim ) into an ascii * string. The address of the string is returned to the user. The * string is internally static and is of a fixed format * * The string format is * * HH:MM:SS(null) - if the time is valid. * ** ** **(null) - if the time is unknown * * * Parameters: * tim Time to convert * * Returns: * Address of an internal buffer that holds the ascii time * *========================================================================*/ char *time_a( time_t tim ) { static char buf[] = "00:00:00"; /* Hold return string here */ static char invalid[] = "** ** **"; /* Invalid string */ time_t hours, mins, secs; if( tim >= 0 && tim < 359999L ) { hours = tim / 3600L; /* Calc the hours */ tim -= hours * 3600L; /* secs left */ mins = tim / 60L; tim -= mins * 60L; secs = tim; sprintf( buf, "%2.2ld:%2.2ld:%2.2ld", hours, mins, secs ); return ( buf ); } else return ( invalid ); } /*======================================================================== * * Convert time to ascii * * Purpose: * This routine will convert the time ( as held in tim ) into an ascii * string. The address of the string is returned to the user. The * string is internally static and is of a fixed format * * The string format is * * HH:MM:SS(null) - if the time is valid. * -- -- --(null) - if the time is unknown and "flag" is true * ** ** **(null) - if the time is unknown and flag is false * * * Parameters: * tim Time to convert * flag Control display if time is not valid * TRUE "-- -- --" * FALSE "** ** **" * * Returns: * Address of an internal buffer that holds the ascii time * *========================================================================*/ char *time_fa( time_t tim, char flag ) { static char buf1[] = "00:00:00"; /* Hold return string here */ static char finvalid[] = "** ** **"; /* Invalid string */ static char tinvalid[] = "-- -- --"; time_t hours, mins, secs; if( tim >= 0 && tim < 359999L ) { hours = tim / 3600L; /* Calc the hours */ tim -= hours * 3600L; /* secs left */ mins = tim / 60L; tim -= mins * 60L; secs = tim; sprintf( buf1, "%2.2ld:%2.2ld:%2.2ld", hours, mins, secs ); return ( buf1 ); } else if( flag ) { return ( tinvalid ); } else { return ( finvalid ); } } /*======================================================================== * * Convert hh,mm,ss to time * * Purpose: * This function is convert hours, mins and seconds into the internal * format of time * * Parameters: * hh hours 0.. * mm minutes 0..59 * ss Seconds 0..59 * * Returns: * Time * *========================================================================*/ time_t conv_time( int hh, int mm, int ss ) { time_t time; /* The final result */ time = ( ( ( time_t ) hh * 60L ) + ( time_t ) mm ) * 60L + ( time_t ) ss; return ( time ); } /*======================================================================== * * Read/Edit time from user * * Purpose: * This function is called accept a time field from user and allow * the operator to edit the time as it is entered. * * Parameters: * tim - Time to edit * * Returns: * Time entered * Will return an illegal time if the entry is not complete * *========================================================================*/ time_t get_time(time_t utim) { char c; /* One of those */ int i = 0; /* Counts user input */ int maxi = 0; /* Max user input */ static char buf[] = "00:00:00"; /* Build up a string here */ static char max_digit[] = "29:59:59"; /* Maximum digit's allowed in entry */ int hh = 0, mm = 0, ss = 0; /* ** Insert the base time into the edit buffer ** Invalid times are treated as 00:00:00 */ if ( utim == -1L ) { utim = 0; } { char * ctime; ctime = time_a(utim); if ( ctime[0] != '*' ) memcpy( buf, ctime, sizeof(buf)-1); else memcpy( buf, "00:00:00", sizeof(buf)-1); } /* ** Edit the string */ while( TRUE ) { c = getinp(); if( c == ABORT_CHAR ) { i = 0; break; } if( c == '\n' || c == '\t' ) break; if( ( c == rubout ) && i > 0 ) /* rubout character */ { i--; if( max_digit[i] == ':' ) { printf( "\010:\010" ); buf[i--] = ':'; } buf[i] = '0'; printf( "\010" "0" "\010" ); } else if ( (c == (char)LEFTARROW) && i > 0 ) { i--; if( max_digit[i] == ':' ) { printf( "\010" ); buf[i--] = ':'; } printf( "\010" ); } else if ( (c == (char)DELETE_KEY) && i < 8 ) { buf[i]='0'; printf( "0" "\010"); } else if ( (c == (char)RIGHTARROW) && i < 8 ) { printf( "%c", buf[i++] ); if( max_digit[i] == ':' ) { printf( ":" ); buf[i++] = ':'; } } else if( ( i < 9 ) && isascii( c ) && isdigit( c ) && max_digit[i] >= c ) { printf( "%c", c ); buf[i++] = c; if( max_digit[i] == ':' ) { printf( ":" ); buf[i++] = ':'; } } else { beep(); } if ( i > maxi ) maxi = i; } if( maxi > 0 ) { /* * Parse the buffer extracting the required fields */ if( sscanf( buf, "%d:%d:%d", &hh, &mm, &ss ) == 3 ) { return ( conv_time( hh, mm, ss ) ); } } return ( ( time_t ) -1L ); } /********************************* EOF ***********************************/