| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) 2013 Adrien Devresse <[email protected]>, CERN |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <davix_internal_config.hpp> |
| 22 |
#include "datetime_utils.hpp" |
| 23 |
|
| 24 |
time_t parse_http_date(const char* http_date){ |
| 25 |
// TODO : possible issue with timezone != GMT, |
| 26 |
|
| 27 |
static const char rfc1123[] = "%a, %d %b %Y %H:%M:%S GMT"; |
| 28 |
struct tm tm; |
| 29 |
memset(&tm,0, sizeof(struct tm)); |
| 30 |
|
| 31 |
const char * p = strptime(http_date, rfc1123, &tm); |
| 32 |
if ( p == NULL || *p != '\0'){ |
| 33 |
return -1; |
| 34 |
} |
| 35 |
return timegm(&tm); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
time_t parse_iso8601date(const char* iso_date){ |
| 41 |
|
| 42 |
// TODO : refactor to a regex parser.. ISO8601 b******* is impossible to parse correctly with strptime |
| 43 |
struct tm tm_time; |
| 44 |
char* p, *end_p; |
| 45 |
memset(&tm_time,0, sizeof(struct tm )); |
| 46 |
if( ( (p = strptime(iso_date, "%Y-%m-%dT%H:%M:%SZ", &tm_time) ) == NULL || *p != '\0') |
| 47 |
&& ( (p = strptime(iso_date, "%Y-%m-%dT%H:%M:%S", &tm_time) ) == NULL || *p != '.' |
| 48 |
|| *(iso_date + strlen(iso_date) -1) != 'Z' )){ |
| 49 |
if( (p = strptime(iso_date, "%Y-%m-%dT%H:%M:%S", &tm_time) ) != NULL |
| 50 |
&& (*p == '+' || *p == '-') ){ |
| 51 |
struct tm tm_time_offset; |
| 52 |
memset(&tm_time_offset,0, sizeof(struct tm )); |
| 53 |
if( ( ( end_p= strptime(p+1, "%H:%M", &tm_time_offset)) == NULL |
| 54 |
|| *end_p != '\0' |
| 55 |
) |
| 56 |
&& ( (end_p =strptime(p+1, "%H%M", &tm_time_offset)) == NULL |
| 57 |
|| *end_p != '\0' |
| 58 |
) |
| 59 |
) |
| 60 |
return -1; |
| 61 |
if( *p =='+'){ |
| 62 |
tm_time.tm_hour += tm_time_offset.tm_hour; |
| 63 |
tm_time.tm_min += tm_time_offset.tm_min; |
| 64 |
} else{ |
| 65 |
tm_time.tm_hour -= tm_time_offset.tm_hour; |
| 66 |
tm_time.tm_min -= tm_time_offset.tm_min; |
| 67 |
} |
| 68 |
}else{ |
| 69 |
return -1; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
return timegm(&tm_time); |
| 75 |
} |
| 76 |
|
| 77 |
time_t parse_standard_date(const char* http_date){ |
| 78 |
if(strchr(http_date, ',') != NULL){ // detect if rfc date or iso8601 date |
| 79 |
return parse_http_date(http_date); |
| 80 |
}else{ |
| 81 |
return parse_iso8601date(http_date); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
std::string Davix::current_time(const std::string format) { |
| 86 |
return Davix::time_as_string(time(NULL), format); |
| 87 |
} |
| 88 |
|
| 89 |
std::string Davix::time_as_string(const time_t t, const std::string format) { |
| 90 |
struct tm utc; |
| 91 |
char date[255]; |
| 92 |
|
| 93 |
date[254] = '\0'; |
| 94 |
#ifdef HAVE_GMTIME_R |
| 95 |
gmtime_r(&t, &utc); |
| 96 |
#else |
| 97 |
struct tm* p_utc = gmtime(&t); |
| 98 |
memcpy(&utc_current, p_utc, sizeof(struct tm)); |
| 99 |
#endif |
| 100 |
strftime(date, 254, format.c_str(), &utc); |
| 101 |
return std::string(date); |
| 102 |
} |
| 103 |
|