comparison 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
comparison
equal deleted inserted replaced
437:1a4c1b5f4d8f 438:1c27f03d9bd2
726 726
727 return uw_unit_v; 727 return uw_unit_v;
728 } 728 }
729 729
730 #define TIME_FMT "%x %X" 730 #define TIME_FMT "%x %X"
731 #define TIME_FMT_PG "%Y-%m-%d %T"
731 732
732 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) { 733 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
733 size_t len; 734 size_t len;
734 char *r; 735 char *r;
735 struct tm stm; 736 struct tm stm;
948 else 949 else
949 return NULL; 950 return NULL;
950 } 951 }
951 952
952 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) { 953 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
953 char *end = strchr(s, 0); 954 char *dot = strchr(s, '.'), *end = strchr(s, 0);
954 struct tm stm; 955 struct tm stm;
955 956
956 if (strptime(s, TIME_FMT, &stm) == end) { 957 if ((dot ? (*dot = 0, strptime(s, TIME_FMT_PG, &stm)) : strptime(s, TIME_FMT, &stm)) == end) {
957 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); 958 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
958 *r = mktime(&stm); 959 *r = mktime(&stm);
959 return r; 960 return r;
960 } 961 }
961 else 962 else
990 else 991 else
991 uw_error(ctx, FATAL, "Can't parse bool: %s", s); 992 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
992 } 993 }
993 994
994 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) { 995 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
995 char *end = strchr(s, 0); 996 char *dot = strchr(s, '.'), *end = strchr(s, 0);
996 struct tm stm = {}; 997 struct tm stm = {};
997 998
998 if (strptime(s, TIME_FMT, &stm) == end) 999 if (dot) {
999 return mktime(&stm); 1000 *dot = 0;
1000 else 1001 if (strptime(s, TIME_FMT_PG, &stm)) {
1001 uw_error(ctx, FATAL, "Can't parse time: %s", s); 1002 *dot = '.';
1002 } 1003 return mktime(&stm);
1004 }
1005 else {
1006 *dot = '.';
1007 uw_error(ctx, FATAL, "Can't parse time: %s", s);
1008 }
1009 }
1010 else {
1011 if (strptime(s, TIME_FMT, &stm) == end)
1012 return mktime(&stm);
1013 else
1014 uw_error(ctx, FATAL, "Can't parse time: %s", s);
1015 }
1016 }