comparison src/c/urweb.c @ 1374:8cdd25f9cfd0

Next attempt at getting time parsing right
author Adam Chlipala <adam@chlipala.net>
date Wed, 29 Dec 2010 15:58:54 -0500
parents 04bd0d91b74c
children f294173d87db
comparison
equal deleted inserted replaced
1373:04bd0d91b74c 1374:8cdd25f9cfd0
12 #include <limits.h> 12 #include <limits.h>
13 #include <stdint.h> 13 #include <stdint.h>
14 #include <sys/types.h> 14 #include <sys/types.h>
15 #include <sys/socket.h> 15 #include <sys/socket.h>
16 #include <crypt.h> 16 #include <crypt.h>
17 #include <time.h>
17 18
18 #include <pthread.h> 19 #include <pthread.h>
19 20
20 #include "types.h" 21 #include "types.h"
21 22
2692 return NULL; 2693 return NULL;
2693 } 2694 }
2694 2695
2695 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) { 2696 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
2696 char *dot = strchr(s, '.'), *end = strchr(s, 0); 2697 char *dot = strchr(s, '.'), *end = strchr(s, 0);
2697 struct tm stm; 2698 struct tm stm = {};
2698 2699
2699 if (dot) { 2700 if (dot) {
2700 *dot = 0; 2701 *dot = 0;
2701 if (strptime(s, TIME_FMT_PG, &stm) == end) { 2702 if (strptime(s, TIME_FMT_PG, &stm) == end) {
2702 *dot = '.'; 2703 *dot = '.';
2728 } 2729 }
2729 } 2730 }
2730 2731
2731 uw_Basis_time *uw_Basis_stringToTimef(uw_context ctx, const char *fmt, uw_Basis_string s) { 2732 uw_Basis_time *uw_Basis_stringToTimef(uw_context ctx, const char *fmt, uw_Basis_string s) {
2732 char *end = strchr(s, 0); 2733 char *end = strchr(s, 0);
2733 struct tm stm; 2734 struct tm stm = {};
2734 2735
2735 if (strptime(s, fmt, &stm) == end) { 2736 if (strptime(s, fmt, &stm) == end) {
2736 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); 2737 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
2737 r->seconds = mktime(&stm); 2738 r->seconds = mktime(&stm);
2738 r->microseconds = 0; 2739 r->microseconds = 0;
3659 3660
3660 uw_Basis_bool uw_Basis_le_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) { 3661 uw_Basis_bool uw_Basis_le_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
3661 return !!(uw_Basis_eq_time(ctx, t1, t2) || uw_Basis_lt_time(ctx, t1, t2)); 3662 return !!(uw_Basis_eq_time(ctx, t1, t2) || uw_Basis_lt_time(ctx, t1, t2));
3662 } 3663 }
3663 3664
3665 /*uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) {
3666 uw_Basis_time *r = uw_Basis_stringToTime(ctx, s);
3667
3668 printf("timezone = %ld\n", timezone);
3669
3670 if (r)
3671 r->seconds -= timezone;
3672
3673 return r;
3674 }*/
3675
3664 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) { 3676 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) {
3665 struct tm tm, tm2; 3677 struct tm stm = {};
3666 time_t t; 3678
3667 3679 if (strptime(s, TIME_FMT_PG, &stm) || strptime(s, TIME_FMT, &stm)) {
3668 char *other = uw_Basis_strcat(ctx, s, " UTC");
3669 if (strptime(other, TIME_FMT " %Z", &tm) || strptime(other, TIME_FMT_PG " %Z", &tm)) {
3670 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); 3680 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
3671 3681 stm.tm_hour -= timezone / (60 * 60);
3682 r->seconds = mktime(&stm);
3672 r->microseconds = 0; 3683 r->microseconds = 0;
3673 3684
3674 t = mktime(&tm);
3675 localtime_r(&t, &tm2);
3676
3677 tm.tm_sec += tm2.tm_gmtoff;
3678 r->seconds = mktime(&tm);
3679
3680 return r; 3685 return r;
3681 } else 3686 }
3687 else
3682 return NULL; 3688 return NULL;
3683 } 3689 }