Subversion Repositories svn1-original

Rev

Rev 381 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*************************************************************************
*       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    <ctype.h>
#include    <time.h>
#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 );
}

/********************************* EOF ***********************************/