]> git.saurik.com Git - apple/libc.git/blob - sys.subproj/gettimeofday.c
Libc-166.tar.gz
[apple/libc.git] / sys.subproj / 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
40 if (syscall (SYS_gettimeofday, tp, tzp) < 0) {
41 return (-1);
42 }
43 if (validtz == 0) {
44 struct tm *localtm = localtime ((time_t *)&tp->tv_sec);
45 cached_tz.tz_dsttime = localtm->tm_isdst;
46 cached_tz.tz_minuteswest =
47 (-localtm->tm_gmtoff / SECSPERMIN) +
48 (localtm->tm_isdst * MINSPERHOUR);
49 validtz = 1;
50 }
51 if (tzp) {
52 tzp->tz_dsttime = cached_tz.tz_dsttime;
53 tzp->tz_minuteswest = cached_tz.tz_minuteswest;
54 }
55 return (0);
56 }