]>
Commit | Line | Data |
---|---|---|
b24be05f RM |
1 | /* localtime -- convert `time_t' to `struct tm' in local time zone |
2 | Copyright (C) 1991, 92, 93, 95, 96 Free Software Foundation, Inc. | |
28f540f4 RM |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Library General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Library General Public | |
16 | License along with the GNU C Library; see the file COPYING.LIB. If | |
17 | not, write to the Free Software Foundation, Inc., 675 Mass Ave, | |
18 | Cambridge, MA 02139, USA. */ | |
19 | ||
c2216480 | 20 | #include <errno.h> |
28f540f4 | 21 | #include <time.h> |
b24be05f | 22 | #include <libc-lock.h> |
28f540f4 | 23 | |
c2216480 RM |
24 | /* The C Standard says that localtime and gmtime return the same pointer. */ |
25 | struct tm _tmbuf; | |
28f540f4 | 26 | |
28f540f4 RM |
27 | /* Return the `struct tm' representation of *TIMER in the local timezone. */ |
28 | struct tm * | |
6c2f0507 RM |
29 | localtime (timer) |
30 | const time_t *timer; | |
c2216480 RM |
31 | { |
32 | return __localtime_r (timer, &_tmbuf); | |
33 | } | |
34 | ||
35 | struct tm * | |
36 | __localtime_r (timer, tp) | |
37 | const time_t *timer; | |
38 | struct tm *tp; | |
28f540f4 | 39 | { |
b24be05f RM |
40 | /* This lock is defined in tzset.c and locks all the data defined there |
41 | and in tzfile.c; the internal functions do no locking themselves. | |
42 | This lock is only taken here and in `tzset'. */ | |
a5113b14 | 43 | __libc_lock_define (extern, __tzset_lock) |
b24be05f | 44 | extern int __tzset_run, __use_tzfile; |
6c2f0507 RM |
45 | extern int __tz_compute __P ((time_t timer, struct tm *tp)); |
46 | extern int __tzfile_compute __P ((time_t timer, | |
47 | long int *leap_correct, int *leap_hit)); | |
28f540f4 RM |
48 | long int leap_correction; |
49 | int leap_extra_secs; | |
50 | ||
51 | if (timer == NULL) | |
52 | { | |
c4029823 | 53 | __set_errno (EINVAL); |
28f540f4 RM |
54 | return NULL; |
55 | } | |
56 | ||
b24be05f RM |
57 | __libc_lock_lock (__tzset_lock); |
58 | ||
59 | /* Make sure the database is initialized. */ | |
60 | if (! __tzset_run) | |
61 | __tzset (); | |
28f540f4 RM |
62 | |
63 | if (__use_tzfile) | |
64 | { | |
65 | if (! __tzfile_compute (*timer, &leap_correction, &leap_extra_secs)) | |
b24be05f | 66 | tp = NULL; |
28f540f4 RM |
67 | } |
68 | else | |
69 | { | |
b24be05f RM |
70 | tp = __gmtime_r (timer, tp); |
71 | if (tp && ! __tz_compute (*timer, tp)) | |
72 | tp = NULL; | |
28f540f4 RM |
73 | leap_correction = 0L; |
74 | leap_extra_secs = 0; | |
75 | } | |
76 | ||
b24be05f RM |
77 | if (tp) |
78 | { | |
79 | __offtime (timer, __timezone - leap_correction, tp); | |
80 | tp->tm_sec += leap_extra_secs; | |
81 | tp->tm_isdst = __daylight; | |
82 | tp->tm_gmtoff = __timezone; | |
83 | tp->tm_zone = __tzname[__daylight]; | |
84 | } | |
85 | ||
86 | __libc_lock_unlock (__tzset_lock); | |
87 | ||
28f540f4 RM |
88 | return tp; |
89 | } | |
c2216480 | 90 | weak_alias (__localtime_r, localtime_r) |