]> sjero.net Git - wget/commitdiff
[svn] Avoid code repetition between time_str and datetime_str.
authorhniksic <devnull@localhost>
Tue, 8 Aug 2006 14:32:53 +0000 (07:32 -0700)
committerhniksic <devnull@localhost>
Tue, 8 Aug 2006 14:32:53 +0000 (07:32 -0700)
src/ChangeLog
src/cookies.c
src/ftp.c
src/http.c
src/main.c
src/utils.c
src/utils.h

index cdc43f6810303809af794cf310119200c9ad0326..c43619b3f33a862cb566f27e344b03ed32bb3f0e 100644 (file)
@@ -1,3 +1,7 @@
+2006-08-08  Hrvoje Niksic  <hniksic@xemacs.org>
+
+       * utils.c (datetime_str): Avoid code repetition with time_str.
+
 2006-07-21  Hrvoje Niksic  <hniksic@xemacs.org>
 
        * init.c (commands): Correctly place "contentdisposition".
 2006-07-21  Hrvoje Niksic  <hniksic@xemacs.org>
 
        * init.c (commands): Correctly place "contentdisposition".
index 4906a6bee1ef74ff5a308f99924f076e151cf646..e173c5bc381d6fb41193aa8b7b9d9c1059a5ef00 100644 (file)
@@ -265,7 +265,7 @@ store_cookie (struct cookie_jar *jar, struct cookie *cookie)
               cookie->path,
               cookie->permanent ? "permanent" : "session",
               cookie->secure ? "secure" : "insecure",
               cookie->path,
               cookie->permanent ? "permanent" : "session",
               cookie->secure ? "secure" : "insecure",
-              cookie->expiry_time ? datetime_str (&exptime) : "none",
+              cookie->expiry_time ? datetime_str (exptime) : "none",
               cookie->attr, cookie->value));
     }
 }
               cookie->attr, cookie->value));
     }
 }
@@ -1255,7 +1255,7 @@ cookie_jar_save (struct cookie_jar *jar, const char *file)
     }
 
   fputs ("# HTTP cookie file.\n", fp);
     }
 
   fputs ("# HTTP cookie file.\n", fp);
-  fprintf (fp, "# Generated by Wget on %s.\n", datetime_str (&cookies_now));
+  fprintf (fp, "# Generated by Wget on %s.\n", datetime_str (cookies_now));
   fputs ("# Edit at your own risk.\n\n", fp);
 
   for (hash_table_iterate (jar->chains, &iter);
   fputs ("# Edit at your own risk.\n\n", fp);
 
   for (hash_table_iterate (jar->chains, &iter);
index ca07dfbbb775797e5c2afc68dba5a1ac323dc591..9728c3394cfdc0257ae30884d73d80f498a52b0f 100644 (file)
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -37,6 +37,7 @@ so, delete this exception statement from your version.  */
 #endif
 #include <assert.h>
 #include <errno.h>
 #endif
 #include <assert.h>
 #include <errno.h>
+#include <time.h>
 
 #include "wget.h"
 #include "utils.h"
 
 #include "wget.h"
 #include "utils.h"
@@ -967,7 +968,7 @@ Error in server response, closing control connection.\n"));
                      expected_bytes ? expected_bytes - restval : 0,
                      restval, &rd_size, len, &con->dltime, flags);
 
                      expected_bytes ? expected_bytes - restval : 0,
                      restval, &rd_size, len, &con->dltime, flags);
 
-  tms = time_str (NULL);
+  tms = time_str (time (NULL));
   tmrate = retr_rate (rd_size, con->dltime);
   total_download_time += con->dltime;
 
   tmrate = retr_rate (rd_size, con->dltime);
   total_download_time += con->dltime;
 
@@ -1149,7 +1150,7 @@ ftp_loop_internal (struct url *u, struct fileinfo *f, ccon *con)
        restval = 0;
 
       /* Get the current time string.  */
        restval = 0;
 
       /* Get the current time string.  */
-      tms = time_str (NULL);
+      tms = time_str (time (NULL));
       /* Print fetch message, if opt.verbose.  */
       if (opt.verbose)
        {
       /* Print fetch message, if opt.verbose.  */
       if (opt.verbose)
        {
@@ -1213,7 +1214,7 @@ ftp_loop_internal (struct url *u, struct fileinfo *f, ccon *con)
          /* Not as great.  */
          abort ();
        }
          /* Not as great.  */
          abort ();
        }
-      tms = time_str (NULL);
+      tms = time_str (time (NULL));
       if (!opt.spider)
         tmrate = retr_rate (len - restval, con->dltime);
 
       if (!opt.spider)
         tmrate = retr_rate (len - restval, con->dltime);
 
index f5f7744d1690fd647d04ef2a007bed43385e4a26..77f9797232cdb648dde8b641c4eb2d023ca0e7bb 100644 (file)
@@ -2280,7 +2280,7 @@ http_loop (struct url *u, char **newloc, char **local_file, const char *referer,
       sleep_between_retrievals (count);
       
       /* Get the current time string.  */
       sleep_between_retrievals (count);
       
       /* Get the current time string.  */
-      tms = time_str (NULL);
+      tms = time_str (time (NULL));
       
       /* Print fetch message, if opt.verbose.  */
       if (opt.verbose)
       
       /* Print fetch message, if opt.verbose.  */
       if (opt.verbose)
@@ -2344,7 +2344,7 @@ http_loop (struct url *u, char **newloc, char **local_file, const char *referer,
       err = gethttp (u, &hstat, dt, proxy);
 
       /* Time?  */
       err = gethttp (u, &hstat, dt, proxy);
 
       /* Time?  */
-      tms = time_str (NULL);
+      tms = time_str (time (NULL));
       
       /* Get the new location (with or without the redirection).  */
       if (hstat.newloc)
       
       /* Get the new location (with or without the redirection).  */
       if (hstat.newloc)
index 75b35fea2fd1d224a835dd713abda07f1f565864..bf9eef772fb44958b90a8ead6428399eee41c386 100644 (file)
@@ -41,6 +41,7 @@ so, delete this exception statement from your version.  */
 #endif
 #include <assert.h>
 #include <errno.h>
 #endif
 #include <assert.h>
 #include <errno.h>
+#include <time.h>
 
 #include "wget.h"
 #include "utils.h"
 
 #include "wget.h"
 #include "utils.h"
@@ -1017,7 +1018,7 @@ Can't timestamp and not clobber old files at the same time.\n"));
     {
       logprintf (LOG_NOTQUIET,
                  _("FINISHED --%s--\nDownloaded: %d files, %s in %s (%s)\n"),
     {
       logprintf (LOG_NOTQUIET,
                  _("FINISHED --%s--\nDownloaded: %d files, %s in %s (%s)\n"),
-                 time_str (NULL),
+                 time_str (time (NULL)),
                  opt.numurls,
                  human_readable (total_downloaded_bytes),
                  secs_to_human_time (total_download_time),
                  opt.numurls,
                  human_readable (total_downloaded_bytes),
                  secs_to_human_time (total_download_time),
index 6419009eb92b58b6df1372229cebc873d89b8ff4..7a90c0692dfe6a569e1e53bafd6e92de23d91d36 100644 (file)
@@ -253,51 +253,38 @@ concat_strings (const char *str0, ...)
   return ret;
 }
 \f
   return ret;
 }
 \f
+/* Format the provided time according to the specified format.  The
+   format is a string with format elements supported by strftime.  */
+
+static char *
+fmttime (time_t t, const char *fmt)
+{
+  static char output[32];
+  struct tm *tm = localtime(&t);
+  if (!tm)
+    abort ();
+  if (!strftime(output, sizeof(output), fmt, tm))
+    abort ();
+  return output;
+}
+
 /* Return pointer to a static char[] buffer in which zero-terminated
    string-representation of TM (in form hh:mm:ss) is printed.
 
    If TM is NULL, the current time will be used.  */
 
 char *
 /* Return pointer to a static char[] buffer in which zero-terminated
    string-representation of TM (in form hh:mm:ss) is printed.
 
    If TM is NULL, the current time will be used.  */
 
 char *
-time_str (time_t *tm)
+time_str (time_t t)
 {
 {
-  static char output[15];
-  struct tm *ptm;
-  time_t secs = tm ? *tm : time (NULL);
-
-  if (secs == -1)
-    {
-      /* In case of error, return the empty string.  Maybe we should
-        just abort if this happens?  */
-      *output = '\0';
-      return output;
-    }
-  ptm = localtime (&secs);
-  sprintf (output, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
-  return output;
+  return fmttime(t, "%H:%M:%S");
 }
 
 /* Like the above, but include the date: YYYY-MM-DD hh:mm:ss.  */
 
 char *
 }
 
 /* Like the above, but include the date: YYYY-MM-DD hh:mm:ss.  */
 
 char *
-datetime_str (time_t *tm)
+datetime_str (time_t t)
 {
 {
-  static char output[20];      /* "YYYY-MM-DD hh:mm:ss" + \0 */
-  struct tm *ptm;
-  time_t secs = tm ? *tm : time (NULL);
-
-  if (secs == -1)
-    {
-      /* In case of error, return the empty string.  Maybe we should
-        just abort if this happens?  */
-      *output = '\0';
-      return output;
-    }
-  ptm = localtime (&secs);
-  sprintf (output, "%04d-%02d-%02d %02d:%02d:%02d",
-          ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,
-          ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
-  return output;
+  return fmttime(t, "%Y-%m-%d %H:%M:%S");
 }
 \f
 /* The Windows versions of the following two functions are defined in
 }
 \f
 /* The Windows versions of the following two functions are defined in
index 8a1e1f9b912938fe1295a7199797c4cfb7e23966..596a8b870ef9ff98a609032833163020d11e1e4e 100644 (file)
@@ -40,8 +40,8 @@ struct file_memory {
 
 #define HYPHENP(x) (*(x) == '-' && !*((x) + 1))
 
 
 #define HYPHENP(x) (*(x) == '-' && !*((x) + 1))
 
-char *time_str (time_t *);
-char *datetime_str (time_t *);
+char *time_str (time_t);
+char *datetime_str (time_t);
 
 #ifdef DEBUG_MALLOC
 void print_malloc_debug_stats ();
 
 #ifdef DEBUG_MALLOC
 void print_malloc_debug_stats ();