2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: tminit.c /main/3 1995/11/01 18:54:45 rswiston $ */
24 /***************************************************************
26 * AT&T - PROPRIETARY *
28 * THIS IS PROPRIETARY SOURCE CODE LICENSED BY *
31 * Copyright (c) 1995 AT&T Corp. *
32 * All Rights Reserved *
34 * This software is licensed by AT&T Corp. *
35 * under the terms and conditions of the license in *
36 * http://www.research.att.com/orgs/ssr/book/reuse *
38 * This software was created by the *
39 * Software Engineering Research Department *
40 * AT&T Bell Laboratories *
42 * For further information contact *
43 * gsf@research.att.com *
45 ***************************************************************/
47 /* : : generated by proto : : */
49 #if !defined(__PROTO__)
50 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
51 #if defined(__cplusplus)
52 #define __MANGLE__ "C"
57 #define __PROTO__(x) x
59 #define __PARAM__(n,o) n
60 #if !defined(__STDC__) && !defined(__cplusplus)
61 #if !defined(c_plusplus)
72 #define __PROTO__(x) ()
73 #define __OTORP__(x) x
74 #define __PARAM__(n,o) o
82 #if defined(__cplusplus) || defined(c_plusplus)
83 #define __VARARG__ ...
87 #if defined(__STDARG__)
88 #define __VA_START__(p,a) va_start(p,a)
90 #define __VA_START__(p,a) va_start(p)
99 #if _DLL_INDIRECT_DATA && _DLL
100 #define tzname (_ast_getdll()->_ast_tzname)
103 #define _dat_tzname 1
108 #define _dat_tzname 1
109 #define tzname _tzname
114 extern __MANGLE__ char* tzname[];
121 static const Namval_t options[] =
124 "format", TM_DEFAULT,
134 * return minutes west of GMT for local time clock
136 * isdst will point to non-zero if DST is in effect
137 * this routine also kicks in the local initialization
141 tzwest __PARAM__((time_t* clock, int* isdst), (clock, isdst)) __OTORP__(time_t* clock; int* isdst;){
148 * convert to GMT assuming local time
151 tp = (Tm_t*)gmtime(clock);
157 * localtime() handles DST and GMT offset
160 tp = (Tm_t*)localtime(clock);
161 if (n = tp->tm_yday - n)
164 else if (n < -1) n = 1;
166 *isdst = tp->tm_isdst;
167 return((h - tp->tm_hour - n * 24) * 60 + m - tp->tm_min);
171 * stropt() option handler
175 tmopt __PARAM__((__V_* a, const __V_* p, int n, const char* v), (a, p, n, v)) __OTORP__(__V_* a; const __V_* p; int n; const char* v;){
179 if (p) switch (((Namval_t*)p)->value)
182 tm_info.deformat = (n && (n = strlen(v)) > 0 && (n < 2 || v[n-2] != '%' || v[n-1] != '?')) ? strdup(v) : tm_info.format[TM_DEFAULT];
185 tm_info.local->type = (n && *v) ? ((zp = tmtype(v, NiL)) ? zp->type : strdup(v)) : 0;
188 if (n) tm_info.flags |= ((Namval_t*)p)->value;
189 else tm_info.flags &= ~((Namval_t*)p)->value;
196 * initialize the local timezone
200 tmlocal __PARAM__((void), ()){
201 register Tm_zone_t* zp;
212 static Tm_zone_t local;
215 * tm_info.format language support goes here
222 local.standard = tzname[0];
223 local.daylight = tzname[1];
225 tm_info.format = tm_data.format;
226 tm_info.deformat = tm_data.format[TM_DEFAULT];
232 tm_info.zone = tm_info.local = &local;
234 n = tzwest(&now, &isdst);
237 * compute local DST offset by roaming
238 * through the last 12 months until tzwest() changes
241 for (i = 0; i < 12; i++)
243 now -= 31 * 24 * 60 * 60;
244 if ((m = tzwest(&now, &isdst)) != n)
260 * now get the time zone names
270 if (!local.standard) local.standard = tzname[0];
271 if (!local.daylight) local.daylight = tzname[1];
275 if ((s = getenv("TZNAME")) && *s && (s = strdup(s)))
282 if (s = strchr(s, ',')) *s++ = 0;
286 else if ((s = getenv("TZ")) && *s && *s != ':' && (s = strdup(s)))
289 * POSIX style but skipped by localtime()
293 if (*++s && *++s && *++s)
305 * tm_data.zone table lookup
309 for (zp = tm_data.zone; zp->standard; zp++)
311 if (zp->type) t = zp->type;
312 if (zp->west == n && zp->dst == m)
315 local.standard = zp->standard;
316 if (!(s = zp->daylight))
318 s = tmpoff(buf, zp->standard, 0, 0);
320 tmpoff(s, tm_info.format[TM_DT], m, TM_DST);
333 s = tmpoff(buf, tm_info.format[TM_UT], n, 0);
334 local.standard = strdup(buf);
336 tmpoff(s, tm_info.format[TM_UT], m, TM_DST);
337 local.daylight = strdup(buf);
345 stropt(getenv("TM_OPTIONS"), options, sizeof(*options), tmopt, NiL);
348 * the time zone type is probably related to the locale
355 for (zp = tm_data.zone; zp->standard; zp++)
357 if (zp->type) t = zp->type;
358 if (tmword(s, NiL, zp->standard, NiL, 0))
370 if (!(tm_info.flags & TM_ADJUST))
372 now = (time_t)78811200; /* Jun 30 1972 23:59:60 */
373 tp = (Tm_t*)localtime(&now);
374 if (tp->tm_sec != 60) tm_info.flags |= TM_ADJUST;
376 if (!(tm_info.flags & TM_UTC))
379 for (zp = tm_data.zone; !zp->type && zp->standard; zp++)
380 if (tmword(s, NiL, zp->standard, NiL, 0))
382 tm_info.flags |= TM_UTC;
393 tminit __PARAM__((register Tm_zone_t* zp), (zp)) __OTORP__(register Tm_zone_t* zp;){
394 if (!tm_info.local) tmlocal();
395 if (!zp) zp = tm_info.local;
396 #if HUH950804 /* it only worked on systems that ignored TZ=...! */
397 if (zp != tm_info.zone)
405 s += sfsprintf(s, sizeof(buf), "TZ=%s%d", zp->standard, zp->west / 60);
408 s += sfsprintf(s, sizeof(buf) - (s - buf), "%s", zp->daylight);
409 if (zp->dst != TM_DST) sfsprintf(s, sizeof(buf) - (s - buf), "%d", zp->dst);