author | Michael Krelin <hacker@klever.net> | 2008-06-29 16:08:01 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-06-29 16:11:44 (UTC) |
commit | 12837594b705ad10fdadfd0ba1bfc2249b3b1264 (patch) (unidiff) | |
tree | e0502e678b09668ec59828237ebf5972014d0ce0 | |
parent | 362678728b8232c9490e14ba14ff323d9a92d6be (diff) | |
download | libopkele-12837594b705ad10fdadfd0ba1bfc2249b3b1264.zip libopkele-12837594b705ad10fdadfd0ba1bfc2249b3b1264.tar.gz libopkele-12837594b705ad10fdadfd0ba1bfc2249b3b1264.tar.bz2 |
Fixed w3c to unix timestamp conversion for FreeBSD
Thanks to Göran Löwkrantz for pointing both to the problem and possible
solution.
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | lib/util.cc | 21 |
2 files changed, 19 insertions, 3 deletions
diff --git a/configure.ac b/configure.ac index 3194718..3484146 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -1,28 +1,29 @@ | |||
1 | AC_INIT([libopkele], [2.0], [libopkele-bugs@klever.net]) | 1 | AC_INIT([libopkele], [2.0], [libopkele-bugs@klever.net]) |
2 | AC_CONFIG_SRCDIR([include/opkele/opkele-config.h]) | 2 | AC_CONFIG_SRCDIR([include/opkele/opkele-config.h]) |
3 | AC_CONFIG_HEADERS([config.h include/opkele/acconfig.h]) | 3 | AC_CONFIG_HEADERS([config.h include/opkele/acconfig.h]) |
4 | AM_INIT_AUTOMAKE([dist-bzip2]) | 4 | AM_INIT_AUTOMAKE([dist-bzip2]) |
5 | 5 | ||
6 | AC_PROG_INSTALL | 6 | AC_PROG_INSTALL |
7 | AC_PROG_CXX | 7 | AC_PROG_CXX |
8 | AC_PROG_CC | 8 | AC_PROG_CC |
9 | AC_PROG_LIBTOOL | 9 | AC_PROG_LIBTOOL |
10 | PKG_PROG_PKG_CONFIG | 10 | PKG_PROG_PKG_CONFIG |
11 | 11 | ||
12 | AC_HEADER_STDC | 12 | AC_HEADER_STDC |
13 | AC_CHECK_FUNCS([timegm]) | ||
13 | 14 | ||
14 | AC_PATH_PROG([XSLTPROC],[xsltproc],[true]) | 15 | AC_PATH_PROG([XSLTPROC],[xsltproc],[true]) |
15 | 16 | ||
16 | AC_MSG_CHECKING([for source tree version]) | 17 | AC_MSG_CHECKING([for source tree version]) |
17 | if headrev=$(cd $srcdir && git rev-parse --verify HEAD 2>/dev/null) ; then | 18 | if headrev=$(cd $srcdir && git rev-parse --verify HEAD 2>/dev/null) ; then |
18 | PACKAGE_SRC_VERSION="$(cd $srcdir && git describe --tags $headrev)" | 19 | PACKAGE_SRC_VERSION="$(cd $srcdir && git describe --tags $headrev)" |
19 | test "$PACKAGE_SRC_VERSION" = "$PACKAGE_VERSION" \ | 20 | test "$PACKAGE_SRC_VERSION" = "$PACKAGE_VERSION" \ |
20 | -o "${PACKAGE_SRC_VERSION#${PACKAGE_VERSION}-}" != "$PACKAGE_SRC_VERSION" || PACKAGE_SRC_VERSION="${PACKAGE_VERSION}:${PACKAGE_SRC_VERSION}" | 21 | -o "${PACKAGE_SRC_VERSION#${PACKAGE_VERSION}-}" != "$PACKAGE_SRC_VERSION" || PACKAGE_SRC_VERSION="${PACKAGE_VERSION}:${PACKAGE_SRC_VERSION}" |
21 | ( cd $srcdir && git diff-index $headrev | read dirt ) && PACKAGE_SRC_VERSION="${PACKAGE_SRC_VERSION}-dirty" | 22 | ( cd $srcdir && git diff-index $headrev | read dirt ) && PACKAGE_SRC_VERSION="${PACKAGE_SRC_VERSION}-dirty" |
22 | else | 23 | else |
23 | PACKAGE_SRC_VERSION="$PACKAGE_VERSION" | 24 | PACKAGE_SRC_VERSION="$PACKAGE_VERSION" |
24 | fi | 25 | fi |
25 | AC_MSG_RESULT([$PACKAGE_SRC_VERSION]) | 26 | AC_MSG_RESULT([$PACKAGE_SRC_VERSION]) |
26 | AC_SUBST([PACKAGE_SRC_VERSION]) | 27 | AC_SUBST([PACKAGE_SRC_VERSION]) |
27 | AC_DEFINE_UNQUOTED([PACKAGE_SRC_VERSION],["$PACKAGE_SRC_VERSION"],[more or less precise source tree version]) | 28 | AC_DEFINE_UNQUOTED([PACKAGE_SRC_VERSION],["$PACKAGE_SRC_VERSION"],[more or less precise source tree version]) |
28 | 29 | ||
diff --git a/lib/util.cc b/lib/util.cc index d979502..a46ba2a 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -109,59 +109,74 @@ namespace opkele { | |||
109 | } | 109 | } |
110 | 110 | ||
111 | /* | 111 | /* |
112 | * w3c times | 112 | * w3c times |
113 | */ | 113 | */ |
114 | 114 | ||
115 | string time_to_w3c(time_t t) { | 115 | string time_to_w3c(time_t t) { |
116 | struct tm tm_t; | 116 | struct tm tm_t; |
117 | if(!gmtime_r(&t,&tm_t)) | 117 | if(!gmtime_r(&t,&tm_t)) |
118 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 118 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
119 | char rv[25]; | 119 | char rv[25]; |
120 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 120 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
121 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 121 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
122 | return rv; | 122 | return rv; |
123 | } | 123 | } |
124 | 124 | ||
125 | #ifndef HAVE_TIMEGM | ||
126 | static time_t timegm(struct tm *t) { | ||
127 | char *tz = getenv("TZ"); | ||
128 | setenv("TZ","",1); tzset(); | ||
129 | time_t rv = mktime(t); | ||
130 | if(tz) | ||
131 | setenv("TZ",tz,1); | ||
132 | else | ||
133 | unsetenv("TZ"); | ||
134 | tzset(); | ||
135 | return rv; | ||
136 | } | ||
137 | #define timegm opkele::util::timegm | ||
138 | #endif /* HAVE_TIMEGM */ | ||
139 | |||
125 | time_t w3c_to_time(const string& w) { | 140 | time_t w3c_to_time(const string& w) { |
126 | int fraction; | 141 | int fraction; |
127 | struct tm tm_t; | 142 | struct tm tm_t; |
128 | memset(&tm_t,0,sizeof(tm_t)); | 143 | memset(&tm_t,0,sizeof(tm_t)); |
129 | if( ( | 144 | if( ( |
130 | sscanf( | 145 | sscanf( |
131 | w.c_str(), | 146 | w.c_str(), |
132 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 147 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
133 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 148 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
134 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec | 149 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
135 | ) != 6 | 150 | ) != 6 |
136 | ) && ( | 151 | ) && ( |
137 | sscanf( | 152 | sscanf( |
138 | w.c_str(), | 153 | w.c_str(), |
139 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", | 154 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", |
140 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 155 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
141 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, | 156 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, |
142 | &fraction | 157 | &fraction |
143 | ) != 7 | 158 | ) != 7 |
144 | ) ) | 159 | ) ) |
145 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 160 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
146 | tm_t.tm_mon--; | 161 | tm_t.tm_mon--; |
147 | tm_t.tm_year-=1900; | 162 | tm_t.tm_year-=1900; |
148 | time_t rv = mktime(&tm_t); | 163 | time_t rv = timegm(&tm_t); |
149 | if(rv==(time_t)-1) | 164 | if(rv==(time_t)-1) |
150 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 165 | throw failed_conversion(OPKELE_CP_ "failed to gmtime()"); |
151 | return rv-timezone; | 166 | return rv; |
152 | } | 167 | } |
153 | 168 | ||
154 | /* | 169 | /* |
155 | * | 170 | * |
156 | */ | 171 | */ |
157 | 172 | ||
158 | static inline bool isrfc3986unreserved(int c) { | 173 | static inline bool isrfc3986unreserved(int c) { |
159 | if(c<'-') return false; | 174 | if(c<'-') return false; |
160 | if(c<='.') return true; | 175 | if(c<='.') return true; |
161 | if(c<'0') return false; if(c<='9') return true; | 176 | if(c<'0') return false; if(c<='9') return true; |
162 | if(c<'A') return false; if(c<='Z') return true; | 177 | if(c<'A') return false; if(c<='Z') return true; |
163 | if(c<'_') return false; | 178 | if(c<'_') return false; |
164 | if(c=='_') return true; | 179 | if(c=='_') return true; |
165 | if(c<'a') return false; if(c<='z') return true; | 180 | if(c<'a') return false; if(c<='z') return true; |
166 | if(c=='~') return true; | 181 | if(c=='~') return true; |
167 | return false; | 182 | return false; |