r99: *** empty log message ***
[ctsim.git] / libctsupport / timedate.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: timedate.cpp,v 1.1 2000/06/19 02:58:08 kevin Exp $
6 **  $Log: timedate.cpp,v $
7 **  Revision 1.1  2000/06/19 02:58:08  kevin
8 **  *** empty log message ***
9 **
10 **  Revision 1.1  2000/06/13 16:20:31  kevin
11 **  finished c++ conversions
12 **
13 **  Revision 1.5  2000/06/05 01:33:11  kevin
14 **  *** empty log message ***
15 **
16 **  Revision 1.4  2000/05/11 01:05:51  kevin
17 **  Changed sprintf to snprintf, changed index to strchr
18 **
19 **  Revision 1.3  2000/04/28 18:00:55  kevin
20 **  remove unused files
21 **
22 **  Revision 1.2  2000/04/28 17:38:26  kevin
23 **  Removed unused files
24 **
25 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
26 **  Initial CVS import for first public release
27 **
28 **
29 **
30 **  This program is free software; you can redistribute it and/or modify
31 **  it under the terms of the GNU General Public License (version 2) as
32 **  published by the Free Software Foundation.
33 **
34 **  This program is distributed in the hope that it will be useful,
35 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
36 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37 **  GNU General Public License for more details.
38 **
39 **  You should have received a copy of the GNU General Public License
40 **  along with this program; if not, write to the Free Software
41 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
42 ******************************************************************************/
43
44 /*----------------------------------------------------------------------*/
45 /*                      ROUTINES THAT MANAGE TIME                       */
46 /*----------------------------------------------------------------------*/
47
48 #include <stdio.h>
49 #include "kstddef.h"
50 #include <time.h>
51
52 /* NAME
53  *    td_get_time                       Return time of day
54  *
55  * SYNOPSIS
56  *    t = td_get_time (t)
57  *    TIME *t;                          Returned time
58  */
59
60 TIME *
61 td_get_time (TIME *t)
62 {
63   time_t currtime;
64   struct tm *tm;
65
66   currtime = time(NULL);
67   tm = localtime(&currtime);
68
69   t->hour   = tm->tm_hour;
70   t->minute = tm->tm_min; 
71   t->second = tm->tm_sec; 
72   t->ms     = 0;       
73
74   return (t);
75 }
76
77
78 DATE *
79 td_get_date (DATE *d)
80 {
81   time_t t = time(NULL);
82   struct tm *lt = localtime(&t);
83
84   d->year = lt->tm_year + 1900;
85   d->month = lt->tm_mon;
86   d->month++;
87   d->date = lt->tm_mday;
88   d->dow = lt->tm_wday;
89   d->dow++;
90
91   return (d);
92 }
93
94 double td_current_sec (void)
95 {
96         TIME t;
97
98         td_get_time (&t);
99         return (td_time_to_sec (&t));
100 }
101
102
103 double 
104 td_time_to_sec (TIME *t)
105 {
106         double ts;
107
108         ts = t->hour * 3600.;
109         ts += t->minute * 60.;
110         ts += t->second;
111         ts += t->ms / 1000.;
112
113         return (ts);
114 }
115
116
117 TIME *
118 td_time_sub (const TIME *t1, const TIME *t2, TIME *tdiff)
119 {
120         tdiff->hour   = t1->hour   - t2->hour;
121         tdiff->minute = t1->minute - t2->minute;
122         tdiff->second = t1->second - t2->second;
123         tdiff->ms     = t1->ms     - t2->ms;
124
125         return td_time_norm (tdiff);
126 }
127
128
129 TIME *
130 td_time_add (const TIME *t1, const TIME *t2, TIME *tsum)
131 {
132         tsum->ms       = t1->ms     + t2->ms;
133         tsum->second   = t1->second + t2->second;
134         tsum->minute   = t1->minute + t2->minute;
135         tsum->hour     = t1->hour   + t2->hour;
136
137         return td_time_norm (tsum);
138 }
139
140
141 TIME *
142 td_time_copy (TIME *to, const TIME *from)
143 {
144         to->ms       = from->ms;
145         to->second   = from->second;
146         to->minute   = from->minute;
147         to->hour     = from->hour;
148
149         return (to);
150 }
151
152
153 /* NAME
154  *      td_time_norm                    Normalize time in structure
155  *
156  * SYNOPSIS
157  *      t = td_time_norm (t)
158  *      TIME *t                         Time to be normalized
159  */
160
161 TIME *
162 td_time_norm (TIME *t)
163 {
164         while (t->ms < 0) {
165             t->ms += 1000;
166             t->second--;
167         }
168         while (t->second < 0) {
169             t->second += 60;
170             t->minute--;
171         }
172         while (t->minute < 0) {
173             t->minute += 60;
174             t->hour--;
175         }
176
177         while (t->ms > 1000) {
178             t->ms -= 1000;
179             t->second++;
180         }
181         while (t->second > 60) {
182             t->second -= 60;
183             t->minute++;
184         }
185         while (t->minute > 60) {
186             t->minute -= 60;
187             t->hour++;
188         }
189
190         return (t);
191 }
192
193
194 /* NAME
195  *    td_get_tmdt                       Return current time and date
196  *
197  * SYNOPSIS
198  *    get_tmdt (td)
199  *    TIMEDATE *td                      Pointer to structure that holds time & date
200  */
201
202 void 
203 td_get_tmdt (TIMEDATE *td)
204 {
205         td_get_date (&td->d);
206         td_get_time (&td->t);
207 }
208
209
210 /* NAME
211  *      td_str_tmdt             Put time & date into string
212  *
213  * SYNOPSIS
214  *      str = td_str_tmdt (td)
215  *      TIMEDATE *td            Pointer ot time & date structure
216  *      char *str               Pointer to output string (Width = 21 chars)
217  *
218  * NOTES
219  *          str points to a area of memory devoted to this routine
220  *      DO NOT make any changes to str
221  */
222
223 const char *
224 td_str_tmdt (const TIMEDATE *td)
225 {
226         static char str[80];
227
228         strncpy (str, td_str_date(&td->d), sizeof(str));
229         strncat (str, " ", sizeof(str));
230         strncat (str, td_str_stime(&td->t), sizeof(str));
231
232         return (str);
233 }
234
235
236 /* NAME
237  *   td_str_time                        Convert time into long string form
238  *
239  * SYNOPSIS
240  *   str = td_str_time (t)
241  *   char *str                          Output string (Field width = 12 chars)
242  *   TIME *t                            Time to be converted
243  */
244  
245 const char *
246 td_str_time (const TIME *t)
247 {
248         static char str[80];
249         char am_pm;
250         int hour;
251
252         hour = t->hour;
253         if (hour < 12) {
254             am_pm = 'a';
255             if (hour == 0)
256                 hour = 12;
257         } else if (hour >= 12) {
258             am_pm = 'p';
259             if (hour > 12)
260                 hour -= 12;
261         }
262
263         snprintf (str, sizeof(str), "%2d:%02d:%02d.%03d%c",
264                 hour, t->minute, t->second, t->ms, am_pm);
265
266         return (str);
267 }
268
269
270
271 /* NAME
272  *   td_str_stime                       Convert time into short string form
273  *
274  * SYNOPSIS
275  *   str = td_str_stime (t)
276  *   char *str                          Output string (Field width = 6 chars)
277  *   TIME *t                            Time to be converted
278  */
279  
280 const char *
281 td_str_stime (const TIME *t)
282 {
283         static char str[80];
284         char am_pm;
285         int hour;
286
287         hour = t->hour;
288         if (hour < 12) {
289             am_pm = 'a';
290             if (hour == 0)
291                 hour = 12;
292         } else if (hour >= 12) {
293             am_pm = 'p';
294             if (hour > 12)
295                 hour -= 12;
296         }
297
298         snprintf (str, sizeof(str), "%2d:%02d%c",
299                 hour, t->minute, am_pm);
300
301         return (str);
302 }
303
304
305 /* NAME
306  *      td_str_date             Convert date to string form
307  *
308  * SYNOPSIS
309  *      str = td_str_date (d)
310  *      DATE *d                 Date to be converted
311  *      char *str               Pointer to output string (Width = 8 chars)
312  *
313  * NOTES
314  *      DO NOT make any changes to str.  It belongs to this routine
315  */
316
317 const char *
318 td_str_date (const DATE *d)
319 {
320         static char str[80];
321
322         snprintf (str, sizeof(str), "%2d-%02d-%02d", d->month, d->date, d->year - 1900);
323
324         return (str);
325 }
326
327 /*-----------------------------------------------------------------------------
328  * NAME
329  *   td_str_cdate                       Convert date to a character string
330  *
331  * SYNOPSIS
332  *   str = td_str_cdate (d)
333  *   DATE *d                            Date to convert
334  *   char *str                          Pointer to date in character format
335  *
336  * DESCRIPTION
337  *    The date is put in the form:
338  *          <day name>, <month name> <date>, <year>
339  *
340  *    Field width ranges from 17 to 29 characters
341  *
342  * NOTES
343  *    str belongs to this routine, do NOT alter str
344  *---------------------------------------------------------------------------*/
345
346
347 char *
348 td_str_cdate (DATE *d)
349 {
350         static char str[50];
351         char temp[50];
352
353         strcpy (str, "");
354
355         if (d->dow != 0) {                      /* only print day name if dow != 0 */
356             strcat (str, td_day_name(d->dow));
357             strcat (str, ", ");
358         }
359
360         snprintf (temp, sizeof(temp), "%s %d, %d", td_month_name(d->month), d->date, d->year);
361         strcat (str, temp);
362
363         return (str);
364 }
365
366
367 /*--------------------------------------------------------------*/
368 /* td_month_name(int n)                                         */
369 /*      return pointer to name of month given month number, n   */
370 /*--------------------------------------------------------------*/
371 char *
372 td_month_name ( /* return name of n-th month */
373     int n
374 )
375 {
376         static char *name[] = {
377             "",
378             "January",
379             "February",
380             "March",
381             "April",
382             "May",
383             "June",
384             "July",
385             "August",
386             "September",
387             "October",
388             "November",
389             "December"
390         };
391
392         return ((n < 1 || n > 12) ? name[0] : name[n]);
393 }
394
395
396 /*--------------------------------------------------------------*/
397 /* td_day_name(int n)                                           */
398 /*      return pointer to name of day given day number, n (1..7)*/
399 /*--------------------------------------------------------------*/
400 char *
401 td_day_name (int n)
402 {
403         static char *name[] = {
404             "",
405             "Monday",
406             "Tuesday",
407             "Wednesday",
408             "Thursday",
409             "Friday",
410             "Saturday",
411             "Sunday"
412         };
413
414         return ((n < 1 || n > 7) ? name[0] : name[n]);
415 }