| 91 |
- |
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 |
/********************************* EOF ***********************************/
|