summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdtime/strftime.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/stdtime/strftime.c')
-rw-r--r--lib/libc/stdtime/strftime.c190
1 files changed, 111 insertions, 79 deletions
diff --git a/lib/libc/stdtime/strftime.c b/lib/libc/stdtime/strftime.c
index 5e272be..4bb0ebd 100644
--- a/lib/libc/stdtime/strftime.c
+++ b/lib/libc/stdtime/strftime.c
@@ -17,12 +17,14 @@
#ifdef LIBC_RCS
static const char rcsid[] =
- "$Id: strftime.c,v 1.8 1996/05/27 06:54:01 scrappy Exp $";
+ "$Id: strftime.c,v 1.9 1996/07/12 18:55:32 jkh Exp $";
#endif
#ifndef lint
#ifndef NOID
-static const char elsieid[] = "@(#)strftime.c 7.38";
+/*
+static char elsieid[] = "@(#)strftime.c 7.47";
+*/
/*
** Based on the UCB version with the ID appearing below.
** This is ANSIish only when "multibyte character == plain character".
@@ -44,8 +46,6 @@ static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
#include <rune.h> /* for _PATH_LOCALE */
#include <sys/stat.h>
-#define LOCALE_HOME _PATH_LOCALE
-
struct lc_time_T {
const char * mon[12];
const char * month[12];
@@ -92,12 +92,12 @@ static const struct lc_time_T C_time_locale = {
"%m/%d/%y",
/*
- ** c_fmt (ctime-compatible)
+ ** c_fmt
** Note that
** "%a %b %d %H:%M:%S %Y"
** is used by Solaris 2.3.
*/
- "%a %b %e %X %Y",
+ "%D %X", /* %m/%d/%y %H:%M:%S */
/* am */
"AM",
@@ -106,13 +106,12 @@ static const struct lc_time_T C_time_locale = {
"PM",
/* date_fmt */
- "%a %b %e %X %Z %Y"
+ "%a %b %e %H:%M:%S %Z %Y"
};
static char * _add P((const char *, char *, const char *));
static char * _conv P((int, const char *, char *, const char *));
static char * _fmt P((const char *, const struct tm *, char *, const char *));
-static char * _secs P((const struct tm *, char *, const char *));
size_t strftime P((char *, size_t, const char *, const struct tm *));
@@ -120,12 +119,12 @@ extern char * tzname[];
size_t
strftime(s, maxsize, format, t)
- char *const s;
- const size_t maxsize;
- const char *const format;
- const struct tm *const t;
+char * const s;
+const size_t maxsize;
+const char * const format;
+const struct tm * const t;
{
- char *p;
+ char * p;
tzset();
p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize);
@@ -137,10 +136,10 @@ strftime(s, maxsize, format, t)
static char *
_fmt(format, t, pt, ptlim)
- const char *format;
- const struct tm *const t;
- char *pt;
- const char *const ptlim;
+const char * format;
+const struct tm * const t;
+char * pt;
+const char * const ptlim;
{
for ( ; *format; ++format) {
if (*format == '%') {
@@ -278,7 +277,21 @@ label:
pt = _conv(t->tm_sec, "%02d", pt, ptlim);
continue;
case 's':
- pt = _secs(t, pt, ptlim);
+ {
+ struct tm tm;
+ char buf[INT_STRLEN_MAXIMUM(
+ time_t) + 1];
+ time_t mkt;
+
+ tm = *t;
+ mkt = mktime(&tm);
+ if (TYPE_SIGNED(time_t))
+ (void) sprintf(buf, "%ld",
+ (long) mkt);
+ else (void) sprintf(buf, "%lu",
+ (unsigned long) mkt);
+ pt = _add(buf, pt, ptlim);
+ }
continue;
case 'T':
pt = _fmt("%H:%M:%S", t, pt, ptlim);
@@ -300,67 +313,86 @@ label:
pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday,
"%d", pt, ptlim);
continue;
- case 'V':
- /*
- ** From Arnold Robbins' strftime version 3.0:
- ** "the week number of the year (the first
- ** Monday as the first day of week 1) as a
- ** decimal number (01-53). The method for
- ** determining the week number is as specified
- ** by ISO 8601 (to wit: if the week containing
- ** January 1 has four or more days in the new
- ** year, then it is week 1, otherwise it is
- ** week 53 of the previous year and the next
- ** week is week 1)."
- ** (ado, 5/24/93)
- */
- /*
- ** XXX--If January 1 falls on a Friday,
- ** January 1-3 are part of week 53 of the
- ** previous year. By analogy, if January
- ** 1 falls on a Thursday, are December 29-31
- ** of the PREVIOUS year part of week 1???
- ** (ado 5/24/93)
- */
- /*
- ** You are understood not to expect this.
- */
+ case 'V': /* ISO 8601 week number */
+ case 'G': /* ISO 8601 year (four digits) */
+ case 'g': /* ISO 8601 year (two digits) */
+/*
+** From Arnold Robbins' strftime version 3.0: "the week number of the
+** year (the first Monday as the first day of week 1) as a decimal number
+** (01-53)."
+** (ado, 1993-05-24)
+**
+** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
+** "Week 01 of a year is per definition the first week which has the
+** Thursday in this year, which is equivalent to the week which contains
+** the fourth day of January. In other words, the first week of a new year
+** is the week which has the majority of its days in the new year. Week 01
+** might also contain days from the previous year and the week before week
+** 01 of a year is the last week (52 or 53) of the previous year even if
+** it contains days from the new year. A week starts with Monday (day 1)
+** and ends with Sunday (day 7). For example, the first week of the year
+** 1997 lasts from 1996-12-30 to 1997-01-05..."
+** (ado, 1996-01-02)
+*/
{
- int i;
-
- i = (t->tm_yday + 10 - (t->tm_wday ?
- (t->tm_wday - 1) : 6)) / 7;
- if (i == 0) {
+ int year;
+ int yday;
+ int wday;
+ int w;
+
+ year = t->tm_year + TM_YEAR_BASE;
+ yday = t->tm_yday;
+ wday = t->tm_wday;
+ for ( ; ; ) {
+ int len;
+ int bot;
+ int top;
+
+ len = isleap(year) ?
+ DAYSPERLYEAR :
+ DAYSPERNYEAR;
/*
- ** What day of the week does
- ** January 1 fall on?
+ ** What yday (-3 ... 3) does
+ ** the ISO year begin on?
*/
- i = t->tm_wday -
- (t->tm_yday - 1);
+ bot = ((yday + 11 - wday) %
+ DAYSPERWEEK) - 3;
/*
- ** Fri Jan 1: 53
- ** Sun Jan 1: 52
- ** Sat Jan 1: 53 if previous
- ** year a leap
- ** year, else 52
+ ** What yday does the NEXT
+ ** ISO year begin on?
*/
- if (i == TM_FRIDAY)
- i = 53;
- else if (i == TM_SUNDAY)
- i = 52;
- else i = isleap(t->tm_year +
- TM_YEAR_BASE) ?
- 53 : 52;
+ top = bot -
+ (len % DAYSPERWEEK);
+ if (top < -3)
+ top += DAYSPERWEEK;
+ top += len;
+ if (yday >= top) {
+ ++year;
+ w = 1;
+ break;
+ }
+ if (yday >= bot) {
+ w = 1 + ((yday - bot) /
+ DAYSPERWEEK);
+ break;
+ }
+ --year;
+ yday += isleap(year) ?
+ DAYSPERLYEAR :
+ DAYSPERNYEAR;
+ }
#ifdef XPG4_1994_04_09
- /*
- ** As of 4/9/94, though,
- ** XPG4 calls for 53
- ** unconditionally.
- */
- i = 53;
+ if (w == 52 && t->tm_mon == TM_JANUARY)
+ w = 53;
#endif /* defined XPG4_1994_04_09 */
- }
- pt = _conv(i, "%02d", pt, ptlim);
+ if (*format == 'V')
+ pt = _conv(w, "%02d",
+ pt, ptlim);
+ else if (*format == 'G')
+ pt = _conv(year, "%02d",
+ pt, ptlim);
+ else pt = _conv(year, "%04d",
+ pt, ptlim);
}
continue;
case 'v':
@@ -425,10 +457,10 @@ label:
static char *
_conv(n, format, pt, ptlim)
- const int n;
- const char *const format;
- char *const pt;
- const char *const ptlim;
+const int n;
+const char * const format;
+char * const pt;
+const char * const ptlim;
{
char buf[INT_STRLEN_MAXIMUM(int) + 1];
@@ -455,9 +487,9 @@ _secs(t, pt, ptlim)
static char *
_add(str, pt, ptlim)
- const char *str;
- char *pt;
- const char *const ptlim;
+const char * str;
+char * pt;
+const char * const ptlim;
{
while (pt < ptlim && (*pt = *str++) != '\0')
++pt;
OpenPOWER on IntegriCloud