home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
TIMEXSRC.ZIP
/
TMCLOCK.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-03-29
|
4KB
|
163 lines
/* tmclock.c -- Convert tm struct to a clock value.
Taken from WRAP, but really taken from sources listed below.
This is a PD routine that fills in one of the major holes in the suite
of time conversion functions. Read on...
Also contains xlocaltime(), which I had to include with TIMEX because
the standard localtime() function doesn't work correctly in the non-root
thread. Sheesh!
*/
#include <stdio.h>
#include <memory.h>
#include <sys/types.h>
#include <time.h>
static struct tm xtmblk;
struct tm *
xlocaltime(
time_t *timeP
) {
time_t timval;
memcpy( &xtmblk, localtime( timeP ), sizeof( struct tm ) );
/* Fix up hours and minutes and seconds */
timval = *timeP - timezone;
xtmblk.tm_sec = (int)((long)timval % 60);
xtmblk.tm_min = (int)(((long)timval / 60 ) % 60);
xtmblk.tm_hour = (int)(((long)timval / 3600 ) % 24);
return( &xtmblk );
}
/*
*//* os_tmclock( tmP )
Convert tm struct to time value
Stolen from Arc 5.21 sources and inserted here in original form
(except for any changes needed for os-specific time representation).
*/
/************************* begin tmclock.c *****************************/
/*
* Stolen from the Arc 5.21 sources...
*
* Stolen from Jef Poskanzer's tws time library, which was stolen from
* Marshall Rose's MH Message Handling system...
*
* tmclock() will convert time from a tm struct back to a clock value.
* tmjuliandate() converts a tm struct to its julian day number.
* tmsubdayclock() takes hours, minutes, and seconds from a tm struct
* and returns the number of seconds since midnight of that day. (?)
* -- Howard Chu, August 1 1988 hyc@umix.cc.umich.edu, umix!hyc
*/
/* $Header: tmclock.c,v 1.3 88/08/02 14:15:58 hyc Exp $ */
/* Julian day number of the Unix* clock's origin, 01 Jan 1970. */
#define JD1970 2440587L
#define CENTURY 19
long tzone;
long
tmjuliandate( tm )
struct tm *tm;
{
register int mday, mon, year;
register long a, b;
double jd;
if ( (mday = tm -> tm_mday) < 1 || mday > 31 ||
(mon = tm -> tm_mon + 1) < 1 || mon > 12 ||
(year = tm -> tm_year) < 1 || year > 10000 )
return ( -1L );
if ( year < 100 )
year += CENTURY * 100;
if ( mon == 1 || mon == 2 )
{
--year;
mon += 12;
}
if ( year < 1583 )
return ( -1L );
a = year / 100;
b = 2 - a + a / 4;
b += (long) ( (double) year * 365.25 );
b += (long) ( 30.6001 * ( (double) mon + 1.0 ) );
jd = mday + b + 1720994.5;
return ( (long) jd );
}
long
tmsubdayclock( tm )
struct tm *tm;
{
register int sec, min, hour;
long result;
struct tm *tmcheck;
#if BSD
{
struct timeval tp;
struct timezone tzp;
gettimeofday(&tp, &tzp);
daylight=tzp.tz_dsttime;
tzone=tzp.tz_minuteswest*(-60);
}
#else
tzset();
tzone = -timezone; /* declared as extern in SYSV <time.h> */
#endif
if ( (sec = tm -> tm_sec) < 0 || sec > 59 ||
(min = tm -> tm_min) < 0 || min > 59 ||
(hour = tm -> tm_hour) < 0 || hour > 23 )
return ( -1L );
result = ( (long)hour * 60 + min ) * 60 + sec;
result -= tzone;
#ifdef OUT /* MSDOS doesn't know from dst */
/* Convert back to swag the dst flag */
tmcheck = xlocaltime( &result );
if ( tmcheck->tm_isdst )
result -= 60 * 60;
#endif /* OUT */
return ( result );
}
long
tmclock( tm )
struct tm *tm;
{
register long jd, sdc, result;
if ( ( jd = tmjuliandate( tm ) ) == -1L )
return ( -1L );
if ( ( sdc = tmsubdayclock( tm ) ) == -1L )
return ( -1L );
result = ( jd - JD1970 ) * 24 * 60 * 60 + sdc;
return ( result );
}
/************************* end tmclock.c *******************************/