.
[gnulib.git] / lib / strftime.c
index cc4953e..484852a 100644 (file)
@@ -46,6 +46,7 @@
    %p  locale's AM or PM
    %r  time, 12-hour (hh:mm:ss [AP]M)
    %R  time, 24-hour (hh:mm)
+   %s  time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension)
    %S  second (00..61)
    %T  time, 24-hour (hh:mm:ss)
    %X  locale's time representation (%H:%M:%S)
 
    David MacKenzie <djm@gnu.ai.mit.edu> */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
 #include <sys/types.h>
 #if defined(TM_IN_SYS_TIME) || (!defined(HAVE_TM_ZONE) && !defined(HAVE_TZNAME))
 #include <sys/time.h>
 #include <time.h>
 #endif
 
-#if defined(HAVE_TZNAME)
-extern char *tzname[2];
+#ifndef STDC_HEADERS
+time_t mktime ();
 #endif
 
-#if !__STDC__
-#define const
+#if defined(HAVE_TZNAME)
+extern char *tzname[2];
 #endif
 
 /* Types of padding for numbers in date and time. */
@@ -94,12 +100,12 @@ enum padding
   none, blank, zero
 };
 
-static char *days[] =
+static char const* const days[] =
 {
   "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
 };
 
-static char *months[] =
+static char const * const months[] =
 {
   "January", "February", "March", "April", "May", "June",
   "July", "August", "September", "October", "November", "December"
@@ -108,7 +114,13 @@ static char *months[] =
 /* Add character C to STRING and increment LENGTH,
    unless LENGTH would exceed MAX. */
 
-#define add_char(c) (length + 1 <= max) && (string[length++] = (c))
+#define add_char(c)                                                    \
+  do                                                                   \
+    {                                                                  \
+      if (length + 1 <= max)                                           \
+       string[length++] = (c);                                         \
+    }                                                                  \
+  while (0)
 
 /* Add a 2 digit number to STRING, padding if specified.
    Return the number of characters added, up to MAX. */
@@ -162,7 +174,7 @@ add_num3 (string, num, max, pad)
 static int
 add_str (to, from, max)
      char *to;
-     char *from;
+     const char *from;
      int max;
 {
   int i;
@@ -172,6 +184,25 @@ add_str (to, from, max)
   return i;
 }
 
+static int
+add_num_time_t (string, max, num)
+     char *string;
+     int max;
+     time_t num;
+{
+  /* This buffer is large enough to hold the character representation
+     (including the trailing NUL) of any unsigned decimal quantity
+     whose binary representation fits in 128 bits.  */
+  char buf[40];
+  int length;
+
+  if (sizeof (num) > 16)
+    abort ();
+  sprintf (buf, "%lu", (unsigned long) num);
+  length = add_str (string, buf, max);
+  return length;
+}
+
 /* Return the week in the year of the time in TM, with the weeks
    starting on Sundays. */
 
@@ -317,6 +348,16 @@ strftime (string, max, format, tm)
              length +=
                strftime (&string[length], max - length, "%H:%M", tm);
              break;
+
+           case 's':
+             {
+               struct tm writable_tm;
+               writable_tm = *tm;
+               length += add_num_time_t (&string[length], max - length,
+                                         mktime (&writable_tm));
+             }
+             break;
+
            case 'S':
              length +=
                add_num2 (&string[length], tm->tm_sec, max - length, pad);