add basic parser for pg timestamps webdav

Wed, 20 Apr 2022 17:48:33 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 20 Apr 2022 17:48:33 +0200
branch
webdav
changeset 304
33911d44111d
parent 303
ad9ba51c8634
child 305
4db64fe30588

add basic parser for pg timestamps

src/server/plugins/postgresql/vfs.c file | annotate | diff | comparison | revisions
src/server/plugins/postgresql/vfs.h file | annotate | diff | comparison | revisions
--- a/src/server/plugins/postgresql/vfs.c	Tue Apr 19 20:32:45 2022 +0200
+++ b/src/server/plugins/postgresql/vfs.c	Wed Apr 20 17:48:33 2022 +0200
@@ -287,9 +287,7 @@
             s->st_mode |= 0x4000;
         }
     }
-    // TODO: lastmodified, creationdate
-    // set some test values != 0
-    s->st_mtime = time(NULL);
+    s->st_mtime = pg_convert_timestamp(lastmodified);  
     
     if(contentlength) {
         int64_t len;
@@ -863,3 +861,51 @@
     pool_free(pool, pg);
     pool_free(pool, dir);
 }
+
+time_t pg_convert_timestamp(const char *timestamp) {
+    // TODO: this is a very basic implementation that needs some work
+    // format yyyy-mm-dd HH:MM:SS
+    
+    size_t len = timestamp ? strlen(timestamp) : 0;
+    if(len < 19) {
+        return 0;
+    } else if(len > 63) {
+        return 0;
+    }
+    
+    char buf[64];
+    memcpy(buf, timestamp, len);
+    
+    char *year_str = buf;
+    year_str[4] = '\0';
+    
+    char *month_str = buf + 5;
+    month_str[2] = '\0';
+    
+    char *day_str = buf + 8;
+    day_str[2] = '\0';
+    
+    char *hour_str = buf + 11;
+    hour_str[2] = '\0';
+    
+    char *minute_str = buf + 14;
+    minute_str[2] = '\0';
+    
+    char *second_str = buf + 17;
+    second_str[2] = '\0';
+    
+    struct tm tm;
+    memset(&tm, 0, sizeof(struct tm));
+    tm.tm_year = atoi(year_str) - 1900;
+    tm.tm_mon = atoi(month_str) - 1;
+    tm.tm_mday = atoi(day_str);
+    tm.tm_hour = atoi(hour_str);
+    tm.tm_min = atoi(minute_str);
+    tm.tm_sec = atoi(second_str);
+    
+#ifdef __FreeBSD__
+    return timelocal(&tm);
+#else
+    return mktime(&tparts) - timezone;
+#endif
+}
--- a/src/server/plugins/postgresql/vfs.h	Tue Apr 19 20:32:45 2022 +0200
+++ b/src/server/plugins/postgresql/vfs.h	Wed Apr 20 17:48:33 2022 +0200
@@ -132,6 +132,8 @@
 int pg_vfs_dirio_readdir(VFS_DIR dir, VFS_ENTRY *entry, int getstat);
 void pg_vfs_dirio_close(VFS_DIR dir);
 
+time_t pg_convert_timestamp(const char *timestamp);
+
 #ifdef __cplusplus
 }
 #endif

mercurial