comparison src/c/urweb.c @ 1632:4682b312e9d5

Fix client-side [int] parsing and extend server-side [time] parsing to support a format that also works portably in JavaScript
author Adam Chlipala <adam@chlipala.net>
date Sun, 04 Dec 2011 16:32:06 -0500
parents 438561303d02
children f9ffe8497742
comparison
equal deleted inserted replaced
1631:747f94ac5fc2 1632:4682b312e9d5
2148 return uw_unit_v; 2148 return uw_unit_v;
2149 } 2149 }
2150 2150
2151 #define TIME_FMT "%x %X" 2151 #define TIME_FMT "%x %X"
2152 #define TIME_FMT_PG "%Y-%m-%d %T" 2152 #define TIME_FMT_PG "%Y-%m-%d %T"
2153 #define TIME_FMT_JS "%Y/%m/%d %T"
2153 2154
2154 uw_Basis_string uw_Basis_timeToString(uw_context, uw_Basis_time); 2155 uw_Basis_string uw_Basis_timeToString(uw_context, uw_Basis_time);
2155 2156
2156 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) { 2157 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
2157 return uw_Basis_htmlifyString(ctx, uw_Basis_timeToString(ctx, t)); 2158 return uw_Basis_htmlifyString(ctx, uw_Basis_timeToString(ctx, t));
2797 else if (strptime(s, TIME_FMT, &stm) == end) { 2798 else if (strptime(s, TIME_FMT, &stm) == end) {
2798 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); 2799 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
2799 r->seconds = mktime(&stm); 2800 r->seconds = mktime(&stm);
2800 r->microseconds = 0; 2801 r->microseconds = 0;
2801 return r; 2802 return r;
2803 } else if (strptime(s, TIME_FMT_JS, &stm) == end) {
2804 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
2805 r->seconds = mktime(&stm);
2806 r->microseconds = 0;
2807 return r;
2802 } 2808 }
2803 else 2809 else
2804 return NULL; 2810 return NULL;
2805 } 2811 }
2806 } 2812 }
2935 else { 2941 else {
2936 if (strptime(s, TIME_FMT_PG, &stm) == end) { 2942 if (strptime(s, TIME_FMT_PG, &stm) == end) {
2937 uw_Basis_time r = { mktime(&stm) }; 2943 uw_Basis_time r = { mktime(&stm) };
2938 return r; 2944 return r;
2939 } else if (strptime(s, TIME_FMT, &stm) == end) { 2945 } else if (strptime(s, TIME_FMT, &stm) == end) {
2946 uw_Basis_time r = { mktime(&stm) };
2947 return r;
2948 } else if (strptime(s, TIME_FMT_JS, &stm) == end) {
2940 uw_Basis_time r = { mktime(&stm) }; 2949 uw_Basis_time r = { mktime(&stm) };
2941 return r; 2950 return r;
2942 } else 2951 } else
2943 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s)); 2952 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
2944 } 2953 }
3881 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) { 3890 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) {
3882 struct tm stm = {}; 3891 struct tm stm = {};
3883 char *end = strchr(s, 0); 3892 char *end = strchr(s, 0);
3884 stm.tm_isdst = -1; 3893 stm.tm_isdst = -1;
3885 3894
3886 if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end) { 3895 if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end || strptime(s, TIME_FMT_JS, &stm) == end) {
3887 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); 3896 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
3888 3897
3889 r->seconds = timegm(&stm); 3898 r->seconds = timegm(&stm);
3890 r->microseconds = 0; 3899 r->microseconds = 0;
3891 3900