2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * Most of this code is in the public domain, so clarified as of
9 * June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov).
11 * All modifications to this code to abstract timezones away from
12 * the environment are by Tilghman Lesher, <tlesher@vcch.com>, with
13 * the copyright assigned to Digium.
15 * See http://www.asterisk.org for more information about
16 * the Asterisk project. Please do not directly contact
17 * any of the maintainers of this project for assistance;
18 * the project provides a web site, mailing lists and IRC
19 * channels for your use.
21 * This program is free software, distributed under the terms of
22 * the GNU General Public License Version 2. See the LICENSE file
23 * at the top of the source tree.
28 * Multi-timezone Localtime code
30 * The original source from this file may be obtained from ftp://elsie.nci.nih.gov/pub/
34 ** This file is in the public domain, so clarified as of
35 ** 1996-06-05 by Arthur David Olson.
39 ** Leap second handling from Bradley White.
40 ** POSIX-style TZ environment variable handling from Guy Harris.
49 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
58 #include "asterisk/lock.h"
59 #include "asterisk/localtime.h"
60 #include "asterisk/strings.h"
61 #include "asterisk/linkedlists.h"
62 #include "asterisk/utils.h"
66 static char __attribute__((unused)) elsieid[] = "@(#)localtime.c 8.5";
67 #endif /* !defined NOID */
68 #endif /* !defined lint */
70 #ifndef TZ_ABBR_MAX_LEN
71 #define TZ_ABBR_MAX_LEN 16
72 #endif /* !defined TZ_ABBR_MAX_LEN */
74 #ifndef TZ_ABBR_CHAR_SET
75 #define TZ_ABBR_CHAR_SET \
76 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
77 #endif /* !defined TZ_ABBR_CHAR_SET */
79 #ifndef TZ_ABBR_ERR_CHAR
80 #define TZ_ABBR_ERR_CHAR '_'
81 #endif /* !defined TZ_ABBR_ERR_CHAR */
84 ** SunOS 4.1.1 headers lack O_BINARY.
88 #define OPEN_MODE (O_RDONLY | O_BINARY)
89 #endif /* defined O_BINARY */
91 #define OPEN_MODE O_RDONLY
92 #endif /* !defined O_BINARY */
94 static const char gmt[] = "GMT";
95 static const struct timeval WRONG = { 0, 0 };
98 * The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
99 * We default to US rules as of 1999-08-17.
100 * POSIX 1003.1 section 8.1.1 says that the default DST rules are
101 * implementation dependent; for historical reasons, US rules are a
104 #ifndef TZDEFRULESTRING
105 #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
106 #endif /* !defined TZDEFDST */
108 /*!< \brief time type information */
109 struct ttinfo { /* time type information */
110 long tt_gmtoff; /* UTC offset in seconds */
111 int tt_isdst; /* used to set tm_isdst */
112 int tt_abbrind; /* abbreviation list index */
113 int tt_ttisstd; /* TRUE if transition is std time */
114 int tt_ttisgmt; /* TRUE if transition is UTC */
117 /*! \brief leap second information */
118 struct lsinfo { /* leap second information */
119 time_t ls_trans; /* transition time */
120 long ls_corr; /* correction to apply */
123 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
126 #define MY_TZNAME_MAX TZNAME_MAX
127 #endif /* defined TZNAME_MAX */
129 #define MY_TZNAME_MAX 255
130 #endif /* !defined TZNAME_MAX */
131 #ifndef TZ_STRLEN_MAX
132 #define TZ_STRLEN_MAX 255
133 #endif /* !defined TZ_STRLEN_MAX */
136 /*! Name of the file that this references */
137 char name[TZ_STRLEN_MAX + 1];
144 time_t ats[TZ_MAX_TIMES];
145 unsigned char types[TZ_MAX_TIMES];
146 struct ttinfo ttis[TZ_MAX_TYPES];
147 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
148 (2 * (MY_TZNAME_MAX + 1)))];
149 struct lsinfo lsis[TZ_MAX_LEAPS];
150 AST_LIST_ENTRY(state) list;
154 int r_type; /* type of rule--see below */
155 int r_day; /* day number of rule */
156 int r_week; /* week number of rule */
157 int r_mon; /* month number of rule */
158 long r_time; /* transition time of rule */
161 #define JULIAN_DAY 0 /* Jn - Julian day */
162 #define DAY_OF_YEAR 1 /* n - day of year */
163 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
166 ** Prototypes for static functions.
169 static long detzcode P((const char * codep));
170 static time_t detzcode64 P((const char * codep));
171 static int differ_by_repeat P((time_t t1, time_t t0));
172 static const char * getzname P((const char * strp));
173 static const char * getqzname P((const char * strp, const int delim));
174 static const char * getnum P((const char * strp, int * nump, int min,
176 static const char * getsecs P((const char * strp, long * secsp));
177 static const char * getoffset P((const char * strp, long * offsetp));
178 static const char * getrule P((const char * strp, struct rule * rulep));
179 static int gmtload P((struct state * sp));
180 static struct ast_tm * gmtsub P((const struct timeval * timep, long offset,
181 struct ast_tm * tmp));
182 static struct ast_tm * localsub P((const struct timeval * timep, long offset,
183 struct ast_tm * tmp, const struct state *sp));
184 static int increment_overflow P((int * number, int delta));
185 static int leaps_thru_end_of P((int y));
186 static int long_increment_overflow P((long * number, int delta));
187 static int long_normalize_overflow P((long * tensptr,
188 int * unitsptr, const int base));
189 static int normalize_overflow P((int * tensptr, int * unitsptr,
191 static struct timeval time1 P((struct ast_tm * tmp,
192 struct ast_tm * (*funcp) P((const struct timeval *,
193 long, struct ast_tm *, const struct state *sp)),
194 long offset, const struct state *sp));
195 static struct timeval time2 P((struct ast_tm *tmp,
196 struct ast_tm * (*funcp) P((const struct timeval *,
197 long, struct ast_tm*, const struct state *sp)),
198 long offset, int * okayp, const struct state *sp));
199 static struct timeval time2sub P((struct ast_tm *tmp,
200 struct ast_tm * (*funcp) (const struct timeval *,
201 long, struct ast_tm*, const struct state *sp),
202 long offset, int * okayp, int do_norm_secs, const struct state *sp));
203 static struct ast_tm * timesub P((const struct timeval * timep, long offset,
204 const struct state * sp, struct ast_tm * tmp));
205 static int tmcomp P((const struct ast_tm * atmp,
206 const struct ast_tm * btmp));
207 static time_t transtime P((time_t janfirst, int year,
208 const struct rule * rulep, long offset));
209 static int tzload P((const char * name, struct state * sp,
211 static int tzparse P((const char * name, struct state * sp,
214 static AST_LIST_HEAD_STATIC(zonelist, state);
216 #ifndef TZ_STRLEN_MAX
217 #define TZ_STRLEN_MAX 255
218 #endif /* !defined TZ_STRLEN_MAX */
221 ** Section 4.12.3 of X3.159-1989 requires that
222 ** Except for the strftime function, these functions [asctime,
223 ** ctime, gmtime, localtime] return values in one of two static
224 ** objects: a broken-down time structure and an array of char.
225 ** Thanks to Paul Eggert for noting this.
228 static long detzcode(const char * const codep)
233 result = (codep[0] & 0x80) ? ~0L : 0;
234 for (i = 0; i < 4; ++i)
235 result = (result << 8) | (codep[i] & 0xff);
239 static time_t detzcode64(const char * const codep)
244 result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0;
245 for (i = 0; i < 8; ++i)
246 result = result * 256 + (codep[i] & 0xff);
250 static int differ_by_repeat(const time_t t1, const time_t t0)
252 const long long at1 = t1, at0 = t0;
253 if (TYPE_INTEGRAL(time_t) &&
254 TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
256 return at1 - at0 == SECSPERREPEAT;
259 static int tzload(const char *name, struct state * const sp, const int doextend)
267 struct tzhead tzhead;
268 char buf[2 * sizeof(struct tzhead) +
273 if (name == NULL && (name = TZDEFAULT) == NULL)
278 ** Section 4.9.1 of the C standard says that
279 ** "FILENAME_MAX expands to an integral constant expression
280 ** that is the size needed for an array of char large enough
281 ** to hold the longest file name string that the implementation
282 ** guarantees can be opened."
284 char fullname[FILENAME_MAX + 1];
288 doaccess = name[0] == '/';
290 if ((p = TZDIR) == NULL)
292 if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
294 (void) strcpy(fullname, p);
295 (void) strcat(fullname, "/");
296 (void) strcat(fullname, name);
298 ** Set doaccess if '.' (as in "../") shows up in name.
300 if (strchr(name, '.') != NULL)
304 if (doaccess && access(name, R_OK) != 0)
306 if ((fid = open(name, OPEN_MODE)) == -1)
309 nread = read(fid, u.buf, sizeof u.buf);
310 if (close(fid) < 0 || nread <= 0)
312 for (stored = 4; stored <= 8; stored *= 2) {
316 ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt);
317 ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt);
318 sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt);
319 sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt);
320 sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt);
321 sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt);
322 p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt;
323 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
324 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
325 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
326 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
327 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
328 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
330 if (nread - (p - u.buf) <
331 sp->timecnt * stored + /* ats */
332 sp->timecnt + /* types */
333 sp->typecnt * 6 + /* ttinfos */
334 sp->charcnt + /* chars */
335 sp->leapcnt * (stored + 4) + /* lsinfos */
336 ttisstdcnt + /* ttisstds */
337 ttisgmtcnt) /* ttisgmts */
339 for (i = 0; i < sp->timecnt; ++i) {
340 sp->ats[i] = (stored == 4) ?
341 detzcode(p) : detzcode64(p);
344 for (i = 0; i < sp->timecnt; ++i) {
345 sp->types[i] = (unsigned char) *p++;
346 if (sp->types[i] >= sp->typecnt)
349 for (i = 0; i < sp->typecnt; ++i) {
350 struct ttinfo * ttisp;
352 ttisp = &sp->ttis[i];
353 ttisp->tt_gmtoff = detzcode(p);
355 ttisp->tt_isdst = (unsigned char) *p++;
356 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
358 ttisp->tt_abbrind = (unsigned char) *p++;
359 if (ttisp->tt_abbrind < 0 ||
360 ttisp->tt_abbrind > sp->charcnt)
363 for (i = 0; i < sp->charcnt; ++i)
365 sp->chars[i] = '\0'; /* ensure '\0' at end */
366 for (i = 0; i < sp->leapcnt; ++i) {
367 struct lsinfo * lsisp;
369 lsisp = &sp->lsis[i];
370 lsisp->ls_trans = (stored == 4) ?
371 detzcode(p) : detzcode64(p);
373 lsisp->ls_corr = detzcode(p);
376 for (i = 0; i < sp->typecnt; ++i) {
377 struct ttinfo * ttisp;
379 ttisp = &sp->ttis[i];
381 ttisp->tt_ttisstd = FALSE;
383 ttisp->tt_ttisstd = *p++;
384 if (ttisp->tt_ttisstd != TRUE &&
385 ttisp->tt_ttisstd != FALSE)
389 for (i = 0; i < sp->typecnt; ++i) {
390 struct ttinfo * ttisp;
392 ttisp = &sp->ttis[i];
394 ttisp->tt_ttisgmt = FALSE;
396 ttisp->tt_ttisgmt = *p++;
397 if (ttisp->tt_ttisgmt != TRUE &&
398 ttisp->tt_ttisgmt != FALSE)
403 ** Out-of-sort ats should mean we're running on a
404 ** signed time_t system but using a data file with
405 ** unsigned values (or vice versa).
407 for (i = 0; i < sp->timecnt - 2; ++i)
408 if (sp->ats[i] > sp->ats[i + 1]) {
410 if (TYPE_SIGNED(time_t)) {
412 ** Ignore the end (easy).
417 ** Ignore the beginning (harder).
421 for (j = 0; j + i < sp->timecnt; ++j) {
422 sp->ats[j] = sp->ats[j + i];
423 sp->types[j] = sp->types[j + i];
430 ** If this is an old file, we're done.
432 if (u.tzhead.tzh_version[0] == '\0')
435 for (i = 0; i < nread; ++i)
438 ** If this is a narrow integer time_t system, we're done.
440 if (stored >= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t))
443 if (doextend && nread > 2 &&
444 u.buf[0] == '\n' && u.buf[nread - 1] == '\n' &&
445 sp->typecnt + 2 <= TZ_MAX_TYPES) {
449 u.buf[nread - 1] = '\0';
450 result = tzparse(&u.buf[1], &ts, FALSE);
451 if (result == 0 && ts.typecnt == 2 &&
452 sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) {
453 for (i = 0; i < 2; ++i)
454 ts.ttis[i].tt_abbrind +=
456 for (i = 0; i < ts.charcnt; ++i)
457 sp->chars[sp->charcnt++] =
460 while (i < ts.timecnt &&
462 sp->ats[sp->timecnt - 1])
464 while (i < ts.timecnt &&
465 sp->timecnt < TZ_MAX_TIMES) {
466 sp->ats[sp->timecnt] =
468 sp->types[sp->timecnt] =
474 sp->ttis[sp->typecnt++] = ts.ttis[0];
475 sp->ttis[sp->typecnt++] = ts.ttis[1];
478 i = 2 * YEARSPERREPEAT;
479 sp->goback = sp->goahead = sp->timecnt > i;
480 sp->goback = sp->goback && sp->types[i] == sp->types[0] &&
481 differ_by_repeat(sp->ats[i], sp->ats[0]);
482 sp->goahead = sp->goahead &&
483 sp->types[sp->timecnt - 1] == sp->types[sp->timecnt - 1 - i] &&
484 differ_by_repeat(sp->ats[sp->timecnt - 1],
485 sp->ats[sp->timecnt - 1 - i]);
489 static const int mon_lengths[2][MONSPERYEAR] = {
490 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
491 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
494 static const int year_lengths[2] = {
495 DAYSPERNYEAR, DAYSPERLYEAR
499 ** Given a pointer into a time zone string, scan until a character that is not
500 ** a valid character in a zone name is found. Return a pointer to that
504 static const char * getzname(const char *strp)
508 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
515 ** Given a pointer into an extended time zone string, scan until the ending
516 ** delimiter of the zone name is located. Return a pointer to the delimiter.
518 ** As with getzname above, the legal character set is actually quite
519 ** restricted, with other characters producing undefined results.
520 ** We don't do any checking here; checking is done later in common-case code.
523 static const char * getqzname(const char *strp, const int delim)
527 while ((c = *strp) != '\0' && c != delim)
533 ** Given a pointer into a time zone string, extract a number from that string.
534 ** Check that the number is within a specified range; if it is not, return
536 ** Otherwise, return a pointer to the first character not part of the number.
539 static const char *getnum(const char *strp, int *nump, const int min, const int max)
544 if (strp == NULL || !is_digit(c = *strp))
548 num = num * 10 + (c - '0');
550 return NULL; /* illegal value */
552 } while (is_digit(c));
554 return NULL; /* illegal value */
560 ** Given a pointer into a time zone string, extract a number of seconds,
561 ** in hh[:mm[:ss]] form, from the string.
562 ** If any error occurs, return NULL.
563 ** Otherwise, return a pointer to the first character not part of the number
567 static const char *getsecs(const char *strp, long * const secsp)
572 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
573 ** "M10.4.6/26", which does not conform to Posix,
574 ** but which specifies the equivalent of
575 ** ``02:00 on the first Sunday on or after 23 Oct''.
577 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
580 *secsp = num * (long) SECSPERHOUR;
583 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
586 *secsp += num * SECSPERMIN;
589 /* `SECSPERMIN' allows for leap seconds. */
590 strp = getnum(strp, &num, 0, SECSPERMIN);
600 ** Given a pointer into a time zone string, extract an offset, in
601 ** [+-]hh[:mm[:ss]] form, from the string.
602 ** If any error occurs, return NULL.
603 ** Otherwise, return a pointer to the first character not part of the time.
606 static const char *getoffset(const char *strp, long *offsetp)
613 } else if (*strp == '+')
615 strp = getsecs(strp, offsetp);
617 return NULL; /* illegal time */
619 *offsetp = -*offsetp;
624 ** Given a pointer into a time zone string, extract a rule in the form
625 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
626 ** If a valid rule is not found, return NULL.
627 ** Otherwise, return a pointer to the first character not part of the rule.
630 static const char *getrule(const char *strp, struct rule *rulep)
636 rulep->r_type = JULIAN_DAY;
638 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
639 } else if (*strp == 'M') {
643 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
645 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
650 strp = getnum(strp, &rulep->r_week, 1, 5);
655 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
656 } else if (is_digit(*strp)) {
660 rulep->r_type = DAY_OF_YEAR;
661 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
662 } else return NULL; /* invalid format */
670 strp = getsecs(strp, &rulep->r_time);
671 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
676 ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
677 ** year, a rule, and the offset from UTC at the time that rule takes effect,
678 ** calculate the Epoch-relative time that rule takes effect.
681 static time_t transtime(const time_t janfirst, const int year, const struct rule *rulep, const long offset)
686 int d, m1, yy0, yy1, yy2, dow;
689 leapyear = isleap(year);
690 switch (rulep->r_type) {
694 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
696 ** In non-leap years, or if the day number is 59 or less, just
697 ** add SECSPERDAY times the day number-1 to the time of
698 ** January 1, midnight, to get the day.
700 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
701 if (leapyear && rulep->r_day >= 60)
708 ** Just add SECSPERDAY times the day number to the time of
709 ** January 1, midnight, to get the day.
711 value = janfirst + rulep->r_day * SECSPERDAY;
714 case MONTH_NTH_DAY_OF_WEEK:
716 ** Mm.n.d - nth "dth day" of month m.
719 for (i = 0; i < rulep->r_mon - 1; ++i)
720 value += mon_lengths[leapyear][i] * SECSPERDAY;
723 ** Use Zeller's Congruence to get day-of-week of first day of
726 m1 = (rulep->r_mon + 9) % 12 + 1;
727 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
730 dow = ((26 * m1 - 2) / 10 +
731 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
736 ** "dow" is the day-of-week of the first day of the month. Get
737 ** the day-of-month (zero-origin) of the first "dow" day of the
740 d = rulep->r_day - dow;
743 for (i = 1; i < rulep->r_week; ++i) {
744 if (d + DAYSPERWEEK >=
745 mon_lengths[leapyear][rulep->r_mon - 1])
751 ** "d" is the day-of-month (zero-origin) of the day we want.
753 value += d * SECSPERDAY;
758 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
759 ** question. To get the Epoch-relative time of the specified local
760 ** time on that day, add the transition time and the current offset
763 return value + rulep->r_time + offset;
767 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
771 static int tzparse(const char *name, struct state *sp, const int lastditch)
773 const char * stdname;
774 const char * dstname;
780 unsigned char * typep;
787 stdlen = strlen(name); /* length of standard zone name */
789 if (stdlen >= sizeof sp->chars)
790 stdlen = (sizeof sp->chars) - 1;
796 name = getqzname(name, '>');
799 stdlen = name - stdname;
802 name = getzname(name);
803 stdlen = name - stdname;
807 name = getoffset(name, &stdoffset);
811 load_result = tzload(TZDEFRULES, sp, FALSE);
812 if (load_result != 0)
813 sp->leapcnt = 0; /* so, we're off a little */
817 name = getqzname(name, '>');
820 dstlen = name - dstname;
824 name = getzname(name);
825 dstlen = name - dstname; /* length of DST zone name */
827 if (*name != '\0' && *name != ',' && *name != ';') {
828 name = getoffset(name, &dstoffset);
831 } else dstoffset = stdoffset - SECSPERHOUR;
832 if (*name == '\0' && load_result != 0)
833 name = TZDEFRULESTRING;
834 if (*name == ',' || *name == ';') {
843 if ((name = getrule(name, &start)) == NULL)
847 if ((name = getrule(name, &end)) == NULL)
851 sp->typecnt = 2; /* standard time and DST */
853 ** Two transitions per year, from EPOCH_YEAR forward.
855 sp->ttis[0].tt_gmtoff = -dstoffset;
856 sp->ttis[0].tt_isdst = 1;
857 sp->ttis[0].tt_abbrind = stdlen + 1;
858 sp->ttis[1].tt_gmtoff = -stdoffset;
859 sp->ttis[1].tt_isdst = 0;
860 sp->ttis[1].tt_abbrind = 0;
865 for (year = EPOCH_YEAR;
866 sp->timecnt + 2 <= TZ_MAX_TIMES;
870 starttime = transtime(janfirst, year, &start,
872 endtime = transtime(janfirst, year, &end,
874 if (starttime > endtime) {
876 *typep++ = 1; /* DST ends */
878 *typep++ = 0; /* DST begins */
881 *typep++ = 0; /* DST begins */
883 *typep++ = 1; /* DST ends */
887 newfirst += year_lengths[isleap(year)] *
889 if (newfirst <= janfirst)
904 ** Initial values of theirstdoffset and theirdstoffset.
907 for (i = 0; i < sp->timecnt; ++i) {
909 if (!sp->ttis[j].tt_isdst) {
911 -sp->ttis[j].tt_gmtoff;
916 for (i = 0; i < sp->timecnt; ++i) {
918 if (sp->ttis[j].tt_isdst) {
920 -sp->ttis[j].tt_gmtoff;
925 ** Initially we're assumed to be in standard time.
928 theiroffset = theirstdoffset;
930 ** Now juggle transition times and types
931 ** tracking offsets as you do.
933 for (i = 0; i < sp->timecnt; ++i) {
935 sp->types[i] = sp->ttis[j].tt_isdst;
936 if (sp->ttis[j].tt_ttisgmt) {
937 /* No adjustment to transition time */
940 ** If summer time is in effect, and the
941 ** transition time was not specified as
942 ** standard time, add the summer time
943 ** offset to the transition time;
944 ** otherwise, add the standard time
945 ** offset to the transition time.
948 ** Transitions from DST to DDST
949 ** will effectively disappear since
950 ** POSIX provides for only one DST
953 if (isdst && !sp->ttis[j].tt_ttisstd) {
954 sp->ats[i] += dstoffset -
957 sp->ats[i] += stdoffset -
961 theiroffset = -sp->ttis[j].tt_gmtoff;
962 if (sp->ttis[j].tt_isdst)
963 theirdstoffset = theiroffset;
964 else theirstdoffset = theiroffset;
967 ** Finally, fill in ttis.
968 ** ttisstd and ttisgmt need not be handled.
970 sp->ttis[0].tt_gmtoff = -stdoffset;
971 sp->ttis[0].tt_isdst = FALSE;
972 sp->ttis[0].tt_abbrind = 0;
973 sp->ttis[1].tt_gmtoff = -dstoffset;
974 sp->ttis[1].tt_isdst = TRUE;
975 sp->ttis[1].tt_abbrind = stdlen + 1;
980 sp->typecnt = 1; /* only standard time */
982 sp->ttis[0].tt_gmtoff = -stdoffset;
983 sp->ttis[0].tt_isdst = 0;
984 sp->ttis[0].tt_abbrind = 0;
986 sp->charcnt = stdlen + 1;
988 sp->charcnt += dstlen + 1;
989 if ((size_t) sp->charcnt > sizeof sp->chars)
992 (void) strncpy(cp, stdname, stdlen);
996 (void) strncpy(cp, dstname, dstlen);
997 *(cp + dstlen) = '\0';
1002 static int gmtload(struct state *sp)
1004 if (tzload(gmt, sp, TRUE) != 0)
1005 return tzparse(gmt, sp, TRUE);
1010 static const struct state *ast_tzset(const char *zone)
1014 if (ast_strlen_zero(zone))
1015 zone = "/etc/localtime";
1017 AST_LIST_LOCK(&zonelist);
1018 AST_LIST_TRAVERSE(&zonelist, sp, list) {
1019 if (!strcmp(sp->name, zone)) {
1020 AST_LIST_UNLOCK(&zonelist);
1024 AST_LIST_UNLOCK(&zonelist);
1026 if (!(sp = ast_calloc(1, sizeof *sp)))
1029 if (tzload(zone, sp, TRUE) != 0) {
1030 if (zone[0] == ':' || tzparse(zone, sp, FALSE) != 0)
1033 ast_copy_string(sp->name, zone, sizeof(sp->name));
1034 AST_LIST_LOCK(&zonelist);
1035 AST_LIST_INSERT_TAIL(&zonelist, sp, list);
1036 AST_LIST_UNLOCK(&zonelist);
1041 ** The easy way to behave "as if no library function calls" localtime
1042 ** is to not call it--so we drop its guts into "localsub", which can be
1043 ** freely called. (And no, the PANS doesn't require the above behavior--
1044 ** but it *is* desirable.)
1046 ** The unused offset argument is for the benefit of mktime variants.
1049 static struct ast_tm *localsub(const struct timeval *timep, const long offset, struct ast_tm *tmp, const struct state *sp)
1051 const struct ttinfo * ttisp;
1053 struct ast_tm * result;
1055 memcpy(&t, timep, sizeof(t));
1058 return gmtsub(timep, offset, tmp);
1059 if ((sp->goback && t.tv_sec < sp->ats[0]) ||
1060 (sp->goahead && t.tv_sec > sp->ats[sp->timecnt - 1])) {
1061 struct timeval newt = t;
1064 int_fast64_t icycles;
1066 if (t.tv_sec < sp->ats[0])
1067 seconds = sp->ats[0] - t.tv_sec;
1068 else seconds = t.tv_sec - sp->ats[sp->timecnt - 1];
1070 tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR;
1073 if (tcycles - icycles >= 1 || icycles - tcycles >= 1)
1076 seconds *= YEARSPERREPEAT;
1077 seconds *= AVGSECSPERYEAR;
1078 if (t.tv_sec < sp->ats[0])
1079 newt.tv_sec += seconds;
1080 else newt.tv_sec -= seconds;
1081 if (newt.tv_sec < sp->ats[0] ||
1082 newt.tv_sec > sp->ats[sp->timecnt - 1])
1083 return NULL; /* "cannot happen" */
1084 result = localsub(&newt, offset, tmp, sp);
1085 if (result == tmp) {
1088 newy = tmp->tm_year;
1089 if (t.tv_sec < sp->ats[0])
1090 newy -= icycles * YEARSPERREPEAT;
1092 newy += icycles * YEARSPERREPEAT;
1093 tmp->tm_year = newy;
1094 if (tmp->tm_year != newy)
1099 if (sp->timecnt == 0 || t.tv_sec < sp->ats[0]) {
1101 while (sp->ttis[i].tt_isdst) {
1102 if (++i >= sp->typecnt) {
1109 int hi = sp->timecnt;
1112 int mid = (lo + hi) >> 1;
1114 if (t.tv_sec < sp->ats[mid])
1119 i = (int) sp->types[lo - 1];
1121 ttisp = &sp->ttis[i];
1123 ** To get (wrong) behavior that's compatible with System V Release 2.0
1124 ** you'd replace the statement below with
1125 ** t += ttisp->tt_gmtoff;
1126 ** timesub(&t, 0L, sp, tmp);
1128 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1129 tmp->tm_isdst = ttisp->tt_isdst;
1130 #ifndef SOLARIS /* Solaris doesn't have this element */
1131 tmp->tm_gmtoff = ttisp->tt_gmtoff;
1134 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1135 #endif /* defined TM_ZONE */
1136 tmp->tm_usec = timep->tv_usec;
1140 struct ast_tm *ast_localtime(const struct timeval *timep, struct ast_tm *tmp, const char *zone)
1142 const struct state *sp = ast_tzset(zone);
1143 memset(tmp, 0, sizeof(*tmp));
1144 return sp ? localsub(timep, 0L, tmp, sp) : NULL;
1148 ** This function provides informaton about daylight savings time
1149 ** for the given timezone. This includes whether it can determine
1150 ** if daylight savings is used for this timezone, the UTC times for
1151 ** when daylight savings transitions, and the offset in seconds from
1155 void ast_get_dst_info(const time_t * const timep, int *dst_enabled, time_t *dst_start, time_t *dst_end, int *gmt_off, const char * const zone)
1158 int transition1 = -1;
1159 int transition2 = -1;
1161 int bounds_exceeded = 0;
1163 const struct state *sp;
1165 if (NULL == dst_enabled)
1169 if (NULL == dst_start || NULL == dst_end || NULL == gmt_off)
1174 sp = ast_tzset(zone);
1178 /* If the desired time exceeds the bounds of the defined time transitions
1179 * then give give up on determining DST info and simply look for gmt offset
1180 * This requires that I adjust the given time using increments of Gregorian
1181 * repeats to place the time within the defined time transitions in the
1182 * timezone structure.
1184 if ((sp->goback && t < sp->ats[0]) ||
1185 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1187 int_fast64_t icycles;
1190 seconds = sp->ats[0] - t;
1191 else seconds = t - sp->ats[sp->timecnt - 1];
1193 tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR;
1196 if (tcycles - icycles >= 1 || icycles - tcycles >= 1)
1199 seconds *= YEARSPERREPEAT;
1200 seconds *= AVGSECSPERYEAR;
1206 if (t < sp->ats[0] || t > sp->ats[sp->timecnt - 1])
1207 return; /* "cannot happen" */
1209 bounds_exceeded = 1;
1212 if (sp->timecnt == 0 || t < sp->ats[0]) {
1213 /* I have no transition times or I'm before time */
1215 /* Find where I can get gmtoff */
1217 while (sp->ttis[i].tt_isdst)
1218 if (++i >= sp->typecnt) {
1222 *gmt_off = sp->ttis[i].tt_gmtoff;
1226 for (i = 1; i < sp->timecnt; ++i) {
1227 if (t < sp->ats[i]) {
1228 transition1 = sp->types[i - 1];
1229 transition2 = sp->types[i];
1233 /* if I found transition times that do not bounded the given time and these correspond to
1234 or the bounding zones do not reflect a changes in day light savings, then I do not have dst active */
1235 if (i >= sp->timecnt || 0 > transition1 || 0 > transition2 ||
1236 (sp->ttis[transition1].tt_isdst == sp->ttis[transition2].tt_isdst)) {
1238 *gmt_off = sp->ttis[sp->types[sp->timecnt -1]].tt_gmtoff;
1240 /* I have valid daylight savings information. */
1241 if(sp->ttis[transition2].tt_isdst)
1242 *gmt_off = sp->ttis[transition1].tt_gmtoff;
1244 *gmt_off = sp->ttis[transition2].tt_gmtoff;
1246 /* If I adjusted the time earlier, indicate that the dst is invalid */
1247 if (!bounds_exceeded) {
1249 /* Determine which of the bounds is the start of daylight savings and which is the end */
1250 if(sp->ttis[transition2].tt_isdst) {
1251 *dst_start = sp->ats[i];
1252 *dst_end = sp->ats[i -1];
1254 *dst_start = sp->ats[i -1];
1255 *dst_end = sp->ats[i];
1263 ** gmtsub is to gmtime as localsub is to localtime.
1266 static struct ast_tm *gmtsub(const struct timeval *timep, const long offset, struct ast_tm *tmp)
1268 struct ast_tm * result;
1271 AST_LIST_LOCK(&zonelist);
1272 AST_LIST_TRAVERSE(&zonelist, sp, list) {
1273 if (!strcmp(sp->name, "UTC"))
1278 if (!(sp = (struct state *) ast_calloc(1, sizeof *sp)))
1281 AST_LIST_INSERT_TAIL(&zonelist, sp, list);
1283 AST_LIST_UNLOCK(&zonelist);
1285 result = timesub(timep, offset, sp, tmp);
1288 ** Could get fancy here and deliver something such as
1289 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1290 ** but this is no time for a treasure hunt.
1295 tmp->TM_ZONE = sp->chars;
1296 #endif /* defined TM_ZONE */
1301 ** Return the number of leap years through the end of the given year
1302 ** where, to make the math easy, the answer for year zero is defined as zero.
1305 static int leaps_thru_end_of(const int y)
1307 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1308 -(leaps_thru_end_of(-(y + 1)) + 1);
1311 static struct ast_tm *timesub(const struct timeval *timep, const long offset, const struct state *sp, struct ast_tm *tmp)
1313 const struct lsinfo * lp;
1315 int idays; /* unsigned would be so 2003 */
1327 i = (sp == NULL) ? 0 : sp->leapcnt;
1330 if (timep->tv_sec >= lp->ls_trans) {
1331 if (timep->tv_sec == lp->ls_trans) {
1332 hit = ((i == 0 && lp->ls_corr > 0) ||
1333 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1336 sp->lsis[i].ls_trans ==
1337 sp->lsis[i - 1].ls_trans + 1 &&
1338 sp->lsis[i].ls_corr ==
1339 sp->lsis[i - 1].ls_corr + 1) {
1349 tdays = timep->tv_sec / SECSPERDAY;
1350 rem = timep->tv_sec - tdays * SECSPERDAY;
1351 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1357 tdelta = tdays / DAYSPERLYEAR;
1359 if (tdelta - idelta >= 1 || idelta - tdelta >= 1)
1362 idelta = (tdays < 0) ? -1 : 1;
1364 if (increment_overflow(&newy, idelta))
1366 leapdays = leaps_thru_end_of(newy - 1) -
1367 leaps_thru_end_of(y - 1);
1368 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1373 seconds = tdays * SECSPERDAY + 0.5;
1374 tdays = seconds / SECSPERDAY;
1375 rem += seconds - tdays * SECSPERDAY;
1378 ** Given the range, we can now fearlessly cast...
1381 rem += offset - corr;
1386 while (rem >= SECSPERDAY) {
1391 if (increment_overflow(&y, -1))
1393 idays += year_lengths[isleap(y)];
1395 while (idays >= year_lengths[isleap(y)]) {
1396 idays -= year_lengths[isleap(y)];
1397 if (increment_overflow(&y, 1))
1401 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1403 tmp->tm_yday = idays;
1405 ** The "extra" mods below avoid overflow problems.
1407 tmp->tm_wday = EPOCH_WDAY +
1408 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1409 (DAYSPERNYEAR % DAYSPERWEEK) +
1410 leaps_thru_end_of(y - 1) -
1411 leaps_thru_end_of(EPOCH_YEAR - 1) +
1413 tmp->tm_wday %= DAYSPERWEEK;
1414 if (tmp->tm_wday < 0)
1415 tmp->tm_wday += DAYSPERWEEK;
1416 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1418 tmp->tm_min = (int) (rem / SECSPERMIN);
1420 ** A positive leap second requires a special
1421 ** representation. This uses "... ??:59:60" et seq.
1423 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1424 ip = mon_lengths[isleap(y)];
1425 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1426 idays -= ip[tmp->tm_mon];
1427 tmp->tm_mday = (int) (idays + 1);
1430 tmp->TM_GMTOFF = offset;
1431 #endif /* defined TM_GMTOFF */
1432 tmp->tm_usec = timep->tv_usec;
1437 ** Adapted from code provided by Robert Elz, who writes:
1438 ** The "best" way to do mktime I think is based on an idea of Bob
1439 ** Kridle's (so its said...) from a long time ago.
1440 ** It does a binary search of the time_t space. Since time_t's are
1441 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1442 ** would still be very reasonable).
1446 ** Simplified normalize logic courtesy Paul Eggert.
1449 static int increment_overflow(int *number, int delta)
1455 return (*number < number0) != (delta < 0);
1458 static int long_increment_overflow(long *number, int delta)
1464 return (*number < number0) != (delta < 0);
1467 static int normalize_overflow(int *tensptr, int *unitsptr, const int base)
1471 tensdelta = (*unitsptr >= 0) ?
1472 (*unitsptr / base) :
1473 (-1 - (-1 - *unitsptr) / base);
1474 *unitsptr -= tensdelta * base;
1475 return increment_overflow(tensptr, tensdelta);
1478 static int long_normalize_overflow(long *tensptr, int *unitsptr, const int base)
1482 tensdelta = (*unitsptr >= 0) ?
1483 (*unitsptr / base) :
1484 (-1 - (-1 - *unitsptr) / base);
1485 *unitsptr -= tensdelta * base;
1486 return long_increment_overflow(tensptr, tensdelta);
1489 static int tmcomp(const struct ast_tm *atmp, const struct ast_tm *btmp)
1493 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1494 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1495 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1496 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1497 (result = (atmp->tm_min - btmp->tm_min)) == 0 &&
1498 (result = (atmp->tm_sec - btmp->tm_sec)) == 0)
1499 result = atmp->tm_usec - btmp->tm_usec;
1503 static struct timeval time2sub(struct ast_tm *tmp, struct ast_tm * (* const funcp) (const struct timeval *, long, struct ast_tm *, const struct state *), const long offset, int *okayp, const int do_norm_secs, const struct state *sp)
1512 struct timeval newt = { 0, 0 };
1513 struct timeval t = { 0, 0 };
1514 struct ast_tm yourtm, mytm;
1519 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1523 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1525 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1528 if (long_normalize_overflow(&y, &yourtm.tm_mon, MONSPERYEAR))
1531 ** Turn y into an actual year number for now.
1532 ** It is converted back to an offset from TM_YEAR_BASE later.
1534 if (long_increment_overflow(&y, TM_YEAR_BASE))
1536 while (yourtm.tm_mday <= 0) {
1537 if (long_increment_overflow(&y, -1))
1539 li = y + (1 < yourtm.tm_mon);
1540 yourtm.tm_mday += year_lengths[isleap(li)];
1542 while (yourtm.tm_mday > DAYSPERLYEAR) {
1543 li = y + (1 < yourtm.tm_mon);
1544 yourtm.tm_mday -= year_lengths[isleap(li)];
1545 if (long_increment_overflow(&y, 1))
1549 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1550 if (yourtm.tm_mday <= i)
1552 yourtm.tm_mday -= i;
1553 if (++yourtm.tm_mon >= MONSPERYEAR) {
1555 if (long_increment_overflow(&y, 1))
1559 if (long_increment_overflow(&y, -TM_YEAR_BASE))
1562 if (yourtm.tm_year != y)
1564 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1566 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1568 ** We can't set tm_sec to 0, because that might push the
1569 ** time below the minimum representable time.
1570 ** Set tm_sec to 59 instead.
1571 ** This assumes that the minimum representable time is
1572 ** not in the same minute that a leap second was deleted from,
1573 ** which is a safer assumption than using 58 would be.
1575 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1577 saved_seconds = yourtm.tm_sec;
1578 yourtm.tm_sec = SECSPERMIN - 1;
1580 saved_seconds = yourtm.tm_sec;
1584 ** Do a binary search (this works whatever time_t's type is).
1586 if (!TYPE_SIGNED(time_t)) {
1589 } else if (!TYPE_INTEGRAL(time_t)) {
1590 if (sizeof(time_t) > sizeof(float))
1591 hi = (time_t) DBL_MAX;
1592 else hi = (time_t) FLT_MAX;
1596 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
1601 t.tv_sec = lo / 2 + hi / 2;
1604 else if (t.tv_sec > hi)
1606 if ((*funcp)(&t, offset, &mytm, sp) == NULL) {
1608 ** Assume that t is too extreme to be represented in
1609 ** a struct ast_tm; arrange things so that it is less
1610 ** extreme on the next pass.
1612 dir = (t.tv_sec > 0) ? 1 : -1;
1613 } else dir = tmcomp(&mytm, &yourtm);
1615 if (t.tv_sec == lo) {
1620 } else if (t.tv_sec == hi) {
1633 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1636 ** Right time, wrong type.
1637 ** Hunt for right time, right type.
1638 ** It's okay to guess wrong since the guess
1642 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1644 for (i = sp->typecnt - 1; i >= 0; --i) {
1645 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1647 for (j = sp->typecnt - 1; j >= 0; --j) {
1648 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1650 newt.tv_sec = t.tv_sec + sp->ttis[j].tt_gmtoff -
1651 sp->ttis[i].tt_gmtoff;
1652 if ((*funcp)(&newt, offset, &mytm, sp) == NULL)
1654 if (tmcomp(&mytm, &yourtm) != 0)
1656 if (mytm.tm_isdst != yourtm.tm_isdst)
1668 newt.tv_sec = t.tv_sec + saved_seconds;
1669 if ((newt.tv_sec < t.tv_sec) != (saved_seconds < 0))
1671 t.tv_sec = newt.tv_sec;
1672 if ((*funcp)(&t, offset, tmp, sp))
1677 static struct timeval time2(struct ast_tm *tmp, struct ast_tm * (* const funcp) (const struct timeval *, long, struct ast_tm*, const struct state *sp), const long offset, int *okayp, const struct state *sp)
1682 ** First try without normalization of seconds
1683 ** (in case tm_sec contains a value associated with a leap second).
1684 ** If that fails, try with normalization of seconds.
1686 t = time2sub(tmp, funcp, offset, okayp, FALSE, sp);
1687 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE, sp);
1690 static struct timeval time1(struct ast_tm *tmp, struct ast_tm * (* const funcp) (const struct timeval *, long, struct ast_tm *, const struct state *), const long offset, const struct state *sp)
1694 int sameind, otherind;
1697 int seen[TZ_MAX_TYPES];
1698 int types[TZ_MAX_TYPES];
1701 if (tmp->tm_isdst > 1)
1703 t = time2(tmp, funcp, offset, &okay, sp);
1706 ** PCTS code courtesy Grant Sullivan.
1710 if (tmp->tm_isdst < 0)
1711 tmp->tm_isdst = 0; /* reset to std and try again */
1712 #endif /* defined PCTS */
1714 if (okay || tmp->tm_isdst < 0)
1716 #endif /* !defined PCTS */
1718 ** We're supposed to assume that somebody took a time of one type
1719 ** and did some math on it that yielded a "struct ast_tm" that's bad.
1720 ** We try to divine the type they started from and adjust to the
1725 for (i = 0; i < sp->typecnt; ++i)
1728 for (i = sp->timecnt - 1; i >= 0; --i)
1729 if (!seen[sp->types[i]]) {
1730 seen[sp->types[i]] = TRUE;
1731 types[nseen++] = sp->types[i];
1733 for (sameind = 0; sameind < nseen; ++sameind) {
1734 samei = types[sameind];
1735 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1737 for (otherind = 0; otherind < nseen; ++otherind) {
1738 otheri = types[otherind];
1739 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1741 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1742 sp->ttis[samei].tt_gmtoff;
1743 tmp->tm_isdst = !tmp->tm_isdst;
1744 t = time2(tmp, funcp, offset, &okay, sp);
1747 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1748 sp->ttis[samei].tt_gmtoff;
1749 tmp->tm_isdst = !tmp->tm_isdst;
1755 struct timeval ast_mktime(struct ast_tm *tmp, const char *zone)
1757 const struct state *sp;
1758 if (!(sp = ast_tzset(zone)))
1760 return time1(tmp, localsub, 0L, sp);
1763 int ast_strftime(char *buf, size_t len, const char *tmp, const struct ast_tm *tm)
1765 size_t fmtlen = strlen(tmp) + 1;
1766 char *format = ast_calloc(1, fmtlen), *fptr = format, *newfmt;
1767 int decimals = -1, i, res;
1772 for (; *tmp; tmp++) {
1783 decimals = tmp[1] - '0';
1786 case 'q': /* Milliseconds */
1790 /* Juggle some memory to fit the item */
1791 newfmt = ast_realloc(format, fmtlen + decimals);
1796 fptr = fptr - format + newfmt;
1800 /* Reduce the fraction of time to the accuracy needed */
1801 for (i = 6, fraction = tm->tm_usec; i > decimals; i--)
1803 fptr += sprintf(fptr, "%0*ld", decimals, fraction);
1805 /* Reset, in case more than one 'q' specifier exists */
1813 defcase: *fptr++ = *tmp;
1817 res = (int)strftime(buf, len, format, (struct tm *)tm);
1822 char *ast_strptime(const char *s, const char *format, struct ast_tm *tm)
1824 struct tm tm2 = { 0, };
1825 char *res = strptime(s, format, &tm2);
1826 memcpy(tm, &tm2, sizeof(*tm));
1828 /* strptime(3) doesn't set .tm_isdst correctly, so to force ast_mktime(3)
1829 * to deal with it correctly, we set it to -1. */