9 static void yday2mday(struct tm
*time
) {
10 static int mtab
[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
11 int i
, year
= 1900 + time
->tm_year
;
13 mtab
[1] = (!(year
% 4) && ((year
% 100) || !(year
%400))) ? 29 : 28;
14 time
->tm_mday
= time
->tm_yday
+ 1;
16 for (i
= 0; i
< 12; i
++) {
17 if (time
->tm_mday
> mtab
[i
]) {
18 time
->tm_mday
-= mtab
[i
];
27 /* Convert UTF-8 @string representation of ISO 8601 date to @time.
28 * XXX: Not all ISO formats are supported */
29 isds_error
_isds_datestring2tm(const xmlChar
*string
, struct tm
*time
) {
32 if (!string
|| !time
) return IE_INVAL
;
34 memset(time
, 0, sizeof(*time
));
36 if (sscanf((const char*)string
, "%d-%d-%d%n",
37 &time
->tm_year
, &time
->tm_mon
, &time
->tm_mday
, &tmp
) >= 3
38 && tmp
== strlen((const char*)string
)) {
40 time
->tm_year
-= 1900;
44 memset(time
, 0, sizeof(*time
));
46 if (sscanf((const char*)string
, "%d-%d%n",
47 &time
->tm_year
, &time
->tm_yday
, &tmp
) >= 2
48 && tmp
== strlen((const char*)string
)) {
50 time
->tm_year
-= 1900;
55 memset(time
, 0, sizeof(*time
));
56 len
= strlen((const char*)string
);
62 ptr
= strdup((const char*)string
);
64 if (sscanf(ptr
+ len
- 2, "%d%n", &time
->tm_mday
, &tmp
) < 1 || tmp
< 2) {
71 if (sscanf(ptr
+ len
- 4, "%d%n", &time
->tm_mon
, &tmp
) < 1 || tmp
< 2) {
78 if (sscanf(ptr
, "%d%n", &time
->tm_year
, &tmp
) < 1 || tmp
< len
- 4) {
85 time
->tm_year
-= 1900;
90 /* MSVCRT gmtime() uses thread-local buffer. This is reentrant. */
91 _hidden
struct tm
*gmtime_r(const time_t *timep
, struct tm
*result
) {
100 memcpy(result
, buf
, sizeof(struct tm
));
104 _hidden
char *strndup(const char *s
, size_t n
) {
109 len
= len
> n
? n
: len
;
110 ret
= malloc((len
+ 1) * sizeof(char));
116 strncpy(ret
, s
, len
);
121 /* Convert UTC broken time to time_t.
122 * @broken_utc it time in UTC in broken format. Despite its content is not
123 * touched, it'sw not-const because underlying POSIX function has non-const
125 * @return (time_t) -1 in case of error */
126 _hidden
time_t _isds_timegm(struct tm
*broken_utc
) {
129 struct tm broken
, *tmp
;
138 tmp
->tm_isdst
= broken_utc
->tm_isdst
;
139 diff
= ret
- mktime(tmp
);
140 memcpy(&broken
, broken_utc
, sizeof(struct tm
));
141 broken
.tm_isdst
= tmp
->tm_isdst
; /* handle broken_utc->tm_isdst < 0 */
142 ret
= mktime(&broken
) + diff
;
146 ssize_t
getline(char **bufptr
, size_t *length
, FILE *fp
) {
150 if (!*bufptr
|| *length
< 1) {
153 *bufptr
= malloc(*length
* sizeof(char));
164 ret
= realloc(*bufptr
, *length
* sizeof(char));
176 ret
= fgets(*bufptr
+ pos
, *length
, fp
);
179 pos
= strlen(*bufptr
);
181 } while (ret
&& (*bufptr
)[pos
- 1] != '\n');
183 return pos
|| ret
? pos
: -1;