Subversion Repositories svn1

Rev

Go to most recent revision | Details | 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
 
177
time_t get_time(time_t tim)
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
    */
190
    if ( tim && tim != -1L )
191
    {
192
        char * ctime;
193
        ctime = time_a(tim);
194
        if ( ctime[0] != '*' )
195
            memcpy( buf, ctime, sizeof(buf)-1);
196
    }
197
 
198
    /*
199
    **  Edit the string
200
    */
201
    while( TRUE )
202
    {
203
        c = getinp();
204
        if( c == ABORT_CHAR )
205
        {
206
            i = 0;
207
            break;
208
        }
209
        if( c == '\n' || c == '\t' )
210
            break;
211
 
212
        if( ( c == rubout ) && i > 0 )           /* rubout character */
213
        {
214
            i--;
215
            if( max_digit[i] == ':' )
216
            {
217
                printf( "\010:\010" );
218
                buf[i--] = ':';
219
            }
220
            buf[i] = '0';
221
            printf( "\010" "0" "\010" );
222
        }
223
 
224
        else if ( (c == (char)LEFTARROW)  && i > 0 )
225
        {
226
            i--;
227
            if( max_digit[i] == ':' )
228
            {
229
                printf( "\010" );
230
                buf[i--] = ':';
231
            }
232
            printf( "\010" );
233
        }
234
 
235
        else if ( (c == (char)DELETE_KEY)  && i < 8 )
236
        {
237
            buf[i]='0';
238
            printf( "0" "\010");
239
        }
240
 
241
        else if ( (c == (char)RIGHTARROW)  && i < 8 )
242
        {
243
            printf( "%c", buf[i++] );
244
            if( max_digit[i] == ':' )
245
            {
246
                printf( ":" );
247
                buf[i++] = ':';
248
            }
249
        }
250
 
251
        else if( ( i < 9 ) && isascii( c ) && isdigit( c )
252
                 && max_digit[i] >= c )
253
        {
254
            printf( "%c", c );
255
            buf[i++] = c;
256
            if( max_digit[i] == ':' )
257
            {
258
                printf( ":" );
259
                buf[i++] = ':';
260
            }
261
        }
262
        else
263
        {
264
            beep();
265
        }
266
 
267
        if ( i > maxi )
268
            maxi = i;
269
    }
270
 
271
    if( maxi > 0 )
272
    {
273
        /*
274
         * Parse the buffer extracting the required fields
275
         */
276
        if( sscanf( buf, "%d:%d:%d", &hh, &mm, &ss ) == 3 )
277
        {
278
            return ( conv_time( hh, mm, ss ) );
279
        }
280
    }
281
    return ( ( time_t ) -1L );
282
}
283
 
284
/********************************* EOF ***********************************/