2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org>
6b5e5868e7ebf1aff3a5abd7d0c4ef0e5fbf3648Garrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright (c) 1989 The Regents of the University of California.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Redistribution and use in source and binary forms are permitted
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * provided that the above copyright notice and this paragraph are
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * duplicated in all such forms and that any documentation,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * advertising materials, and other materials related to such
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * distribution and use acknowledge that the software was developed
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * by the University of California, Berkeley. The name of the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * University may not be used to endorse or promote products derived
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * from this software without specific prior written permission.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorestatic char *_add(const char *, char *, const char *);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorestatic char *_conv(int, const char *, char *, const char *);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestatic char *_fmt(locale_t, const char *, const struct tm *, char *,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore const char * const);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorestatic char *_yconv(int, int, int, int, char *, const char *);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amoreextern char *tzname[];
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* DEFAULT, LESS, SPACE, ZERO */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestrftime_l(char *_RESTRICT_KYWD s, size_t maxsize,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore const char *_RESTRICT_KYWD format, const struct tm *_RESTRICT_KYWD t,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore p = _fmt(loc, ((format == NULL) ? "%c" : format), t, s, s + maxsize);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (p - s);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestrftime(char *_RESTRICT_KYWD s, size_t maxsize,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore const char *_RESTRICT_KYWD format, const struct tm *_RESTRICT_KYWD t)
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (strftime_l(s, maxsize, format, t, uselocale(NULL)));
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore_fmt(locale_t loc, const char *format, const struct tm *t, char *pt,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore const char * const ptlim)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * %C used to do a...
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * _fmt("%a %b %e %X %Y", t);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ...whereas now POSIX 1003.2 calls for
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * something completely different.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * (ado, 1993-05-24)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * C99 locale modifiers.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * The sequences
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * %Ec %EC %Ex %EX %Ey %EY
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * %Od %oe %OH %OI %Om %OM
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * %OS %Ou %OU %OV %Ow %OW %Oy
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * are supposed to provide alternate
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * representations.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore pt = _conv(t->tm_hour, PADDING(PAD_FMT_HMS),
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * This used to be...
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * _conv(t->tm_hour % 12 ?
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * t->tm_hour % 12 : 12, 2, ' ');
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ...and has been changed to the below to
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * match SunOS 4.1.1 and Arnold Robbins'
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * strftime version 3.0. That is, "%k" and
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * "%l" have been swapped.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * (ado, 1993-05-24)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * This used to be...
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * _conv(t->tm_hour, 2, ' ');
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ...and has been changed to the below to
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * match SunOS 4.1.1 and Arnold Robbin's
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * strftime version 3.0. That is, "%k" and
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * "%l" have been swapped.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * (ado, 1993-05-24)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore pt = _fmt(loc, tptr->ampm_fmt, t, pt, ptlim);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * From Arnold Robbins' strftime version 3.0:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * "ISO 8601: Weekday as a decimal number
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * [1 (Monday) - 7]"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * (ado, 1993-05-24)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * From Arnold Robbins' strftime version 3.0: "the week number of the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * year (the first Monday as the first day of week 1) as a decimal number
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * (ado, 1993-05-24)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * "Week 01 of a year is per definition the first week which has the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Thursday in this year, which is equivalent to the week which contains
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * the fourth day of January. In other words, the first week of a new year
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * is the week which has the majority of its days in the new year. Week 01
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * might also contain days from the previous year and the week before week
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 01 of a year is the last week (52 or 53) of the previous year even if
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * it contains days from the new year. A week starts with Monday (day 1)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * and ends with Sunday (day 7). For example, the first week of the year
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 1997 lasts from 1996-12-30 to 1997-01-05..."
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * (ado, 1996-01-02)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * What yday (-3 ... 3) does
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * the ISO year begin on?
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * What yday does the NEXT
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ISO year begin on?
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#endif /* defined XPG4_1994_04_09 */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * From Arnold Robbins' strftime version 3.0:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * "date as dd-bbb-YYYY"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * (ado, 1993-05-24)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * C99 says that %Z must be replaced by the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * empty string if the time zone is not
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * determinable.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * C99 says that the UTC offset must
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * be computed by looking only at
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * tm_isdst. This requirement is
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * incorrect, since it means the code
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * must rely on magic (in this case
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * altzone and timezone), and the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * magic might not have the correct
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * offset. Doing things correctly is
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * tricky and requires disobeying C99;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * see GNU C strftime for details.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * For now, punt and conform to the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * standard, even though it's incorrect.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * C99 says that %z must be replaced by the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * empty string if the time zone is not
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * determinable, so output nothing if the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * appropriate variables are not available.
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore pt = _fmt(loc, tptr->date_fmt, t, pt, ptlim);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * X311J/88-090 (4.12.3.5): if conversion char is
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * undefined, behavior is undefined. Print out the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * character itself as printf(3) also does.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore_conv(const int n, const char *format, char *const pt,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore const char *const ptlim)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore_add(const char *str, char *pt, const char *const ptlim)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore while (pt < ptlim && (*pt = *str++) != '\0')
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * POSIX and the C Standard are unclear or inconsistent about
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * what %C and %y do if the year is negative or exceeds 9999.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Use the convention that %C concatenated with %y yields the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * same output as %Y, and that %Y contains at least 4 bytes,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * with more only if necessary.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore_yconv(const int a, const int b, const int convert_top, const int convert_yy,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);