00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022
00023
00024 void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem)
00025 {
00026 int nb, nb_alloc;
00027 unsigned long *tab;
00028
00029 nb = *nb_ptr;
00030 tab = *tab_ptr;
00031 if ((nb & (nb - 1)) == 0) {
00032 if (nb == 0)
00033 nb_alloc = 1;
00034 else
00035 nb_alloc = nb * 2;
00036 tab = av_realloc(tab, nb_alloc * sizeof(unsigned long));
00037 *tab_ptr = tab;
00038 }
00039 tab[nb++] = elem;
00040 *nb_ptr = nb;
00041 }
00042
00043 time_t mktimegm(struct tm *tm)
00044 {
00045 time_t t;
00046
00047 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
00048
00049 if (m < 3) {
00050 m += 12;
00051 y--;
00052 }
00053
00054 t = 86400 *
00055 (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
00056
00057 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
00058
00059 return t;
00060 }
00061
00062 #define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0))
00063 #define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400)
00064
00065
00066
00067 struct tm *brktimegm(time_t secs, struct tm *tm)
00068 {
00069 int days, y, ny, m;
00070 int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
00071
00072 days = secs / 86400;
00073 secs %= 86400;
00074 tm->tm_hour = secs / 3600;
00075 tm->tm_min = (secs % 3600) / 60;
00076 tm->tm_sec = secs % 60;
00077
00078
00079 y = 1970;
00080 while (days >= (ISLEAP(y)?366:365)) {
00081 ny = (y + days/366);
00082 days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1);
00083 y = ny;
00084 }
00085 md[1] = ISLEAP(y)?29:28;
00086 for (m=0; days >= md[m]; m++)
00087 days -= md[m];
00088
00089 tm->tm_year = y;
00090 tm->tm_mon = m+1;
00091 tm->tm_mday = days+1;
00092
00093 return tm;
00094 }
00095
00096
00097
00098 static int date_get_num(const char **pp,
00099 int n_min, int n_max, int len_max)
00100 {
00101 int i, val, c;
00102 const char *p;
00103
00104 p = *pp;
00105 val = 0;
00106 for(i = 0; i < len_max; i++) {
00107 c = *p;
00108 if (!isdigit(c))
00109 break;
00110 val = (val * 10) + c - '0';
00111 p++;
00112 }
00113
00114 if (p == *pp)
00115 return -1;
00116 if (val < n_min || val > n_max)
00117 return -1;
00118 *pp = p;
00119 return val;
00120 }
00121
00122
00123 const char *small_strptime(const char *p, const char *fmt,
00124 struct tm *dt)
00125 {
00126 int c, val;
00127
00128 for(;;) {
00129 c = *fmt++;
00130 if (c == '\0') {
00131 return p;
00132 } else if (c == '%') {
00133 c = *fmt++;
00134 switch(c) {
00135 case 'H':
00136 val = date_get_num(&p, 0, 23, 2);
00137 if (val == -1)
00138 return NULL;
00139 dt->tm_hour = val;
00140 break;
00141 case 'M':
00142 val = date_get_num(&p, 0, 59, 2);
00143 if (val == -1)
00144 return NULL;
00145 dt->tm_min = val;
00146 break;
00147 case 'S':
00148 val = date_get_num(&p, 0, 59, 2);
00149 if (val == -1)
00150 return NULL;
00151 dt->tm_sec = val;
00152 break;
00153 case 'Y':
00154 val = date_get_num(&p, 0, 9999, 4);
00155 if (val == -1)
00156 return NULL;
00157 dt->tm_year = val - 1900;
00158 break;
00159 case 'm':
00160 val = date_get_num(&p, 1, 12, 2);
00161 if (val == -1)
00162 return NULL;
00163 dt->tm_mon = val - 1;
00164 break;
00165 case 'd':
00166 val = date_get_num(&p, 1, 31, 2);
00167 if (val == -1)
00168 return NULL;
00169 dt->tm_mday = val;
00170 break;
00171 case '%':
00172 goto match;
00173 default:
00174 return NULL;
00175 }
00176 } else {
00177 match:
00178 if (c != *p)
00179 return NULL;
00180 p++;
00181 }
00182 }
00183 return p;
00184 }
00185