You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@subversion.apache.org by Nuutti Kotivuori <na...@iki.fi> on 2002/07/15 20:12:26 UTC

[PATCH] Human representation date format change

Ben Collins-Sussman's comments on getting this in before alpha were:

22:48 sussman:#svn => yes please!!!
22:48 sussman:#svn => do it!

So, here it is. While making this, some bugs were found in APR - but
luckily those will not affect here, yet.

Log message:

New human representation timestamp format - changed from

  Mon, 15 Jul 2002 21:06:43 +0300

to

  2002-07-15 21:06:43 +0300 (Mon, 15 Jul 2002)

The latter part format can be made as a configuration option in the
future.

* subversion/libsvn_subr/time.c
(SVN_SUBR__DATE_MAX_LENGTH): New constant.
(human_timestamp_format): Changed format.
(human_timestamp_format_suffix): New variable.
(svn_time_to_human_nts): Changed to use apr_snprintf and apr_strftime
  both to generate the wanted format.

Patch:

Index: ./subversion/libsvn_subr/time.c
===================================================================
--- ./subversion/libsvn_subr/time.c
+++ ./subversion/libsvn_subr/time.c	Mon Jul 15 22:59:19 2002
@@ -55,15 +55,27 @@
 static const char * const old_timestamp_format =
 "%s %d %s %d %02d:%02d:%02d.%06d (day %03d, dst %d, gmt_off %06d)";
 
-/* Our human representation of dates look like this:
+/* Our human representation of dates looks like this:
  *
- *    "Sun, 23 Jun 2002 11:13:02 +0300"
+ *    "2002-06-23 11:13:02 +0300 (Sun, 23 Jun 2002)"
  *
- * This format is used whenever time is shown to the user
- * directly.
+ * This format is used whenever time is shown to the user. It consists
+ * of a machine parseable, almost ISO-8601, part in the beginning -
+ * and a human explanative part at the end. The machine parseable part
+ * is generated strictly by APR and our code, with a apr_snprintf. The
+ * human explanative part is generated by apr_strftime, which means
+ * that it's generation can be affected by locale, it can fail and it
+ * doesn't need to be constant in size. In other words, perfect to be
+ * converted to a configuration option later on.
  */
+/* Maximum length for the date string. */
+#define SVN_SUBR__DATE_MAX_LENGTH 80
+/* Machine parseable part, generated by apr_snprintf. */
 static const char * const human_timestamp_format =
-"%3.3s, %.2d %3.3s %.2d %.2d:%.2d:%.2d %+.2d%.2d";
+"%.4d-%.2d-%.2d %.2d:%.2d:%.2d %+.2d%.2d";
+/* Human explanative part, generated by apr_strftime. */
+static const char * const human_timestamp_format_suffix =
+" (%a, %d %b %Y)";
 
 
 const char *
@@ -209,20 +221,48 @@
 svn_time_to_human_nts (apr_time_t t, apr_pool_t *pool)
 {
   apr_time_exp_t exploded_time;
+  apr_size_t len, retlen;
+  apr_status_t ret;
+  char *datestr, *curptr;
 
+  /* Get the time into parts */
   apr_time_exp_lt (&exploded_time, t);
 
-  return apr_psprintf (pool,
-                       human_timestamp_format,
-                       apr_day_snames[exploded_time.tm_wday],
-                       exploded_time.tm_mday,
-                       apr_month_snames[exploded_time.tm_mon],
-                       exploded_time.tm_year + 1900,
-                       exploded_time.tm_hour,
-                       exploded_time.tm_min,
-                       exploded_time.tm_sec,
-                       exploded_time.tm_gmtoff / (60 * 60),
-                       (exploded_time.tm_gmtoff / 60) % 60);
+  /* Make room for datestring */
+  datestr = apr_palloc(pool, SVN_SUBR__DATE_MAX_LENGTH);
+
+  /* Put in machine parseable part */
+  len = apr_snprintf (datestr,
+                      SVN_SUBR__DATE_MAX_LENGTH,
+                      human_timestamp_format,
+                      exploded_time.tm_year + 1900,
+                      exploded_time.tm_mon + 1,
+                      exploded_time.tm_mday,
+                      exploded_time.tm_hour,
+                      exploded_time.tm_min,
+                      exploded_time.tm_sec,
+                      exploded_time.tm_gmtoff / (60 * 60),
+                      (exploded_time.tm_gmtoff / 60) % 60);
+
+  /* If we overfilled the buffer, just return what we got. */
+  if(len >= SVN_SUBR__DATE_MAX_LENGTH)
+    return datestr;
+
+  /* Calculate offset to the end of the machine parseable part. */
+  curptr = datestr + len;
+
+  /* Put in human readable part */
+  ret = apr_strftime (curptr,
+                      &retlen,
+                      SVN_SUBR__DATE_MAX_LENGTH - len,
+                      human_timestamp_format_suffix,
+                      &exploded_time);
+  
+  /* If there was an error, ensure that the string is zero-terminated. */
+  if (!ret || retlen == 0)
+    curptr = '\0';
+
+  return datestr;
 }
 
 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Human representation date format change

Posted by Nuutti Kotivuori <na...@iki.fi>.
Karl Fogel wrote:
> Nuutti Kotivuori <na...@iki.fi> writes:
>> + * This format is used whenever time is shown to the user. It
>> + consists * of a machine parseable, almost ISO-8601, part in the
>> + beginning - * and a human explanative part at the end. The
>> + machine parseable part
> 
> I never heard of the word "explanative" before, but hey, English is
> a Big Tent language.

Well, you're the english speaker here, not me :)

Apparently Merriam-Webster defines it as 'explanatory'. Think that's a
better expression anyway.

>> + apr_snprintf. The * human explanative part is generated by
>> + apr_strftime, which means * that it's generation can be affected
>> + by locale, it can fail and it
> 
> s/it's/its/

Ouch, thanks.

>> +/* Maximum length for the date string. */
>> +#define SVN_SUBR__DATE_MAX_LENGTH 80
> 
> SVN_TIME__MAX_LENGTH, maybe?  Should use "SVN_TIME__" prefix,
> anyway.

Yup, sounds good.

>> +  /* Put in machine parseable part */
>> +  len = apr_snprintf (datestr,
>> +                      SVN_SUBR__DATE_MAX_LENGTH,
>> +                      human_timestamp_format,
>> +                      exploded_time.tm_year + 1900,
>> +                      exploded_time.tm_mon + 1,
>> +                      exploded_time.tm_mday,
>> +                      exploded_time.tm_hour,
>> +                      exploded_time.tm_min,
>> +                      exploded_time.tm_sec,
>> +                      exploded_time.tm_gmtoff / (60 * 60),
>> +                      (exploded_time.tm_gmtoff / 60) % 60);
>> +
>> +  /* If we overfilled the buffer, just return what we got. */
>> +  if(len >= SVN_SUBR__DATE_MAX_LENGTH)
>> +    return datestr;
> 
> I got confused for a moment, but then learned that apr_snprintf()
> returns the buffer's len (i.e., its second arg) in overfill case.

Well yes - the header file claimed that it did not. Then came the
bugfixes to APR. And this is the "new" behaviour which is supposed to
be the Right Thing.

> So ">=" is okay, but equivalent to "==", right?

Yes, quite right. But from the snprintf manpage from glibc:

,----[ man snprintf ]
| Since glibc 2.1 these functions follow the C99 standard and return the
| number of characters (excluding the trailing '\0') which would have
| been written to the final string if enough space had been available.
`----

So if APR ever would decide to behave that way, it would still work.

>> +  /* Calculate offset to the end of the machine parseable part. */
>> +  curptr = datestr + len;
>> +
>> +  /* Put in human readable part */
>> +  ret = apr_strftime (curptr,
>> +                      &retlen,
>> +                      SVN_SUBR__DATE_MAX_LENGTH - len,
>> +                      human_timestamp_format_suffix,
>> +                      &exploded_time);
> 
> Okay, so in case of overlong length, we sacrifice the user-formatted
> part in favor of the machine-parseable part.  That seems reasonable.

Yes. We need to go through this again when doing it user-configurable
though, but for now it's fine.

> +1.  If this passes "make check", commit it.

Running it now, committing after it.

There's no reason why this wouldn't work exactly the same way on
Windows as well - but I just don't trust strftime (or rather, APR)
implicitely. So someone should mention if this works on the Windows as
well.

-- Naked

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Human representation date format change

Posted by Karl Fogel <kf...@newton.ch.collab.net>.
Nuutti Kotivuori <na...@iki.fi> writes:
> + * This format is used whenever time is shown to the user. It consists
> + * of a machine parseable, almost ISO-8601, part in the beginning -
> + * and a human explanative part at the end. The machine parseable part

I never heard of the word "explanative" before, but hey, English is a
Big Tent language.

> + * is generated strictly by APR and our code, with a apr_snprintf. The
> + * human explanative part is generated by apr_strftime, which means
> + * that it's generation can be affected by locale, it can fail and it

s/it's/its/

> +/* Maximum length for the date string. */
> +#define SVN_SUBR__DATE_MAX_LENGTH 80

SVN_TIME__MAX_LENGTH, maybe?  Should use "SVN_TIME__" prefix, anyway.

> +  /* Put in machine parseable part */
> +  len = apr_snprintf (datestr,
> +                      SVN_SUBR__DATE_MAX_LENGTH,
> +                      human_timestamp_format,
> +                      exploded_time.tm_year + 1900,
> +                      exploded_time.tm_mon + 1,
> +                      exploded_time.tm_mday,
> +                      exploded_time.tm_hour,
> +                      exploded_time.tm_min,
> +                      exploded_time.tm_sec,
> +                      exploded_time.tm_gmtoff / (60 * 60),
> +                      (exploded_time.tm_gmtoff / 60) % 60);
> +
> +  /* If we overfilled the buffer, just return what we got. */
> +  if(len >= SVN_SUBR__DATE_MAX_LENGTH)
> +    return datestr;

I got confused for a moment, but then learned that apr_snprintf()
returns the buffer's len (i.e., its second arg) in overfill case.
So ">=" is okay, but equivalent to "==", right?

> +  /* Calculate offset to the end of the machine parseable part. */
> +  curptr = datestr + len;
> +
> +  /* Put in human readable part */
> +  ret = apr_strftime (curptr,
> +                      &retlen,
> +                      SVN_SUBR__DATE_MAX_LENGTH - len,
> +                      human_timestamp_format_suffix,
> +                      &exploded_time);

Okay, so in case of overlong length, we sacrifice the user-formatted
part in favor of the machine-parseable part.  That seems reasonable.

+1.  If this passes "make check", commit it.

-K

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org