Mercurial > libavformat.hg
diff cutils.c @ 515:ad72189eec07 libavformat
* replacing calls to not-always-available gmtime_r with our own code.
The new helper function actually differs in semantics from gmtime_r,
so if that seems to be a problem I can actually move it to dv.c
completely, since only DV muxer uses the code anyway.
author | romansh |
---|---|
date | Wed, 18 Aug 2004 08:15:07 +0000 |
parents | 9f4f4ca9f7b5 |
children | da1d5db0ce5c |
line wrap: on
line diff
--- a/cutils.c Sat Aug 14 15:08:09 2004 +0000 +++ b/cutils.c Wed Aug 18 08:15:07 2004 +0000 @@ -147,6 +147,40 @@ return t; } +#define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0)) +#define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400) + +/* this is our own gmtime_r. it differs from its POSIX counterpart in a + couple of places, though. */ +struct tm *brktimegm(time_t secs, struct tm *tm) +{ + int days, y, ny, m; + int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + + days = secs / 86400; + secs %= 86400; + tm->tm_hour = secs / 3600; + tm->tm_min = (secs % 3600) / 60; + tm->tm_sec = secs % 60; + + /* oh well, may be someone some day will invent a formula for this stuff */ + y = 1970; /* start "guessing" */ + while (days >= (ISLEAP(y)?366:365)) { + ny = (y + days/366); + days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1); + y = ny; + } + md[1] = ISLEAP(y)?29:28; + for (m=0; days >= md[m]; m++) + days -= md[m]; + + tm->tm_year = y; /* unlike gmtime_r we store complete year here */ + tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */ + tm->tm_mday = days+1; + + return tm; +} + /* get a positive number between n_min and n_max, for a maximum length of len_max. Return -1 if error. */ static int date_get_num(const char **pp,