1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
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 ***
10 ** Revision 1.1 2000/06/13 16:20:31 kevin
11 ** finished c++ conversions
13 ** Revision 1.5 2000/06/05 01:33:11 kevin
14 ** *** empty log message ***
16 ** Revision 1.4 2000/05/11 01:05:51 kevin
17 ** Changed sprintf to snprintf, changed index to strchr
19 ** Revision 1.3 2000/04/28 18:00:55 kevin
20 ** remove unused files
22 ** Revision 1.2 2000/04/28 17:38:26 kevin
23 ** Removed unused files
25 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
26 ** Initial CVS import for first public release
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.
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.
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 ******************************************************************************/
44 /*----------------------------------------------------------------------*/
45 /* ROUTINES THAT MANAGE TIME */
46 /*----------------------------------------------------------------------*/
53 * td_get_time Return time of day
57 * TIME *t; Returned time
66 currtime = time(NULL);
67 tm = localtime(&currtime);
69 t->hour = tm->tm_hour;
70 t->minute = tm->tm_min;
71 t->second = tm->tm_sec;
81 time_t t = time(NULL);
82 struct tm *lt = localtime(&t);
84 d->year = lt->tm_year + 1900;
85 d->month = lt->tm_mon;
87 d->date = lt->tm_mday;
94 double td_current_sec (void)
99 return (td_time_to_sec (&t));
104 td_time_to_sec (TIME *t)
108 ts = t->hour * 3600.;
109 ts += t->minute * 60.;
118 td_time_sub (const TIME *t1, const TIME *t2, TIME *tdiff)
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;
125 return td_time_norm (tdiff);
130 td_time_add (const TIME *t1, const TIME *t2, TIME *tsum)
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;
137 return td_time_norm (tsum);
142 td_time_copy (TIME *to, const TIME *from)
145 to->second = from->second;
146 to->minute = from->minute;
147 to->hour = from->hour;
154 * td_time_norm Normalize time in structure
157 * t = td_time_norm (t)
158 * TIME *t Time to be normalized
162 td_time_norm (TIME *t)
168 while (t->second < 0) {
172 while (t->minute < 0) {
177 while (t->ms > 1000) {
181 while (t->second > 60) {
185 while (t->minute > 60) {
195 * td_get_tmdt Return current time and date
199 * TIMEDATE *td Pointer to structure that holds time & date
203 td_get_tmdt (TIMEDATE *td)
205 td_get_date (&td->d);
206 td_get_time (&td->t);
211 * td_str_tmdt Put time & date into string
214 * str = td_str_tmdt (td)
215 * TIMEDATE *td Pointer ot time & date structure
216 * char *str Pointer to output string (Width = 21 chars)
219 * str points to a area of memory devoted to this routine
220 * DO NOT make any changes to str
224 td_str_tmdt (const TIMEDATE *td)
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));
237 * td_str_time Convert time into long string form
240 * str = td_str_time (t)
241 * char *str Output string (Field width = 12 chars)
242 * TIME *t Time to be converted
246 td_str_time (const TIME *t)
257 } else if (hour >= 12) {
263 snprintf (str, sizeof(str), "%2d:%02d:%02d.%03d%c",
264 hour, t->minute, t->second, t->ms, am_pm);
272 * td_str_stime Convert time into short string form
275 * str = td_str_stime (t)
276 * char *str Output string (Field width = 6 chars)
277 * TIME *t Time to be converted
281 td_str_stime (const TIME *t)
292 } else if (hour >= 12) {
298 snprintf (str, sizeof(str), "%2d:%02d%c",
299 hour, t->minute, am_pm);
306 * td_str_date Convert date to string form
309 * str = td_str_date (d)
310 * DATE *d Date to be converted
311 * char *str Pointer to output string (Width = 8 chars)
314 * DO NOT make any changes to str. It belongs to this routine
318 td_str_date (const DATE *d)
322 snprintf (str, sizeof(str), "%2d-%02d-%02d", d->month, d->date, d->year - 1900);
327 /*-----------------------------------------------------------------------------
329 * td_str_cdate Convert date to a character string
332 * str = td_str_cdate (d)
333 * DATE *d Date to convert
334 * char *str Pointer to date in character format
337 * The date is put in the form:
338 * <day name>, <month name> <date>, <year>
340 * Field width ranges from 17 to 29 characters
343 * str belongs to this routine, do NOT alter str
344 *---------------------------------------------------------------------------*/
348 td_str_cdate (DATE *d)
355 if (d->dow != 0) { /* only print day name if dow != 0 */
356 strcat (str, td_day_name(d->dow));
360 snprintf (temp, sizeof(temp), "%s %d, %d", td_month_name(d->month), d->date, d->year);
367 /*--------------------------------------------------------------*/
368 /* td_month_name(int n) */
369 /* return pointer to name of month given month number, n */
370 /*--------------------------------------------------------------*/
372 td_month_name ( /* return name of n-th month */
376 static char *name[] = {
392 return ((n < 1 || n > 12) ? name[0] : name[n]);
396 /*--------------------------------------------------------------*/
397 /* td_day_name(int n) */
398 /* return pointer to name of day given day number, n (1..7)*/
399 /*--------------------------------------------------------------*/
403 static char *name[] = {
414 return ((n < 1 || n > 7) ? name[0] : name[n]);