comparison src/c/urweb.c @ 1448:37599e85bba8

Fix getting UTC time from formatted strings ( in uw_Basis_readUtc ). Corrects 2 things: - timezones with offsets not an integer number of hours - double correcting for daylight savings time
author Karn Kallio <kkallio@eka>
date Wed, 13 Apr 2011 00:04:41 -0430
parents 17393c5e2b90
children 0982f0242776
comparison
equal deleted inserted replaced
1447:17393c5e2b90 1448:37599e85bba8
3761 }*/ 3761 }*/
3762 3762
3763 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) { 3763 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) {
3764 struct tm stm = {}; 3764 struct tm stm = {};
3765 char *end = strchr(s, 0); 3765 char *end = strchr(s, 0);
3766 stm.tm_isdst = -1;
3767 3766
3768 if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end) { 3767 if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end) {
3769 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); 3768 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
3770 3769
3771 tzset(); 3770 r->seconds = timegm(&stm);
3772 stm.tm_hour -= timezone / (60 * 60);
3773
3774 r->seconds = mktime(&stm);
3775 r->microseconds = 0; 3771 r->microseconds = 0;
3776
3777 localtime_r(&r->seconds, &stm);
3778 if (stm.tm_isdst == 1) {
3779 ++stm.tm_hour;
3780 r->seconds = mktime(&stm);
3781 }
3782 3772
3783 return r; 3773 return r;
3784 } 3774 }
3785 else 3775 else
3786 return NULL; 3776 return NULL;