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