2000-05-06 21:34:20 +02:00
|
|
|
/****************************************************************/
|
|
|
|
/* */
|
|
|
|
/* systime.c */
|
|
|
|
/* */
|
|
|
|
/* DOS/C Date/Time Functions */
|
|
|
|
/* */
|
|
|
|
/* Copyright (c) 1998 */
|
|
|
|
/* Pasquale J. Villani */
|
|
|
|
/* All Rights Reserved */
|
|
|
|
/* */
|
|
|
|
/* This file is part of DOS-C. */
|
|
|
|
/* */
|
|
|
|
/* DOS-C is free software; you can redistribute it and/or */
|
|
|
|
/* modify it under the terms of the GNU General Public License */
|
|
|
|
/* as published by the Free Software Foundation; either version */
|
|
|
|
/* 2, or (at your option) any later version. */
|
|
|
|
/* */
|
|
|
|
/* DOS-C 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 DOS-C; see the file COPYING. If not, */
|
|
|
|
/* write to the Free Software Foundation, 675 Mass Ave, */
|
|
|
|
/* Cambridge, MA 02139, USA. */
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
#include "portab.h"
|
|
|
|
#include "time.h"
|
|
|
|
#include "date.h"
|
|
|
|
#include "globals.h"
|
|
|
|
|
|
|
|
#ifdef VERSION_STRINGS
|
2001-11-18 15:01:12 +01:00
|
|
|
static BYTE *RcsId =
|
|
|
|
"$Id$";
|
2000-05-06 21:34:20 +02:00
|
|
|
#endif
|
|
|
|
|
2002-02-03 23:40:24 +01:00
|
|
|
const UWORD days[2][13] = {
|
2000-05-06 21:34:20 +02:00
|
|
|
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
|
|
|
|
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
|
|
|
|
};
|
|
|
|
|
2002-01-23 23:29:41 +01:00
|
|
|
extern request ASM ClkReqHdr;
|
2001-11-18 15:01:12 +01:00
|
|
|
|
2001-03-21 03:56:26 +01:00
|
|
|
/*
|
|
|
|
return a pointer to an array with the days for that year
|
2001-11-18 15:01:12 +01:00
|
|
|
*/
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2002-02-03 23:40:24 +01:00
|
|
|
const UWORD *is_leap_year_monthdays(UWORD y)
|
2001-03-21 03:56:26 +01:00
|
|
|
{
|
2001-11-18 15:01:12 +01:00
|
|
|
/* this is correct in a strict mathematical sense
|
|
|
|
return ((y) & 3 ? days[0] : (y) % 100 ? days[1] : (y) % 400 ? days[0] : days[1]); */
|
|
|
|
|
|
|
|
/* this will work until 2200 - long enough for me - and saves 0x1f bytes */
|
|
|
|
|
|
|
|
if ((y & 3) || y == 2100)
|
|
|
|
return days[0];
|
|
|
|
|
|
|
|
return days[1];
|
2001-03-21 03:56:26 +01:00
|
|
|
}
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2001-08-19 14:58:36 +02:00
|
|
|
UWORD DaysFromYearMonthDay(UWORD Year, UWORD Month, UWORD DayOfMonth)
|
2001-03-21 03:56:26 +01:00
|
|
|
{
|
2001-11-18 15:01:12 +01:00
|
|
|
if (Year < 1980)
|
|
|
|
return 0;
|
|
|
|
|
2001-03-21 03:56:26 +01:00
|
|
|
return DayOfMonth - 1
|
|
|
|
+ is_leap_year_monthdays(Year)[Month - 1]
|
2001-11-18 15:01:12 +01:00
|
|
|
+ ((Year - 1980) * 365) + ((Year - 1980 + 3) / 4);
|
|
|
|
|
2001-03-21 03:56:26 +01:00
|
|
|
}
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2001-03-21 03:56:26 +01:00
|
|
|
/* common - call the clock driver */
|
|
|
|
void ExecuteClockDriverRequest(BYTE command)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
BinaryCharIO(clock, sizeof(struct ClockRecord), &ClkRecord, command);
|
2001-11-18 15:01:12 +01:00
|
|
|
}
|
2001-03-21 03:56:26 +01:00
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
void DosGetTime(struct dostime *dt)
|
2001-03-21 03:56:26 +01:00
|
|
|
{
|
|
|
|
ExecuteClockDriverRequest(C_INPUT);
|
2001-11-18 15:01:12 +01:00
|
|
|
|
2000-05-06 21:34:20 +02:00
|
|
|
if (ClkReqHdr.r_status & S_ERROR)
|
|
|
|
return;
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
dt->hour = ClkRecord.clkHours;
|
|
|
|
dt->minute = ClkRecord.clkMinutes;
|
|
|
|
dt->second = ClkRecord.clkSeconds;
|
|
|
|
dt->hundredth = ClkRecord.clkHundredths;
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
int DosSetTime(const struct dostime *dt)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-05-09 00:49:35 +02:00
|
|
|
/* for ClkRecord.clkDays */
|
|
|
|
ExecuteClockDriverRequest(C_INPUT);
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
ClkRecord.clkHours = dt->hour;
|
|
|
|
ClkRecord.clkMinutes = dt->minute;
|
|
|
|
ClkRecord.clkSeconds = dt->second;
|
|
|
|
ClkRecord.clkHundredths = dt->hundredth;
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2001-03-21 03:56:26 +01:00
|
|
|
ExecuteClockDriverRequest(C_OUTPUT);
|
2000-05-06 21:34:20 +02:00
|
|
|
|
|
|
|
if (ClkReqHdr.r_status & S_ERROR)
|
|
|
|
return char_error(&ClkReqHdr, (struct dhdr FAR *)clock);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
unsigned char DosGetDate(struct dosdate *dd)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2001-08-19 14:58:36 +02:00
|
|
|
UWORD c;
|
2002-02-03 23:40:24 +01:00
|
|
|
const UWORD *pdays;
|
2001-11-18 15:01:12 +01:00
|
|
|
UWORD Year, Month;
|
2001-03-21 03:56:26 +01:00
|
|
|
|
|
|
|
ExecuteClockDriverRequest(C_INPUT);
|
2000-05-06 21:34:20 +02:00
|
|
|
|
|
|
|
if (ClkReqHdr.r_status & S_ERROR)
|
2002-10-22 04:40:19 +02:00
|
|
|
return 0;
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2001-11-18 15:01:12 +01:00
|
|
|
for (Year = 1980, c = ClkRecord.clkDays;;)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2001-03-21 03:56:26 +01:00
|
|
|
pdays = is_leap_year_monthdays(Year);
|
|
|
|
if (c >= pdays[12])
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
|
|
|
++Year;
|
2001-03-21 03:56:26 +01:00
|
|
|
c -= pdays[12];
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* c contains the days left and count the number of days for */
|
|
|
|
/* that year. Use this to index the table. */
|
|
|
|
Month = 1;
|
2001-03-21 03:56:26 +01:00
|
|
|
while (c >= pdays[Month])
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
|
|
|
++Month;
|
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
dd->year = Year;
|
|
|
|
dd->month = Month;
|
|
|
|
dd->monthday = c - pdays[Month - 1] + 1;
|
2000-05-06 21:34:20 +02:00
|
|
|
|
|
|
|
/* Day of week is simple. Take mod 7, add 2 (for Tuesday */
|
|
|
|
/* 1-1-80) and take mod again */
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
return (ClkRecord.clkDays + 2) % 7;
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
int DosSetDate(const struct dosdate *dd)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
UWORD Year = dd->year;
|
|
|
|
UWORD Month = dd->month;
|
|
|
|
UWORD DayOfMonth = dd->monthday;
|
|
|
|
const UWORD *pdays = is_leap_year_monthdays(Year);
|
2001-11-18 15:01:12 +01:00
|
|
|
|
2000-05-06 21:34:20 +02:00
|
|
|
if (Year < 1980 || Year > 2099
|
|
|
|
|| Month < 1 || Month > 12
|
2002-10-22 04:40:19 +02:00
|
|
|
|| DayOfMonth < 1
|
|
|
|
|| DayOfMonth > pdays[Month] - pdays[Month - 1])
|
2000-05-06 21:34:20 +02:00
|
|
|
return DE_INVLDDATA;
|
|
|
|
|
2002-05-09 00:49:35 +02:00
|
|
|
ExecuteClockDriverRequest(C_INPUT);
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2001-03-21 03:56:26 +01:00
|
|
|
ClkRecord.clkDays = DaysFromYearMonthDay(Year, Month, DayOfMonth);
|
|
|
|
|
|
|
|
ExecuteClockDriverRequest(C_OUTPUT);
|
2000-05-06 21:34:20 +02:00
|
|
|
|
|
|
|
if (ClkReqHdr.r_status & S_ERROR)
|
|
|
|
return char_error(&ClkReqHdr, (struct dhdr FAR *)clock);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-11-18 00:26:45 +01:00
|
|
|
|