diff src/c/urweb.c @ 438:1c27f03d9bd2

Reading timestamps from SQL
author Adam Chlipala <adamc@hcoop.net>
date Thu, 30 Oct 2008 14:57:15 -0400
parents 024478c34f4d
children 322c8620bbdf
line wrap: on
line diff
--- a/src/c/urweb.c	Thu Oct 30 14:40:42 2008 -0400
+++ b/src/c/urweb.c	Thu Oct 30 14:57:15 2008 -0400
@@ -728,6 +728,7 @@
 }
 
 #define TIME_FMT "%x %X"
+#define TIME_FMT_PG "%Y-%m-%d %T"
 
 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
   size_t len;
@@ -950,10 +951,10 @@
 }
 
 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
-  char *end = strchr(s, 0);
+  char *dot = strchr(s, '.'), *end = strchr(s, 0);
   struct tm stm;
 
-  if (strptime(s, TIME_FMT, &stm) == end) {
+  if ((dot ? (*dot = 0, strptime(s, TIME_FMT_PG, &stm)) : strptime(s, TIME_FMT, &stm)) == end) {
     uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
     *r = mktime(&stm);
     return r;    
@@ -992,11 +993,24 @@
 }
 
 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
-  char *end = strchr(s, 0);
+  char *dot = strchr(s, '.'), *end = strchr(s, 0);
   struct tm stm = {};
 
-  if (strptime(s, TIME_FMT, &stm) == end)
-    return mktime(&stm);
-  else
-    uw_error(ctx, FATAL, "Can't parse time: %s", s);
+  if (dot) {
+    *dot = 0;
+    if (strptime(s, TIME_FMT_PG, &stm)) {
+      *dot = '.';
+      return mktime(&stm);
+    }
+    else {
+      *dot = '.';
+      uw_error(ctx, FATAL, "Can't parse time: %s", s);
+    }
+  }
+  else {
+    if (strptime(s, TIME_FMT, &stm) == end)
+      return mktime(&stm);
+    else
+      uw_error(ctx, FATAL, "Can't parse time: %s", s);
+  }
 }