comparison src/c/urweb.c @ 1352:703c2c94afd5

Use proper string time format for SQLite
author Adam Chlipala <adam@chlipala.net>
date Mon, 20 Dec 2010 13:22:44 -0500
parents 87156c44824f
children e9cf053108ed
comparison
equal deleted inserted replaced
1351:74d35d9a5d16 1352:703c2c94afd5
2053 return uw_unit_v; 2053 return uw_unit_v;
2054 } 2054 }
2055 2055
2056 #define TIME_FMT "%x %X" 2056 #define TIME_FMT "%x %X"
2057 #define TIME_FMT_PG "%Y-%m-%d %T" 2057 #define TIME_FMT_PG "%Y-%m-%d %T"
2058 #define TIME_FMT_PG "%Y-%m-%d %T"
2058 2059
2059 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) { 2060 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
2060 size_t len; 2061 size_t len;
2061 char *r; 2062 char *r;
2062 struct tm stm; 2063 struct tm stm;
2558 return r; 2559 return r;
2559 } else 2560 } else
2560 return "<Invalid time>"; 2561 return "<Invalid time>";
2561 } 2562 }
2562 2563
2564 uw_Basis_string uw_Basis_timeToStringf(uw_context ctx, const char *fmt, uw_Basis_time t) {
2565 size_t len;
2566 char *r;
2567 struct tm stm;
2568
2569 if (localtime_r(&t, &stm)) {
2570 uw_check_heap(ctx, TIMES_MAX);
2571 r = ctx->heap.front;
2572 len = strftime(r, TIMES_MAX, fmt, &stm);
2573 ctx->heap.front += len+1;
2574 return r;
2575 } else
2576 return "<Invalid time>";
2577 }
2578
2563 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) { 2579 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
2564 char *endptr; 2580 char *endptr;
2565 uw_Basis_int n = strtoll(s, &endptr, 10); 2581 uw_Basis_int n = strtoll(s, &endptr, 10);
2566 2582
2567 if (*s != '\0' && *endptr == '\0') { 2583 if (*s != '\0' && *endptr == '\0') {
2641 else 2657 else
2642 return NULL; 2658 return NULL;
2643 } 2659 }
2644 } 2660 }
2645 2661
2662 uw_Basis_time *uw_Basis_stringToTimef(uw_context ctx, const char *fmt, uw_Basis_string s) {
2663 char *end = strchr(s, 0);
2664 struct tm stm;
2665
2666 if (strptime(s, fmt, &stm) == end) {
2667 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
2668 *r = mktime(&stm);
2669 return r;
2670 }
2671 else
2672 return NULL;
2673 }
2674
2646 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) { 2675 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
2647 char *endptr; 2676 char *endptr;
2648 uw_Basis_int n = strtoll(s, &endptr, 10); 2677 uw_Basis_int n = strtoll(s, &endptr, 10);
2649 2678
2650 if (*s != '\0' && *endptr == '\0') 2679 if (*s != '\0' && *endptr == '\0')
2702 return uw_Basis_False; 2731 return uw_Basis_False;
2703 else 2732 else
2704 uw_error(ctx, FATAL, "Can't parse bool: %s", s); 2733 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
2705 } 2734 }
2706 2735
2736 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
2737 char *dot = strchr(s, '.'), *end = strchr(s, 0);
2738 struct tm stm = {};
2739
2740 if (dot) {
2741 *dot = 0;
2742 if (strptime(s, TIME_FMT_PG, &stm)) {
2743 *dot = '.';
2744 return mktime(&stm);
2745 }
2746 else {
2747 *dot = '.';
2748 uw_error(ctx, FATAL, "Can't parse time: %s", s);
2749 }
2750 }
2751 else {
2752 if (strptime(s, TIME_FMT_PG, &stm) == end) {
2753 return mktime(&stm);
2754 } else if (strptime(s, TIME_FMT, &stm) == end) {
2755 return mktime(&stm);
2756 } else
2757 uw_error(ctx, FATAL, "Can't parse time: %s", s);
2758 }
2759 }
2760
2707 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) { 2761 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
2708 char *dot = strchr(s, '.'), *end = strchr(s, 0); 2762 char *dot = strchr(s, '.'), *end = strchr(s, 0);
2709 struct tm stm = {}; 2763 struct tm stm = {};
2710 2764
2711 if (dot) { 2765 if (dot) {
2727 else 2781 else
2728 uw_error(ctx, FATAL, "Can't parse time: %s", s); 2782 uw_error(ctx, FATAL, "Can't parse time: %s", s);
2729 } 2783 }
2730 } 2784 }
2731 2785
2732 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) { 2786 uw_Basis_time uw_Basis_stringToTimef_error(uw_context ctx, const char *fmt, uw_Basis_string s) {
2733 char *dot = strchr(s, '.'), *end = strchr(s, 0); 2787 char *end = strchr(s, 0);
2734 struct tm stm = {}; 2788 struct tm stm = {};
2735 2789
2736 if (dot) { 2790 if (strptime(s, fmt, &stm) == end)
2737 *dot = 0; 2791 return mktime(&stm);
2738 if (strptime(s, TIME_FMT_PG, &stm)) { 2792 else
2739 *dot = '.'; 2793 uw_error(ctx, FATAL, "Can't parse time: %s", s);
2740 return mktime(&stm);
2741 }
2742 else {
2743 *dot = '.';
2744 uw_error(ctx, FATAL, "Can't parse time: %s", s);
2745 }
2746 }
2747 else {
2748 if (strptime(s, TIME_FMT_PG, &stm) == end) {
2749 return mktime(&stm);
2750 } else if (strptime(s, TIME_FMT, &stm) == end) {
2751 return mktime(&stm);
2752 } else
2753 uw_error(ctx, FATAL, "Can't parse time: %s", s);
2754 }
2755 } 2794 }
2756 2795
2757 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) { 2796 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
2758 char *r = ctx->heap.front; 2797 char *r = ctx->heap.front;
2759 uw_Basis_blob b = {len, r}; 2798 uw_Basis_blob b = {len, r};