]> git.saurik.com Git - apple/libc.git/blob - sys/gettimeofday.c
Libc-262.tar.gz
[apple/libc.git] / sys / gettimeofday.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
24 *
25 * File: libc/sys/gettimeofday.c
26 */
27
28 #include <time.h>
29 #include <tzfile.h>
30 #include <sys/time.h>
31 #include <errno.h>
32 #include <sys/syscall.h>
33 #include <unistd.h>
34
35 int gettimeofday (struct timeval *tp, struct timezone *tzp)
36 {
37 static int validtz = 0;
38 static struct timezone cached_tz = {0};
39 struct timeval localtv;
40 #ifdef __ppc__
41 extern __ppc_gettimeofday(struct timeval *, struct timezone *);
42 #endif
43
44 if (tzp && (tp == NULL) && (validtz == 0)) {
45 tp = &localtv;
46 }
47
48 #ifdef __ppc__
49 if(__ppc_gettimeofday(tp, tzp))
50 return(-1);
51 #else
52 if (syscall (SYS_gettimeofday, tp, tzp) < 0) {
53 return (-1);
54 }
55 #endif
56 if (tzp) {
57 if (validtz == 0) {
58 struct tm *localtm = localtime ((time_t *)&tp->tv_sec);
59 cached_tz.tz_dsttime = localtm->tm_isdst;
60 cached_tz.tz_minuteswest =
61 (-localtm->tm_gmtoff / SECSPERMIN) +
62 (localtm->tm_isdst * MINSPERHOUR);
63 validtz = 1;
64 }
65 tzp->tz_dsttime = cached_tz.tz_dsttime;
66 tzp->tz_minuteswest = cached_tz.tz_minuteswest;
67 }
68 return (0);
69 }