comparison src/c/urweb.c @ 1392:e305ffee2b5b

Fixing daylight saving time in time string processing
author Adam Chlipala <adam@chlipala.net>
date Thu, 13 Jan 2011 13:20:14 -0500
parents 65fbb250b875
children a5bfed32d4f7
comparison
equal deleted inserted replaced
1391:59c8a19bfb22 1392:e305ffee2b5b
2099 #define TIME_FMT_PG "%Y-%m-%d %T" 2099 #define TIME_FMT_PG "%Y-%m-%d %T"
2100 2100
2101 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) { 2101 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
2102 size_t len; 2102 size_t len;
2103 char *r; 2103 char *r;
2104 struct tm stm; 2104 struct tm stm = {};
2105 stm.tm_isdst = -1;
2105 2106
2106 if (localtime_r(&t.seconds, &stm)) { 2107 if (localtime_r(&t.seconds, &stm)) {
2107 uw_check_heap(ctx, TIMES_MAX); 2108 uw_check_heap(ctx, TIMES_MAX);
2108 r = ctx->heap.front; 2109 r = ctx->heap.front;
2109 len = strftime(r, TIMES_MAX, TIME_FMT, &stm); 2110 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
2114 } 2115 }
2115 2116
2116 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) { 2117 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
2117 size_t len; 2118 size_t len;
2118 char *r; 2119 char *r;
2119 struct tm stm; 2120 struct tm stm = {};
2121 stm.tm_isdst = -1;
2120 2122
2121 if (localtime_r(&t.seconds, &stm)) { 2123 if (localtime_r(&t.seconds, &stm)) {
2122 uw_check(ctx, TIMES_MAX); 2124 uw_check(ctx, TIMES_MAX);
2123 r = ctx->page.front; 2125 r = ctx->page.front;
2124 len = strftime(r, TIMES_MAX, TIME_FMT, &stm); 2126 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
2541 } 2543 }
2542 2544
2543 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) { 2545 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
2544 size_t len; 2546 size_t len;
2545 char *r, *s; 2547 char *r, *s;
2546 struct tm stm; 2548 struct tm stm = {};
2549 stm.tm_isdst = -1;
2547 2550
2548 if (localtime_r(&t.seconds, &stm)) { 2551 if (localtime_r(&t.seconds, &stm)) {
2549 s = uw_malloc(ctx, TIMES_MAX); 2552 s = uw_malloc(ctx, TIMES_MAX);
2550 len = strftime(s, TIMES_MAX, TIME_FMT_PG, &stm); 2553 len = strftime(s, TIMES_MAX, TIME_FMT_PG, &stm);
2551 if (t.microseconds) { 2554 if (t.microseconds) {
2561 } 2564 }
2562 2565
2563 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) { 2566 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
2564 size_t len; 2567 size_t len;
2565 char *r; 2568 char *r;
2566 struct tm stm; 2569 struct tm stm = {};
2570 stm.tm_isdst = -1;
2567 2571
2568 if (localtime_r(&t.seconds, &stm)) { 2572 if (localtime_r(&t.seconds, &stm)) {
2569 uw_check_heap(ctx, TIMES_MAX); 2573 uw_check_heap(ctx, TIMES_MAX);
2570 r = ctx->heap.front; 2574 r = ctx->heap.front;
2571 len = strftime(r, TIMES_MAX, TIME_FMT, &stm); 2575 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
2576 } 2580 }
2577 2581
2578 char *uw_Basis_ensqlTime(uw_context ctx, uw_Basis_time t) { 2582 char *uw_Basis_ensqlTime(uw_context ctx, uw_Basis_time t) {
2579 size_t len; 2583 size_t len;
2580 char *r; 2584 char *r;
2581 struct tm stm; 2585 struct tm stm = {};
2586 stm.tm_isdst = -1;
2582 2587
2583 if (localtime_r(&t.seconds, &stm)) { 2588 if (localtime_r(&t.seconds, &stm)) {
2584 uw_check_heap(ctx, TIMES_MAX); 2589 uw_check_heap(ctx, TIMES_MAX);
2585 r = ctx->heap.front; 2590 r = ctx->heap.front;
2586 len = strftime(r, TIMES_MAX-7, TIME_FMT_PG, &stm); 2591 len = strftime(r, TIMES_MAX-7, TIME_FMT_PG, &stm);
2646 } 2651 }
2647 2652
2648 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) { 2653 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
2649 size_t len; 2654 size_t len;
2650 char *r; 2655 char *r;
2651 struct tm stm; 2656 struct tm stm = {};
2657 stm.tm_isdst = -1;
2652 2658
2653 if (localtime_r(&t.seconds, &stm)) { 2659 if (localtime_r(&t.seconds, &stm)) {
2654 uw_check_heap(ctx, TIMES_MAX); 2660 uw_check_heap(ctx, TIMES_MAX);
2655 r = ctx->heap.front; 2661 r = ctx->heap.front;
2656 len = strftime(r, TIMES_MAX, TIME_FMT, &stm); 2662 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
2661 } 2667 }
2662 2668
2663 uw_Basis_string uw_Basis_timef(uw_context ctx, const char *fmt, uw_Basis_time t) { 2669 uw_Basis_string uw_Basis_timef(uw_context ctx, const char *fmt, uw_Basis_time t) {
2664 size_t len; 2670 size_t len;
2665 char *r; 2671 char *r;
2666 struct tm stm; 2672 struct tm stm = {};
2673 stm.tm_isdst = -1;
2667 2674
2668 if (localtime_r(&t.seconds, &stm)) { 2675 if (localtime_r(&t.seconds, &stm)) {
2669 uw_check_heap(ctx, TIMES_MAX); 2676 uw_check_heap(ctx, TIMES_MAX);
2670 r = ctx->heap.front; 2677 r = ctx->heap.front;
2671 len = strftime(r, TIMES_MAX, fmt, &stm); 2678 len = strftime(r, TIMES_MAX, fmt, &stm);
2726 } 2733 }
2727 2734
2728 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) { 2735 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
2729 char *dot = strchr(s, '.'), *end = strchr(s, 0); 2736 char *dot = strchr(s, '.'), *end = strchr(s, 0);
2730 struct tm stm = {}; 2737 struct tm stm = {};
2738 stm.tm_isdst = -1;
2731 2739
2732 if (dot) { 2740 if (dot) {
2733 *dot = 0; 2741 *dot = 0;
2734 if (strptime(s, TIME_FMT_PG, &stm) == end) { 2742 if (strptime(s, TIME_FMT_PG, &stm) == end) {
2735 *dot = '.'; 2743 *dot = '.';
2762 } 2770 }
2763 2771
2764 uw_Basis_time *uw_Basis_stringToTimef(uw_context ctx, const char *fmt, uw_Basis_string s) { 2772 uw_Basis_time *uw_Basis_stringToTimef(uw_context ctx, const char *fmt, uw_Basis_string s) {
2765 char *end = strchr(s, 0); 2773 char *end = strchr(s, 0);
2766 struct tm stm = {}; 2774 struct tm stm = {};
2775 stm.tm_isdst = -1;
2767 2776
2768 if (strptime(s, fmt, &stm) == end) { 2777 if (strptime(s, fmt, &stm) == end) {
2769 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); 2778 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
2770 r->seconds = mktime(&stm); 2779 r->seconds = mktime(&stm);
2771 r->microseconds = 0; 2780 r->microseconds = 0;
2837 } 2846 }
2838 2847
2839 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) { 2848 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
2840 char *dot = strchr(s, '.'), *end = strchr(s, 0); 2849 char *dot = strchr(s, '.'), *end = strchr(s, 0);
2841 struct tm stm = {}; 2850 struct tm stm = {};
2851 stm.tm_isdst = -1;
2842 2852
2843 if (dot) { 2853 if (dot) {
2844 *dot = 0; 2854 *dot = 0;
2845 if (strptime(s, TIME_FMT_PG, &stm)) { 2855 if (strptime(s, TIME_FMT_PG, &stm)) {
2846 *dot = '.'; 2856 *dot = '.';
2865 } 2875 }
2866 2876
2867 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) { 2877 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
2868 char *dot = strchr(s, '.'), *end = strchr(s, 0); 2878 char *dot = strchr(s, '.'), *end = strchr(s, 0);
2869 struct tm stm = {}; 2879 struct tm stm = {};
2880 stm.tm_isdst = -1;
2870 2881
2871 if (dot) { 2882 if (dot) {
2872 *dot = 0; 2883 *dot = 0;
2873 if (strptime(s, TIME_FMT_PG, &stm)) { 2884 if (strptime(s, TIME_FMT_PG, &stm)) {
2874 *dot = '.'; 2885 *dot = '.';
2895 } 2906 }
2896 2907
2897 uw_Basis_time uw_Basis_stringToTimef_error(uw_context ctx, const char *fmt, uw_Basis_string s) { 2908 uw_Basis_time uw_Basis_stringToTimef_error(uw_context ctx, const char *fmt, uw_Basis_string s) {
2898 char *end = strchr(s, 0); 2909 char *end = strchr(s, 0);
2899 struct tm stm = {}; 2910 struct tm stm = {};
2911 stm.tm_isdst = -1;
2900 2912
2901 if (strptime(s, fmt, &stm) == end) { 2913 if (strptime(s, fmt, &stm) == end) {
2902 uw_Basis_time r = { mktime(&stm) }; 2914 uw_Basis_time r = { mktime(&stm) };
2903 return r; 2915 return r;
2904 } else 2916 } else
2992 uw_write_header(ctx, v); 3004 uw_write_header(ctx, v);
2993 uw_write_header(ctx, "; path="); 3005 uw_write_header(ctx, "; path=");
2994 uw_write_header(ctx, prefix); 3006 uw_write_header(ctx, prefix);
2995 if (expires) { 3007 if (expires) {
2996 char formatted[30]; 3008 char formatted[30];
2997 struct tm tm; 3009 struct tm tm = {};
3010 tm.tm_isdst = -1;
2998 3011
2999 gmtime_r(&expires->seconds, &tm); 3012 gmtime_r(&expires->seconds, &tm);
3000 3013
3001 strftime(formatted, sizeof formatted, "%a, %d-%b-%Y %T GMT", &tm); 3014 strftime(formatted, sizeof formatted, "%a, %d-%b-%Y %T GMT", &tm);
3002 3015
3719 }*/ 3732 }*/
3720 3733
3721 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) { 3734 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) {
3722 struct tm stm = {}; 3735 struct tm stm = {};
3723 char *end = strchr(s, 0); 3736 char *end = strchr(s, 0);
3737 stm.tm_isdst = -1;
3724 3738
3725 if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end) { 3739 if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end) {
3726 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); 3740 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
3727 3741
3728 tzset(); 3742 tzset();