# HG changeset patch
# User Olaf Wintermann <olaf.wintermann@gmail.com>
# Date 1650469713 -7200
# Node ID 33911d44111d3e99405ef156b03c96f79ec6b12f
# Parent  ad9ba51c8634110f419e4e9cc05f04730874a260
add basic parser for pg timestamps

diff -r ad9ba51c8634 -r 33911d44111d src/server/plugins/postgresql/vfs.c
--- 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
+}
diff -r ad9ba51c8634 -r 33911d44111d src/server/plugins/postgresql/vfs.h
--- 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