1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: timedate.cpp,v 1.2 2000/06/19 19:04:05 kevin Exp $
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License (version 2) as
9 ** published by the Free Software Foundation.
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ******************************************************************************/
21 /*----------------------------------------------------------------------*/
22 /* ROUTINES THAT MANAGE TIME */
23 /*----------------------------------------------------------------------*/
27 #include "ctsupport.h"
31 * td_get_time Return time of day
35 * TIME *t; Returned time
44 currtime = time(NULL);
45 tm = localtime(&currtime);
47 t->hour = tm->tm_hour;
48 t->minute = tm->tm_min;
49 t->second = tm->tm_sec;
59 time_t t = time(NULL);
60 struct tm *lt = localtime(&t);
62 d->year = lt->tm_year + 1900;
63 d->month = lt->tm_mon;
65 d->date = lt->tm_mday;
72 double td_current_sec (void)
77 return (td_time_to_sec (&t));
82 td_time_to_sec (TIME *t)
87 ts += t->minute * 60.;
96 td_time_sub (const TIME *t1, const TIME *t2, TIME *tdiff)
98 tdiff->hour = t1->hour - t2->hour;
99 tdiff->minute = t1->minute - t2->minute;
100 tdiff->second = t1->second - t2->second;
101 tdiff->ms = t1->ms - t2->ms;
103 return td_time_norm (tdiff);
108 td_time_add (const TIME *t1, const TIME *t2, TIME *tsum)
110 tsum->ms = t1->ms + t2->ms;
111 tsum->second = t1->second + t2->second;
112 tsum->minute = t1->minute + t2->minute;
113 tsum->hour = t1->hour + t2->hour;
115 return td_time_norm (tsum);
120 td_time_copy (TIME *to, const TIME *from)
123 to->second = from->second;
124 to->minute = from->minute;
125 to->hour = from->hour;
132 * td_time_norm Normalize time in structure
135 * t = td_time_norm (t)
136 * TIME *t Time to be normalized
140 td_time_norm (TIME *t)
146 while (t->second < 0) {
150 while (t->minute < 0) {
155 while (t->ms > 1000) {
159 while (t->second > 60) {
163 while (t->minute > 60) {
173 * td_get_tmdt Return current time and date
177 * TIMEDATE *td Pointer to structure that holds time & date
181 td_get_tmdt (TIMEDATE *td)
183 td_get_date (&td->d);
184 td_get_time (&td->t);
189 * td_str_tmdt Put time & date into string
192 * str = td_str_tmdt (td)
193 * TIMEDATE *td Pointer ot time & date structure
194 * char *str Pointer to output string (Width = 21 chars)
197 * str points to a area of memory devoted to this routine
198 * DO NOT make any changes to str
202 td_str_tmdt (const TIMEDATE *td)
206 strncpy (str, td_str_date(&td->d), sizeof(str));
207 strncat (str, " ", sizeof(str));
208 strncat (str, td_str_stime(&td->t), sizeof(str));
215 * td_str_time Convert time into long string form
218 * str = td_str_time (t)
219 * char *str Output string (Field width = 12 chars)
220 * TIME *t Time to be converted
224 td_str_time (const TIME *t)
235 } else if (hour >= 12) {
241 snprintf (str, sizeof(str), "%2d:%02d:%02d.%03d%c",
242 hour, t->minute, t->second, t->ms, am_pm);
250 * td_str_stime Convert time into short string form
253 * str = td_str_stime (t)
254 * char *str Output string (Field width = 6 chars)
255 * TIME *t Time to be converted
259 td_str_stime (const TIME *t)
270 } else if (hour >= 12) {
276 snprintf (str, sizeof(str), "%2d:%02d%c",
277 hour, t->minute, am_pm);
284 * td_str_date Convert date to string form
287 * str = td_str_date (d)
288 * DATE *d Date to be converted
289 * char *str Pointer to output string (Width = 8 chars)
292 * DO NOT make any changes to str. It belongs to this routine
296 td_str_date (const DATE *d)
300 snprintf (str, sizeof(str), "%2d-%02d-%02d", d->month, d->date, d->year - 1900);
305 /*-----------------------------------------------------------------------------
307 * td_str_cdate Convert date to a character string
310 * str = td_str_cdate (d)
311 * DATE *d Date to convert
312 * char *str Pointer to date in character format
315 * The date is put in the form:
316 * <day name>, <month name> <date>, <year>
318 * Field width ranges from 17 to 29 characters
321 * str belongs to this routine, do NOT alter str
322 *---------------------------------------------------------------------------*/
326 td_str_cdate (DATE *d)
333 if (d->dow != 0) { /* only print day name if dow != 0 */
334 strcat (str, td_day_name(d->dow));
338 snprintf (temp, sizeof(temp), "%s %d, %d", td_month_name(d->month), d->date, d->year);
345 /*--------------------------------------------------------------*/
346 /* td_month_name(int n) */
347 /* return pointer to name of month given month number, n */
348 /*--------------------------------------------------------------*/
350 td_month_name ( /* return name of n-th month */
354 static char *name[] = {
370 return ((n < 1 || n > 12) ? name[0] : name[n]);
374 /*--------------------------------------------------------------*/
375 /* td_day_name(int n) */
376 /* return pointer to name of day given day number, n (1..7)*/
377 /*--------------------------------------------------------------*/
381 static char *name[] = {
392 return ((n < 1 || n > 7) ? name[0] : name[n]);