.
[glibc-ports.git] / sysdeps / unix / sysv / aix / gettimeofday.c
blob34a92eb391f1c5e6151719be049f037ac514e196
1 /* Copyright (C) 1991,92,94,95,96,97,2001,2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <time.h>
21 #include <sys/time.h>
23 #ifndef HAVE_GNU_LD
24 # define __daylight daylight
25 # define __timezone timezone
26 # define __tzname tzname
27 #endif
29 #undef __gettimeofday
31 extern int rtc_upper (void);
32 extern int rtc_lower (void);
34 /* Assembler Routines to access the timer registers */
35 asm("\n\
36 .rtc_upper: mfspr 3,4 # copy RTCU to return register\n\
37 blr\n\
38 \n\
39 .rtc_lower: mfspr 3,5 # copy RTCL to return register\n\
40 blr\n\
41 ");
43 /* Get the current time of day and timezone information,
44 putting it into *TV and *TZ. If TZ is NULL, *TZ is not filled.
45 Returns 0 on success, -1 on errors. */
46 int
47 __gettimeofday (tv, tz)
48 struct timeval *tv;
49 struct timezone *tz;
51 int ts, tl, tu;
53 if (tv == NULL)
55 __set_errno (EINVAL);
56 return -1;
59 ts = rtc_upper (); /* Seconds. */
60 tl = rtc_lower (); /* Nanoseconds. */
61 tu = rtc_upper (); /* Check for a carry from. */
62 if (ts != tu) /* The lower reg to the upper. */
63 tl = rtc_lower (); /* Recover from the race condition. */
65 tv->tv_sec = (long int) (tu + (double) tl / 1000000000);
66 tv->tv_usec = (long int) ((double) tl / 1000);
68 if (tz != NULL)
70 const time_t timer = tv->tv_sec;
71 struct tm tm;
72 const struct tm *tmp;
74 const long int save_timezone = __timezone;
75 const long int save_daylight = __daylight;
76 char *save_tzname[2];
77 save_tzname[0] = __tzname[0];
78 save_tzname[1] = __tzname[1];
80 tmp = localtime_r (&timer, &tm);
82 tz->tz_minuteswest = __timezone / 60;
83 tz->tz_dsttime = __daylight;
85 __timezone = save_timezone;
86 __daylight = save_daylight;
87 __tzname[0] = save_tzname[0];
88 __tzname[1] = save_tzname[1];
90 if (tmp == NULL)
91 return -1;
94 return 0;
97 INTDEF(__gettimeofday)
98 weak_alias (__gettimeofday, gettimeofday)