Subversion Repositories svn1-original

Rev

Rev 46 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 root 1
/*************************************************************************
2
*       Copyright (C) 1995 Embbedded Solutions
3
*                       All rights reserved
4
*
5
* file:     src\times.c
6
*
7
* purpose:  This module contains routines associated with the
8
*           time calculations
9
*
10
*           As the storage and display of a time is machine dependent so
11
*           these routines may need to be altered
12
*
13
* functions
14
*       time_a                  - Convert time to ascii
15
*       time_fa                 - Convert time to ascii
16
*       conv_time               - Convert hh,mm,ss to time
17
*       get_time                - Read/Edit time from user
18
*
19
* programmer: David Purdie
20
*
21
* revision  date        by      reason
22
*           17-Oct-88   DDP     Added new routine time_fa()
23
*   00.0    27/01/95    DDP     Tidies up the program and formatted the file
24
*   01.0    18/09/2005  DDP     Allow editing of time fields
25
*                               Simplied the conversion of text to time
26
*
27
**************************************************************************/
28
 
29
#include    <ctype.h>
30
#include    <time.h>
31
#include    "consts.h"
32
#include    "structs.h"
33
#include    "proto.h"
34
 
35
/*========================================================================
36
 *
37
 *  Convert time to ascii
38
 *
39
 *  Purpose:
40
 *      This routine will convert the time ( as held in tim ) into an ascii
41
 *      string. The address of the string is returned to the user. The
42
 *      string is internally static and is of a fixed format
43
 *
44
 *      The string format is
45
 *
46
 *      HH:MM:SS(null)  - if the time is valid.
47
 *      ** ** **(null)  - if the time is unknown
48
 * 
49
 *
50
 *  Parameters:
51
 *      tim         Time to convert
52
 *
53
 *  Returns:
54
 *      Address of an internal buffer that holds the ascii time
55
 *
56
 *========================================================================*/
57
 
58
char       *time_a( time_t tim )
59
{
60
    static char buf[] = "00:00:00";              /* Hold return string here */
61
    static char invalid[] = "** ** **";          /* Invalid string */
62
 
63
    time_t      hours, mins, secs;
64
 
65
    if( tim >= 0 && tim < 359999L )
66
    {
67
        hours = tim / 3600L;                     /* Calc the hours */
68
        tim -= hours * 3600L;                    /* secs left */
69
        mins = tim / 60L;
70
        tim -= mins * 60L;
71
        secs = tim;
72
        sprintf( buf, "%2.2ld:%2.2ld:%2.2ld", hours, mins, secs );
73
        return ( buf );
74
    }
75
    else
76
        return ( invalid );
77
}
78
 
79
/*========================================================================
80
 *
81
 *  Convert time to ascii
82
 *
83
 *  Purpose:
84
 *      This routine will convert the time ( as held in tim ) into an ascii
85
 *      string. The address of the string is returned to the user. The
86
 *      string is internally static and is of a fixed format
87
 *
88
 *      The string format is
89
 *
90
 *      HH:MM:SS(null)  - if the time is valid.
91
 *      -- -- --(null)  - if the time is unknown and "flag" is true
92
 *      ** ** **(null)  - if the time is unknown and flag is false
93
 *
94
 *
95
 *  Parameters:
96
 *      tim         Time to convert
97
 *      flag        Control display if time is not valid
98
 *                      TRUE  "-- -- --"
99
 *                      FALSE "** ** **"
100
 *
101
 *  Returns:
102
 *      Address of an internal buffer that holds the ascii time
103
 *
104
 *========================================================================*/
105
 
106
char       *time_fa( time_t tim, char flag )
107
{
108
    static char buf1[] = "00:00:00";             /* Hold return string here */
109
    static char finvalid[] = "** ** **";         /* Invalid string */
110
    static char tinvalid[] = "-- -- --";
111
    time_t      hours, mins, secs;
112
 
113
    if( tim >= 0 && tim < 359999L )
114
    {
115
        hours = tim / 3600L;                     /* Calc the hours */
116
        tim -= hours * 3600L;                    /* secs left */
117
        mins = tim / 60L;
118
        tim -= mins * 60L;
119
        secs = tim;
120
        sprintf( buf1, "%2.2ld:%2.2ld:%2.2ld", hours, mins, secs );
121
        return ( buf1 );
122
    }
123
    else if( flag )
124
    {
125
        return ( tinvalid );
126
    }
127
    else
128
    {
129
        return ( finvalid );
130
    }
131
}
132
 
133
/*========================================================================
134
 *
135
 *  Convert hh,mm,ss to time
136
 *
137
 *  Purpose:
138
 *      This function is convert hours, mins and seconds into the internal
139
 *      format of time
140
 *
141
 *  Parameters:
142
 *      hh          hours 0..
143
 *      mm          minutes 0..59
144
 *      ss          Seconds 0..59
145
 *
146
 *  Returns:
147
 *      Time
148
 *
149
 *========================================================================*/
150
 
151
time_t conv_time( int hh, int mm, int ss )
152
{
153
    time_t      time;                            /* The final result */
154
 
155
    time = ( ( ( time_t ) hh * 60L ) + ( time_t ) mm ) * 60L + ( time_t ) ss;
156
    return ( time );
157
}
158
 
159
 
160
/*========================================================================
161
 *
162
 *  Read/Edit time from user
163
 *
164
 *  Purpose:
165
 *      This function is called accept a time field from user and allow
166
 *      the operator to edit the time as it is entered.
167
 *
168
 *  Parameters:
169
 *      tim         - Time to edit
170
 *
171
 *  Returns:
172
 *      Time entered
173
 *      Will return an illegal time if the entry is not complete
174
 *
175
 *========================================================================*/
176
 
31 - 177
time_t get_time(time_t utim)
1 root 178
{
179
    char        c;                              /* One of those */
180
    int         i = 0;                          /* Counts user input */
181
    int         maxi = 0;                       /* Max user input */
182
    static char buf[] = "00:00:00";             /* Build up a string here */
183
    static char max_digit[] = "29:59:59";       /* Maximum digit's allowed in entry */
184
    int         hh = 0, mm = 0, ss = 0;
185
 
186
    /*
187
    **  Insert the base time into the edit buffer
188
    **  Invalid times are treated as 00:00:00
189
    */
31 - 190
    if ( utim == -1L )
1 root 191
    {
31 - 192
        utim = 0;
193
    }
194
 
195
    {
1 root 196
        char * ctime;
31 - 197
        ctime = time_a(utim);
1 root 198
        if ( ctime[0] != '*' )
199
            memcpy( buf, ctime, sizeof(buf)-1);
31 - 200
        else
201
            memcpy( buf, "00:00:00", sizeof(buf)-1);
1 root 202
    }
203
 
204
    /*
205
    **  Edit the string
206
    */
207
    while( TRUE )
208
    {
209
        c = getinp();
210
        if( c == ABORT_CHAR )
211
        {
212
            i = 0;
213
            break;
214
        }
215
        if( c == '\n' || c == '\t' )
216
            break;
217
 
218
        if( ( c == rubout ) && i > 0 )           /* rubout character */
219
        {
220
            i--;
221
            if( max_digit[i] == ':' )
222
            {
223
                printf( "\010:\010" );
224
                buf[i--] = ':';
225
            }
226
            buf[i] = '0';
227
            printf( "\010" "0" "\010" );
228
        }
229
 
230
        else if ( (c == (char)LEFTARROW)  && i > 0 )
231
        {
232
            i--;
233
            if( max_digit[i] == ':' )
234
            {
235
                printf( "\010" );
236
                buf[i--] = ':';
237
            }
238
            printf( "\010" );
239
        }
240
 
241
        else if ( (c == (char)DELETE_KEY)  && i < 8 )
242
        {
243
            buf[i]='0';
244
            printf( "0" "\010");
245
        }
246
 
247
        else if ( (c == (char)RIGHTARROW)  && i < 8 )
248
        {
249
            printf( "%c", buf[i++] );
250
            if( max_digit[i] == ':' )
251
            {
252
                printf( ":" );
253
                buf[i++] = ':';
254
            }
255
        }
256
 
257
        else if( ( i < 9 ) && isascii( c ) && isdigit( c )
258
                 && max_digit[i] >= c )
259
        {
260
            printf( "%c", c );
261
            buf[i++] = c;
262
            if( max_digit[i] == ':' )
263
            {
264
                printf( ":" );
265
                buf[i++] = ':';
266
            }
267
        }
268
        else
269
        {
270
            beep();
271
        }
272
 
273
        if ( i > maxi )
274
            maxi = i;
275
    }
276
 
277
    if( maxi > 0 )
278
    {
279
        /*
280
         * Parse the buffer extracting the required fields
281
         */
282
        if( sscanf( buf, "%d:%d:%d", &hh, &mm, &ss ) == 3 )
283
        {
284
            return ( conv_time( hh, mm, ss ) );
285
        }
286
    }
287
    return ( ( time_t ) -1L );
288
}
289
 
290
/********************************* EOF ***********************************/