From 8566a727674ab3c2b0df03c31c6085a0d5d5bf81 Mon Sep 17 00:00:00 2001 From: hniksic Date: Tue, 8 Aug 2006 07:32:53 -0700 Subject: [PATCH] [svn] Avoid code repetition between time_str and datetime_str. --- src/ChangeLog | 4 ++++ src/cookies.c | 4 ++-- src/ftp.c | 7 ++++--- src/http.c | 4 ++-- src/main.c | 3 ++- src/utils.c | 51 +++++++++++++++++++-------------------------------- src/utils.h | 4 ++-- 7 files changed, 35 insertions(+), 42 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index cdc43f68..c43619b3 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2006-08-08 Hrvoje Niksic + + * utils.c (datetime_str): Avoid code repetition with time_str. + 2006-07-21 Hrvoje Niksic * init.c (commands): Correctly place "contentdisposition". diff --git a/src/cookies.c b/src/cookies.c index 4906a6be..e173c5bc 100644 --- a/src/cookies.c +++ b/src/cookies.c @@ -265,7 +265,7 @@ store_cookie (struct cookie_jar *jar, struct cookie *cookie) 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)); } } @@ -1255,7 +1255,7 @@ cookie_jar_save (struct cookie_jar *jar, const char *file) } 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); diff --git a/src/ftp.c b/src/ftp.c index ca07dfbb..9728c339 100644 --- a/src/ftp.c +++ b/src/ftp.c @@ -37,6 +37,7 @@ so, delete this exception statement from your version. */ #endif #include #include +#include #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); - tms = time_str (NULL); + tms = time_str (time (NULL)); 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. */ - tms = time_str (NULL); + tms = time_str (time (NULL)); /* 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 (); } - tms = time_str (NULL); + tms = time_str (time (NULL)); if (!opt.spider) tmrate = retr_rate (len - restval, con->dltime); diff --git a/src/http.c b/src/http.c index f5f7744d..77f97972 100644 --- a/src/http.c +++ b/src/http.c @@ -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. */ - tms = time_str (NULL); + tms = time_str (time (NULL)); /* 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? */ - tms = time_str (NULL); + tms = time_str (time (NULL)); /* Get the new location (with or without the redirection). */ if (hstat.newloc) diff --git a/src/main.c b/src/main.c index 75b35fea..bf9eef77 100644 --- a/src/main.c +++ b/src/main.c @@ -41,6 +41,7 @@ so, delete this exception statement from your version. */ #endif #include #include +#include #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"), - time_str (NULL), + time_str (time (NULL)), opt.numurls, human_readable (total_downloaded_bytes), secs_to_human_time (total_download_time), diff --git a/src/utils.c b/src/utils.c index 6419009e..7a90c069 100644 --- a/src/utils.c +++ b/src/utils.c @@ -253,51 +253,38 @@ concat_strings (const char *str0, ...) return ret; } +/* 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 * -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 * -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"); } /* The Windows versions of the following two functions are defined in diff --git a/src/utils.h b/src/utils.h index 8a1e1f9b..596a8b87 100644 --- a/src/utils.h +++ b/src/utils.h @@ -40,8 +40,8 @@ struct file_memory { #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 (); -- 2.39.2