Back to index

glibc  2.9
gettimeofday.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,1992,1994-1997,2002,2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <time.h>
00021 #include <sys/time.h>
00022 
00023 #undef __gettimeofday
00024 
00025 /* Get the current time of day and timezone information,
00026    putting it into *TV and *TZ.  If TZ is NULL, *TZ is not filled.
00027    Returns 0 on success, -1 on errors.  */
00028 int
00029 __gettimeofday (tv, tz)
00030      struct timeval *tv;
00031      struct timezone *tz;
00032 {
00033   if (tv == NULL)
00034     {
00035       __set_errno (EINVAL);
00036       return -1;
00037     }
00038 
00039   tv->tv_sec = (long int) time ((time_t *) NULL);
00040   tv->tv_usec = 0L;
00041 
00042   if (tz != NULL)
00043     {
00044       const time_t timer = tv->tv_sec;
00045       struct tm tm;
00046       const struct tm *tmp;
00047 
00048       const long int save_timezone = __timezone;
00049       const long int save_daylight = __daylight;
00050       char *save_tzname[2];
00051       save_tzname[0] = __tzname[0];
00052       save_tzname[1] = __tzname[1];
00053 
00054       tmp = localtime_r (&timer, &tm);
00055 
00056       tz->tz_minuteswest = __timezone / 60;
00057       tz->tz_dsttime = __daylight;
00058 
00059       __timezone = save_timezone;
00060       __daylight = save_daylight;
00061       __tzname[0] = save_tzname[0];
00062       __tzname[1] = save_tzname[1];
00063 
00064       if (tmp == NULL)
00065        return -1;
00066     }
00067 
00068   return 0;
00069 }
00070 
00071 INTDEF(__gettimeofday)
00072 weak_alias (__gettimeofday, gettimeofday)