]> git.ipfire.org Git - thirdparty/glibc.git/blame - time/localtime.c
update from main archive
[thirdparty/glibc.git] / time / localtime.c
CommitLineData
b24be05f
RM
1/* localtime -- convert `time_t' to `struct tm' in local time zone
2Copyright (C) 1991, 92, 93, 95, 96 Free Software Foundation, Inc.
28f540f4
RM
3This file is part of the GNU C Library.
4
5The GNU C Library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public License as
7published by the Free Software Foundation; either version 2 of the
8License, or (at your option) any later version.
9
10The GNU C Library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with the GNU C Library; see the file COPYING.LIB. If
17not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18Cambridge, 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. */
25struct tm _tmbuf;
28f540f4 26
28f540f4
RM
27/* Return the `struct tm' representation of *TIMER in the local timezone. */
28struct tm *
6c2f0507
RM
29localtime (timer)
30 const time_t *timer;
c2216480
RM
31{
32 return __localtime_r (timer, &_tmbuf);
33}
34
35struct 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 90weak_alias (__localtime_r, localtime_r)