Mercurial > urweb
comparison src/c/urweb.c @ 439:322c8620bbdf
Marshaling time to SQL
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Thu, 30 Oct 2008 15:11:37 -0400 |
parents | 1c27f03d9bd2 |
children | 360cbc202756 |
comparison
equal
deleted
inserted
replaced
438:1c27f03d9bd2 | 439:322c8620bbdf |
---|---|
858 return "FALSE"; | 858 return "FALSE"; |
859 else | 859 else |
860 return "TRUE"; | 860 return "TRUE"; |
861 } | 861 } |
862 | 862 |
863 char *uw_Basis_ensqlBool(uw_Basis_bool b) { | 863 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) { |
864 static uw_Basis_int true = 1; | |
865 static uw_Basis_int false = 0; | |
866 | |
867 if (b == uw_Basis_False) | |
868 return (char *)&false; | |
869 else | |
870 return (char *)&true; | |
871 } | |
872 | |
873 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) { | |
874 int len; | |
875 char *r; | |
876 | |
877 uw_check_heap(ctx, INTS_MAX); | |
878 r = ctx->heap_front; | |
879 sprintf(r, "%lld%n", n, &len); | |
880 ctx->heap_front += len+1; | |
881 return r; | |
882 } | |
883 | |
884 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) { | |
885 int len; | |
886 char *r; | |
887 | |
888 uw_check_heap(ctx, FLOATS_MAX); | |
889 r = ctx->heap_front; | |
890 sprintf(r, "%g%n", n, &len); | |
891 ctx->heap_front += len+1; | |
892 return r; | |
893 } | |
894 | |
895 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) { | |
896 if (b == uw_Basis_False) | |
897 return "False"; | |
898 else | |
899 return "True"; | |
900 } | |
901 | |
902 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) { | |
903 size_t len; | 864 size_t len; |
904 char *r; | 865 char *r; |
905 struct tm stm; | 866 struct tm stm; |
906 | 867 |
907 if (localtime_r(&t, &stm)) { | 868 if (localtime_r(&t, &stm)) { |
912 return r; | 873 return r; |
913 } else | 874 } else |
914 return "<Invalid time>"; | 875 return "<Invalid time>"; |
915 } | 876 } |
916 | 877 |
878 char *uw_Basis_ensqlBool(uw_Basis_bool b) { | |
879 static uw_Basis_int true = 1; | |
880 static uw_Basis_int false = 0; | |
881 | |
882 if (b == uw_Basis_False) | |
883 return (char *)&false; | |
884 else | |
885 return (char *)&true; | |
886 } | |
887 | |
888 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) { | |
889 int len; | |
890 char *r; | |
891 | |
892 uw_check_heap(ctx, INTS_MAX); | |
893 r = ctx->heap_front; | |
894 sprintf(r, "%lld%n", n, &len); | |
895 ctx->heap_front += len+1; | |
896 return r; | |
897 } | |
898 | |
899 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) { | |
900 int len; | |
901 char *r; | |
902 | |
903 uw_check_heap(ctx, FLOATS_MAX); | |
904 r = ctx->heap_front; | |
905 sprintf(r, "%g%n", n, &len); | |
906 ctx->heap_front += len+1; | |
907 return r; | |
908 } | |
909 | |
910 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) { | |
911 if (b == uw_Basis_False) | |
912 return "False"; | |
913 else | |
914 return "True"; | |
915 } | |
916 | |
917 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) { | |
918 size_t len; | |
919 char *r; | |
920 struct tm stm; | |
921 | |
922 if (localtime_r(&t, &stm)) { | |
923 uw_check_heap(ctx, TIMES_MAX); | |
924 r = ctx->heap_front; | |
925 len = strftime(r, TIMES_MAX, TIME_FMT, &stm); | |
926 ctx->heap_front += len+1; | |
927 return r; | |
928 } else | |
929 return "<Invalid time>"; | |
930 } | |
931 | |
917 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) { | 932 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) { |
918 char *endptr; | 933 char *endptr; |
919 uw_Basis_int n = strtoll(s, &endptr, 10); | 934 uw_Basis_int n = strtoll(s, &endptr, 10); |
920 | 935 |
921 if (*s != '\0' && *endptr == '\0') { | 936 if (*s != '\0' && *endptr == '\0') { |
952 | 967 |
953 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) { | 968 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) { |
954 char *dot = strchr(s, '.'), *end = strchr(s, 0); | 969 char *dot = strchr(s, '.'), *end = strchr(s, 0); |
955 struct tm stm; | 970 struct tm stm; |
956 | 971 |
957 if ((dot ? (*dot = 0, strptime(s, TIME_FMT_PG, &stm)) : strptime(s, TIME_FMT, &stm)) == end) { | 972 if (dot) { |
958 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); | 973 *dot = 0; |
959 *r = mktime(&stm); | 974 if (strptime(s, TIME_FMT_PG, &stm) == end) { |
960 return r; | 975 *dot = '.'; |
961 } | 976 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); |
962 else | 977 *r = mktime(&stm); |
963 return NULL; | 978 return r; |
979 } | |
980 else { | |
981 *dot = '.'; | |
982 return NULL; | |
983 } | |
984 } | |
985 else { | |
986 if (strptime(s, TIME_FMT_PG, &stm) == end) { | |
987 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); | |
988 *r = mktime(&stm); | |
989 return r; | |
990 } | |
991 else if (strptime(s, TIME_FMT, &stm) == end) { | |
992 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); | |
993 *r = mktime(&stm); | |
994 return r; | |
995 } | |
996 else | |
997 return NULL; | |
998 } | |
964 } | 999 } |
965 | 1000 |
966 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) { | 1001 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) { |
967 char *endptr; | 1002 char *endptr; |
968 uw_Basis_int n = strtoll(s, &endptr, 10); | 1003 uw_Basis_int n = strtoll(s, &endptr, 10); |
1006 *dot = '.'; | 1041 *dot = '.'; |
1007 uw_error(ctx, FATAL, "Can't parse time: %s", s); | 1042 uw_error(ctx, FATAL, "Can't parse time: %s", s); |
1008 } | 1043 } |
1009 } | 1044 } |
1010 else { | 1045 else { |
1011 if (strptime(s, TIME_FMT, &stm) == end) | 1046 if (strptime(s, TIME_FMT_PG, &stm) == end) |
1047 return mktime(&stm); | |
1048 else if (strptime(s, TIME_FMT, &stm) == end) | |
1012 return mktime(&stm); | 1049 return mktime(&stm); |
1013 else | 1050 else |
1014 uw_error(ctx, FATAL, "Can't parse time: %s", s); | 1051 uw_error(ctx, FATAL, "Can't parse time: %s", s); |
1015 } | 1052 } |
1016 } | 1053 } |