xref: /csrg-svn/lib/libcompat/4.1/ftime.c (revision 59996)
148368Sbostic /*-
248368Sbostic  * Copyright (c) 1980 The Regents of the University of California.
348368Sbostic  * All rights reserved.
448368Sbostic  *
548368Sbostic  * %sccs.include.proprietary.c%
621326Sdist  */
712846Ssam 
826516Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*59996Storek static char sccsid[] = "@(#)ftime.c	5.4 (Berkeley) 05/13/93";
1048368Sbostic #endif /* LIBC_SCCS and not lint */
1121326Sdist 
1212846Ssam #include <sys/types.h>
1312846Ssam #include <sys/time.h>
1412846Ssam 
1512846Ssam /*
1612846Ssam  * Backwards compatible ftime.
1712846Ssam  */
1812846Ssam 
1912846Ssam /* from old timeb.h */
2012846Ssam struct timeb {
2112846Ssam 	time_t	time;
2212846Ssam 	u_short	millitm;
2312846Ssam 	short	timezone;
2412846Ssam 	short	dstflag;
2512846Ssam };
2612846Ssam 
2712846Ssam ftime(tp)
2812846Ssam 	register struct timeb *tp;
2912846Ssam {
3012846Ssam 	struct timeval t;
3112846Ssam 	struct timezone tz;
32*59996Storek 	struct tm *tm;
33*59996Storek 	time_t zero;
3412846Ssam 
3512846Ssam 	if (gettimeofday(&t, &tz) < 0)
3612846Ssam 		return (-1);
3712846Ssam 	tp->time = t.tv_sec;
3812846Ssam 	tp->millitm = t.tv_usec / 1000;
39*59996Storek 	if ((tm = localtime(&tp->time)) == NULL) {
40*59996Storek 		/* in the absence of anything better, use kernel's timezone */
41*59996Storek 		tp->timezone = tz.tz_minuteswest;
42*59996Storek 		tp->dstflag = tz.tz_dsttime;
43*59996Storek 	} else {
44*59996Storek 		tp->dstflag = tm->tm_isdst;
45*59996Storek 		if (tm->tm_isdst) {	/* tm_gmtoff has an offset applied */
46*59996Storek 			zero = 0;	/* try 0 and hope for the best */
47*59996Storek 			tp->timezone = -localtime(&zero)->tm_gmtoff / 60;
48*59996Storek 		} else
49*59996Storek 			tp->timezone = -tm->tm_gmtoff / 60;
50*59996Storek 	}
5112846Ssam }
52