[Date Prev][Date Next] [Chronological] [Thread] [Top]

Slapd strftime(3) ISO-8601 patch



The attached patch modifies slapd to make the generated timestamp
strings conform with ISO-8601.  It also includes the expansion of
the year from two to four digits and the use of a more human-readable
format.

A macro has been added to the top section of ldapconfig.h.edit to
allow a user to choose whether to use UTC or local time.  Two more
macros defined in the SLAPD section based on the choice.  One expands
to either 'localtime' or 'gmtime', the other to the strftime(3)
format string.

The SLAPD_STRFTIME_FORMAT values in the patch insert the punctuation
characters allowed by ISO-8601 to increase readability.  When UTC is
chosen, the 'Z' timezone qualifier will be used.  Since few if any
current implementations of strftime(3) have a format specifier for
the offset notation, local time is presented with no timezone indicator.
(ISO-8601 allows this as an unspecified offset from UTC.)

Since the format strings are in macros in a config file, I increased
the size of the time string buffers significantly more than was
actually necessary.

NOTE that since the slapd ldbm back-end stores the time values in
ASCII, this patch will not affect any values which are already in
your database(s).

These diffs were taken against stable-980929 with my previously
posted UID changes.  The patches do not directly overlap; but
the line numbers in the ldapconfig.h.edit diffs will be slightly
off from an unpatched version.



-Pat
diff -b -B -P -u -r stable-19980929/include/ldapconfig.h.edit stable-19980929-PML/include/ldapconfig.h.edit
--- stable-19980929/include/ldapconfig.h.edit	Wed Oct 14 22:27:02 1998
+++ stable-19980929-PML/include/ldapconfig.h.edit	Fri Oct 16 12:28:41 1998
@@ -46,6 +46,12 @@
 #define DEFAULT_BIND_CRED	NULL
 #define DEFAULT_BIND_CRED_FILE "%ETCDIR%/ldapbindcred"
 
+/*
+ * Uncomment this if you want slapd to format time values with the
+ * local timezone offset instead of in UTC.
+ */
+/* #define SLAPD_USE_LOCAL_TIME */
+
 /*********************************************************************
  *                                                                   *
  * You probably do not need to edit anything below this point        *
@@ -303,5 +309,26 @@
 #define SLAPD_DEFAULT_UID		0
 	/* gid to use after binding to socket - if 0 do not change gid */
 #define SLAPD_DEFAULT_GID		0
+
+#if defined(SLAPD_USE_LOCAL_TIME)
+	/* function to use to convert a time_t into struct tm */
+#define	SLAPD_TIME_FUNC			localtime
+	/*
+	 * format string to use with strftime
+	 * NOTE: ISO-8601 specifies that a value with no timezone specifier
+	 * is assumed to be at an unspecified offset from UTC.  When given,
+	 * a timezone specifier is a signed offset in hours and minutes from
+	 * UTC.  (E.g., -08:00 instead of PST)  There is no space between
+	 * the seconds in the value and the sign of the offset.  (E.g.,
+	 * 1998-10-16 11:59:46-08:00)  Most versions of strftime(3) don't
+	 * have a format specifier for the offset notation.
+	 */
+#define	SLAPD_STRFTIME_FORMAT		"%Y-%m-%d %H:%M:%S"
+#else
+	/* function to use to convert a time_t into struct tm */
+#define	SLAPD_TIME_FUNC			gmtime
+	/* format string to use with strftime */
+#define	SLAPD_STRFTIME_FORMAT		"%Y-%m-%d %H:%M:%SZ"
+#endif
 
 #endif /* _LDAP_CONFIG_H */
diff -b -B -P -u -r stable-19980929/servers/slapd/add.c stable-19980929-PML/servers/slapd/add.c
--- stable-19980929/servers/slapd/add.c	Tue Aug 18 16:30:57 1998
+++ stable-19980929-PML/servers/slapd/add.c	Fri Oct 16 13:01:47 1998
@@ -145,7 +145,7 @@
 static void
 add_created_attrs( Operation *op, Entry *e )
 {
-	char		buf[20];
+	char		buf[40];
 	struct berval	bv;
 	struct berval	*bvals[2];
 	Attribute	**a, **next;
@@ -182,8 +182,8 @@
 	attr_merge( e, "creatorsname", bvals );
 
 	pthread_mutex_lock( &currenttime_mutex );
-        ltm = localtime( &currenttime );
-        strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
+        ltm = SLAPD_TIME_FUNC( &currenttime );
+        strftime( buf, sizeof(buf), SLAPD_STRFTIME_FORMAT, ltm );
 	pthread_mutex_unlock( &currenttime_mutex );
 
 	bv.bv_val = buf;
diff -b -B -P -u -r stable-19980929/servers/slapd/modify.c stable-19980929-PML/servers/slapd/modify.c
--- stable-19980929/servers/slapd/modify.c	Tue Aug 18 16:30:57 1998
+++ stable-19980929-PML/servers/slapd/modify.c	Fri Oct 16 13:02:12 1998
@@ -200,7 +200,7 @@
 static void
 add_lastmods( Operation *op, LDAPMod **mods )
 {
-	char		buf[20];
+	char		buf[40];
 	struct berval	bv;
 	struct berval	*bvals[2];
 	LDAPMod		**m;
@@ -251,8 +251,8 @@
 	*mods = tmp;
 
 	pthread_mutex_lock( &currenttime_mutex );
-        ltm = localtime( &currenttime );
-        strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
+        ltm = SLAPD_TIME_FUNC( &currenttime );
+        strftime( buf, sizeof(buf), SLAPD_STRFTIME_FORMAT, ltm );
 	pthread_mutex_unlock( &currenttime_mutex );
 	bv.bv_val = buf;
 	bv.bv_len = strlen( bv.bv_val );
diff -b -B -P -u -r stable-19980929/servers/slapd/monitor.c stable-19980929-PML/servers/slapd/monitor.c
--- stable-19980929/servers/slapd/monitor.c	Sat Aug  8 19:13:59 1998
+++ stable-19980929-PML/servers/slapd/monitor.c	Fri Oct 16 13:03:44 1998
@@ -49,7 +49,7 @@
 monitor_info( Connection *conn, Operation *op )
 {
 	Entry		*e;
-	char		buf[BUFSIZ], buf2[20];
+	char		buf[BUFSIZ], buf2[40];
 	struct berval	val;
 	struct berval	*vals[2];
 	int		i, nconns, nwritewaiters, nreadwaiters;
@@ -91,8 +91,9 @@
 				nreadwaiters++;
 			}
 			pthread_mutex_lock( &currenttime_mutex );
-			ltm = localtime( &c[i].c_starttime );
-			strftime( buf2, sizeof(buf2), "%y%m%d%H%M%SZ", ltm );
+			ltm = SLAPD_TIME_FUNC( &c[i].c_starttime );
+			strftime( buf2, sizeof(buf2), SLAPD_STRFTIME_FORMAT,
+				  ltm );
 			pthread_mutex_unlock( &currenttime_mutex );
 
 			pthread_mutex_lock( &c[i].c_dnmutex );
@@ -155,16 +156,16 @@
 	attr_merge( e, "bytessent", vals );
 
 	pthread_mutex_lock( &currenttime_mutex );
-        ltm = localtime( &currenttime );
-        strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
+        ltm = SLAPD_TIME_FUNC( &currenttime );
+        strftime( buf, sizeof(buf), SLAPD_STRFTIME_FORMAT, ltm );
 	pthread_mutex_unlock( &currenttime_mutex );
 	val.bv_val = buf;
 	val.bv_len = strlen( buf );
 	attr_merge( e, "currenttime", vals );
 
 	pthread_mutex_lock( &currenttime_mutex );
-        ltm = localtime( &starttime );
-        strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
+        ltm = SLAPD_TIME_FUNC( &starttime );
+        strftime( buf, sizeof(buf), SLAPD_STRFTIME_FORMAT, ltm );
 	pthread_mutex_unlock( &currenttime_mutex );
 	val.bv_val = buf;
 	val.bv_len = strlen( buf );
diff -b -B -P -u -r stable-19980929/servers/slapd/slap.h stable-19980929-PML/servers/slapd/slap.h
--- stable-19980929/servers/slapd/slap.h	Thu Aug 20 21:33:42 1998
+++ stable-19980929-PML/servers/slapd/slap.h	Fri Oct 16 12:41:13 1998
@@ -11,6 +11,7 @@
 
 #include "avl.h"
 #include "lber.h"
+#include "ldapconfig.h"
 #include "ldap.h"
 #include "lthread.h"
 #include "ldif.h"