--- /dev/null
+/*****************************************************************************
+** This is part of the CTSim program
+** Copyright (C) 1983-2000 Kevin Rosenberg
+**
+** $Id: timedate.cpp,v 1.1 2000/06/19 02:58:08 kevin Exp $
+** $Log: timedate.cpp,v $
+** Revision 1.1 2000/06/19 02:58:08 kevin
+** *** empty log message ***
+**
+** Revision 1.1 2000/06/13 16:20:31 kevin
+** finished c++ conversions
+**
+** Revision 1.5 2000/06/05 01:33:11 kevin
+** *** empty log message ***
+**
+** Revision 1.4 2000/05/11 01:05:51 kevin
+** Changed sprintf to snprintf, changed index to strchr
+**
+** Revision 1.3 2000/04/28 18:00:55 kevin
+** remove unused files
+**
+** Revision 1.2 2000/04/28 17:38:26 kevin
+** Removed unused files
+**
+** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
+** Initial CVS import for first public release
+**
+**
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License (version 2) as
+** published by the Free Software Foundation.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+******************************************************************************/
+
+/*----------------------------------------------------------------------*/
+/* ROUTINES THAT MANAGE TIME */
+/*----------------------------------------------------------------------*/
+
+#include <stdio.h>
+#include "kstddef.h"
+#include <time.h>
+
+/* NAME
+ * td_get_time Return time of day
+ *
+ * SYNOPSIS
+ * t = td_get_time (t)
+ * TIME *t; Returned time
+ */
+
+TIME *
+td_get_time (TIME *t)
+{
+ time_t currtime;
+ struct tm *tm;
+
+ currtime = time(NULL);
+ tm = localtime(&currtime);
+
+ t->hour = tm->tm_hour;
+ t->minute = tm->tm_min;
+ t->second = tm->tm_sec;
+ t->ms = 0;
+
+ return (t);
+}
+
+
+DATE *
+td_get_date (DATE *d)
+{
+ time_t t = time(NULL);
+ struct tm *lt = localtime(&t);
+
+ d->year = lt->tm_year + 1900;
+ d->month = lt->tm_mon;
+ d->month++;
+ d->date = lt->tm_mday;
+ d->dow = lt->tm_wday;
+ d->dow++;
+
+ return (d);
+}
+
+double td_current_sec (void)
+{
+ TIME t;
+
+ td_get_time (&t);
+ return (td_time_to_sec (&t));
+}
+
+
+double
+td_time_to_sec (TIME *t)
+{
+ double ts;
+
+ ts = t->hour * 3600.;
+ ts += t->minute * 60.;
+ ts += t->second;
+ ts += t->ms / 1000.;
+
+ return (ts);
+}
+
+
+TIME *
+td_time_sub (const TIME *t1, const TIME *t2, TIME *tdiff)
+{
+ tdiff->hour = t1->hour - t2->hour;
+ tdiff->minute = t1->minute - t2->minute;
+ tdiff->second = t1->second - t2->second;
+ tdiff->ms = t1->ms - t2->ms;
+
+ return td_time_norm (tdiff);
+}
+
+
+TIME *
+td_time_add (const TIME *t1, const TIME *t2, TIME *tsum)
+{
+ tsum->ms = t1->ms + t2->ms;
+ tsum->second = t1->second + t2->second;
+ tsum->minute = t1->minute + t2->minute;
+ tsum->hour = t1->hour + t2->hour;
+
+ return td_time_norm (tsum);
+}
+
+
+TIME *
+td_time_copy (TIME *to, const TIME *from)
+{
+ to->ms = from->ms;
+ to->second = from->second;
+ to->minute = from->minute;
+ to->hour = from->hour;
+
+ return (to);
+}
+
+
+/* NAME
+ * td_time_norm Normalize time in structure
+ *
+ * SYNOPSIS
+ * t = td_time_norm (t)
+ * TIME *t Time to be normalized
+ */
+
+TIME *
+td_time_norm (TIME *t)
+{
+ while (t->ms < 0) {
+ t->ms += 1000;
+ t->second--;
+ }
+ while (t->second < 0) {
+ t->second += 60;
+ t->minute--;
+ }
+ while (t->minute < 0) {
+ t->minute += 60;
+ t->hour--;
+ }
+
+ while (t->ms > 1000) {
+ t->ms -= 1000;
+ t->second++;
+ }
+ while (t->second > 60) {
+ t->second -= 60;
+ t->minute++;
+ }
+ while (t->minute > 60) {
+ t->minute -= 60;
+ t->hour++;
+ }
+
+ return (t);
+}
+
+
+/* NAME
+ * td_get_tmdt Return current time and date
+ *
+ * SYNOPSIS
+ * get_tmdt (td)
+ * TIMEDATE *td Pointer to structure that holds time & date
+ */
+
+void
+td_get_tmdt (TIMEDATE *td)
+{
+ td_get_date (&td->d);
+ td_get_time (&td->t);
+}
+
+
+/* NAME
+ * td_str_tmdt Put time & date into string
+ *
+ * SYNOPSIS
+ * str = td_str_tmdt (td)
+ * TIMEDATE *td Pointer ot time & date structure
+ * char *str Pointer to output string (Width = 21 chars)
+ *
+ * NOTES
+ * str points to a area of memory devoted to this routine
+ * DO NOT make any changes to str
+ */
+
+const char *
+td_str_tmdt (const TIMEDATE *td)
+{
+ static char str[80];
+
+ strncpy (str, td_str_date(&td->d), sizeof(str));
+ strncat (str, " ", sizeof(str));
+ strncat (str, td_str_stime(&td->t), sizeof(str));
+
+ return (str);
+}
+
+
+/* NAME
+ * td_str_time Convert time into long string form
+ *
+ * SYNOPSIS
+ * str = td_str_time (t)
+ * char *str Output string (Field width = 12 chars)
+ * TIME *t Time to be converted
+ */
+
+const char *
+td_str_time (const TIME *t)
+{
+ static char str[80];
+ char am_pm;
+ int hour;
+
+ hour = t->hour;
+ if (hour < 12) {
+ am_pm = 'a';
+ if (hour == 0)
+ hour = 12;
+ } else if (hour >= 12) {
+ am_pm = 'p';
+ if (hour > 12)
+ hour -= 12;
+ }
+
+ snprintf (str, sizeof(str), "%2d:%02d:%02d.%03d%c",
+ hour, t->minute, t->second, t->ms, am_pm);
+
+ return (str);
+}
+
+
+
+/* NAME
+ * td_str_stime Convert time into short string form
+ *
+ * SYNOPSIS
+ * str = td_str_stime (t)
+ * char *str Output string (Field width = 6 chars)
+ * TIME *t Time to be converted
+ */
+
+const char *
+td_str_stime (const TIME *t)
+{
+ static char str[80];
+ char am_pm;
+ int hour;
+
+ hour = t->hour;
+ if (hour < 12) {
+ am_pm = 'a';
+ if (hour == 0)
+ hour = 12;
+ } else if (hour >= 12) {
+ am_pm = 'p';
+ if (hour > 12)
+ hour -= 12;
+ }
+
+ snprintf (str, sizeof(str), "%2d:%02d%c",
+ hour, t->minute, am_pm);
+
+ return (str);
+}
+
+
+/* NAME
+ * td_str_date Convert date to string form
+ *
+ * SYNOPSIS
+ * str = td_str_date (d)
+ * DATE *d Date to be converted
+ * char *str Pointer to output string (Width = 8 chars)
+ *
+ * NOTES
+ * DO NOT make any changes to str. It belongs to this routine
+ */
+
+const char *
+td_str_date (const DATE *d)
+{
+ static char str[80];
+
+ snprintf (str, sizeof(str), "%2d-%02d-%02d", d->month, d->date, d->year - 1900);
+
+ return (str);
+}
+
+/*-----------------------------------------------------------------------------
+ * NAME
+ * td_str_cdate Convert date to a character string
+ *
+ * SYNOPSIS
+ * str = td_str_cdate (d)
+ * DATE *d Date to convert
+ * char *str Pointer to date in character format
+ *
+ * DESCRIPTION
+ * The date is put in the form:
+ * <day name>, <month name> <date>, <year>
+ *
+ * Field width ranges from 17 to 29 characters
+ *
+ * NOTES
+ * str belongs to this routine, do NOT alter str
+ *---------------------------------------------------------------------------*/
+
+
+char *
+td_str_cdate (DATE *d)
+{
+ static char str[50];
+ char temp[50];
+
+ strcpy (str, "");
+
+ if (d->dow != 0) { /* only print day name if dow != 0 */
+ strcat (str, td_day_name(d->dow));
+ strcat (str, ", ");
+ }
+
+ snprintf (temp, sizeof(temp), "%s %d, %d", td_month_name(d->month), d->date, d->year);
+ strcat (str, temp);
+
+ return (str);
+}
+
+
+/*--------------------------------------------------------------*/
+/* td_month_name(int n) */
+/* return pointer to name of month given month number, n */
+/*--------------------------------------------------------------*/
+char *
+td_month_name ( /* return name of n-th month */
+ int n
+)
+{
+ static char *name[] = {
+ "",
+ "January",
+ "February",
+ "March",
+ "April",
+ "May",
+ "June",
+ "July",
+ "August",
+ "September",
+ "October",
+ "November",
+ "December"
+ };
+
+ return ((n < 1 || n > 12) ? name[0] : name[n]);
+}
+
+
+/*--------------------------------------------------------------*/
+/* td_day_name(int n) */
+/* return pointer to name of day given day number, n (1..7)*/
+/*--------------------------------------------------------------*/
+char *
+td_day_name (int n)
+{
+ static char *name[] = {
+ "",
+ "Monday",
+ "Tuesday",
+ "Wednesday",
+ "Thursday",
+ "Friday",
+ "Saturday",
+ "Sunday"
+ };
+
+ return ((n < 1 || n > 7) ? name[0] : name[n]);
+}