9 /* Convert UTF-8 @string representation of ISO 8601 date to @time.
10 * XXX: Not all ISO formats are supported */
11 isds_error
_isds_datestring2tm(const xmlChar
*string
, struct tm
*time
) {
14 if (!string
|| !time
) return IE_INVAL
;
16 memset(time
, 0, sizeof(*time
));
18 if (sscanf((const char*)string
, "%d-%d-%d%n",
19 &time
->tm_year
, &time
->tm_mon
, &time
->tm_mday
, &tmp
) >= 3
20 && tmp
== strlen((const char*)string
)) {
22 time
->tm_year
-= 1900;
26 memset(time
, 0, sizeof(*time
));
28 if (sscanf((const char*)string
, "%d-%d%n",
29 &time
->tm_year
, &time
->tm_yday
, &tmp
) >= 2
30 && tmp
== strlen((const char*)string
)) {
32 time
->tm_year
-= 1900;
33 _isds_yday2mday(time
);
37 memset(time
, 0, sizeof(*time
));
38 len
= strlen((const char*)string
);
44 ptr
= strdup((const char*)string
);
46 if (sscanf(ptr
+ len
- 2, "%d%n", &time
->tm_mday
, &tmp
) < 1 || tmp
< 2) {
53 if (sscanf(ptr
+ len
- 4, "%d%n", &time
->tm_mon
, &tmp
) < 1 || tmp
< 2) {
60 if (sscanf(ptr
, "%d%n", &time
->tm_year
, &tmp
) < 1 || tmp
< len
- 4) {
67 time
->tm_year
-= 1900;
72 /* MSVCRT gmtime() uses thread-local buffer. This is reentrant. */
73 _hidden
struct tm
*gmtime_r(const time_t *timep
, struct tm
*result
) {
82 memcpy(result
, buf
, sizeof(struct tm
));
86 _hidden
char *strndup(const char *s
, size_t n
) {
91 len
= len
> n
? n
: len
;
92 ret
= malloc((len
+ 1) * sizeof(char));
103 /* Convert UTC broken time to time_t.
104 * @broken_utc it time in UTC in broken format. Despite its content is not
105 * touched, it'sw not-const because underlying POSIX function has non-const
107 * @return (time_t) -1 in case of error */
108 _hidden
time_t _isds_timegm(struct tm
*broken_utc
) {
111 struct tm broken
, *tmp
;
120 tmp
->tm_isdst
= broken_utc
->tm_isdst
;
121 diff
= ret
- mktime(tmp
);
122 memcpy(&broken
, broken_utc
, sizeof(struct tm
));
123 broken
.tm_isdst
= tmp
->tm_isdst
; /* handle broken_utc->tm_isdst < 0 */
124 ret
= mktime(&broken
) + diff
;
128 ssize_t
getline(char **bufptr
, size_t *length
, FILE *fp
) {
132 if (!*bufptr
|| *length
< 1) {
135 *bufptr
= malloc(*length
* sizeof(char));
146 ret
= realloc(*bufptr
, *length
* sizeof(char));
158 ret
= fgets(*bufptr
+ pos
, *length
, fp
);
161 pos
= strlen(*bufptr
);
163 } while (ret
&& (*bufptr
)[pos
- 1] != '\n');
165 return pos
|| ret
? pos
: -1;