svn commit: samba r20462 - in branches/SAMBA_4_0/source/lib/ldb: common include

metze at samba.org metze at samba.org
Tue Jan 2 10:59:40 GMT 2007


Author: metze
Date: 2007-01-02 10:59:38 +0000 (Tue, 02 Jan 2007)
New Revision: 20462

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=20462

Log:
add functions to handle UTCTime strings

metze
Modified:
   branches/SAMBA_4_0/source/lib/ldb/common/ldb_msg.c
   branches/SAMBA_4_0/source/lib/ldb/include/ldb.h


Changeset:
Modified: branches/SAMBA_4_0/source/lib/ldb/common/ldb_msg.c
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/common/ldb_msg.c	2007-01-02 08:46:55 UTC (rev 20461)
+++ branches/SAMBA_4_0/source/lib/ldb/common/ldb_msg.c	2007-01-02 10:59:38 UTC (rev 20462)
@@ -757,7 +757,7 @@
 }
 
 /*
-  return a LDAP formatted time string
+  return a LDAP formatted GeneralizedTime string
 */
 char *ldb_timestring(TALLOC_CTX *mem_ctx, time_t t)
 {
@@ -787,9 +787,8 @@
 	return ts;
 }
 
-
 /*
-  convert a LDAP time string to a time_t. Return 0 if unable to convert
+  convert a LDAP GeneralizedTime string to a time_t. Return 0 if unable to convert
 */
 time_t ldb_string_to_time(const char *s)
 {
@@ -809,8 +808,62 @@
 	return timegm(&tm);
 }
 
+/*
+  return a LDAP formatted UTCTime string
+*/
+char *ldb_timestring_utc(TALLOC_CTX *mem_ctx, time_t t)
+{
+	struct tm *tm = gmtime(&t);
+	char *ts;
+	int r;
 
+	if (!tm) {
+		return NULL;
+	}
+
+	/* we now excatly how long this string will be */
+	ts = talloc_array(mem_ctx, char, 14);
+
+	/* formatted like: 20040408072012.0Z => 040408072012Z */
+	r = snprintf(ts, 14,
+			"%02u%02u%02u%02u%02u%02uZ",
+			(tm->tm_year+1900)%100, tm->tm_mon+1,
+			tm->tm_mday, tm->tm_hour, tm->tm_min,
+			tm->tm_sec);
+
+	if (r != 13) {
+		talloc_free(ts);
+		return NULL;
+	}
+
+	return ts;
+}
+
 /*
+  convert a LDAP UTCTime string to a time_t. Return 0 if unable to convert
+*/
+time_t ldb_string_utc_to_time(const char *s)
+{
+	struct tm tm;
+	
+	if (s == NULL) return 0;
+	
+	memset(&tm, 0, sizeof(tm));
+	if (sscanf(s, "%02u%02u%02u%02u%02u%02u", 
+		   &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 
+		   &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
+		return 0;
+	}
+	if (tm.tm_year < 50) {
+		tm.tm_year += 100;
+	}
+	tm.tm_mon -= 1;
+	
+	return timegm(&tm);
+}
+
+
+/*
   dump a set of results to a file. Useful from within gdb
 */
 void ldb_dump_results(struct ldb_context *ldb, struct ldb_result *result, FILE *f)

Modified: branches/SAMBA_4_0/source/lib/ldb/include/ldb.h
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/include/ldb.h	2007-01-02 08:46:55 UTC (rev 20461)
+++ branches/SAMBA_4_0/source/lib/ldb/include/ldb.h	2007-01-02 10:59:38 UTC (rev 20462)
@@ -1550,9 +1550,9 @@
 /**
    Convert a time structure to a string
 
-   This function converts a time_t structure to an LDAP formatted time
-   string.
-
+   This function converts a time_t structure to an LDAP formatted
+   GeneralizedTime string.
+		
    \param mem_ctx the memory context to allocate the return string in
    \param t the time structure to convert
 
@@ -1564,8 +1564,8 @@
 /**
    Convert a string to a time structure
 
-   This function converts an LDAP formatted time string to a time_t
-   structure.
+   This function converts an LDAP formatted GeneralizedTime string
+   to a time_t structure.
 
    \param s the string to convert
 
@@ -1573,6 +1573,32 @@
 */
 time_t ldb_string_to_time(const char *s);
 
+/**
+   Convert a time structure to a string
 
+   This function converts a time_t structure to an LDAP formatted
+   UTCTime string.
+		
+   \param mem_ctx the memory context to allocate the return string in
+   \param t the time structure to convert
+
+   \return the formatted string, or NULL if the time structure could
+   not be converted
+*/
+char *ldb_timestring_utc(void *mem_ctx, time_t t);
+
+/**
+   Convert a string to a time structure
+
+   This function converts an LDAP formatted UTCTime string
+   to a time_t structure.
+
+   \param s the string to convert
+
+   \return the time structure, or 0 if the string cannot be converted
+*/
+time_t ldb_string_utc_to_time(const char *s);
+
+
 void ldb_qsort (void *const pbase, size_t total_elems, size_t size, void *opaque, ldb_qsort_cmp_fn_t cmp);
 #endif



More information about the samba-cvs mailing list