29 struct tm *ltm=gmtime(t);
30 if(ltm==NULL || tm==NULL)
return(NULL);
31 *tm=*ltm; tm->tm_isdst=-1;
38#ifndef HAVE_LOCALTIME_R
53 struct tm *ltm=localtime(t);
54 if(ltm==NULL || tm==NULL)
return(NULL);
55 *tm=*ltm; tm->tm_isdst=-1;
77 if(!tm) temp_lt=0;
else temp_lt=mktime(tm);
78 if(
gmtime_r(&temp_lt, &temp_gm)==NULL)
return((time_t)-1);
79 return(time_t)(temp_lt + (temp_lt - mktime(&temp_gm)));
87 if(!tm) temp_lt=0;
else temp_lt=mktime(tm);
88 if(gmtime_s(&temp_gm, &temp_lt)!=0)
return(time_t)-1);
89 return(time_t)(temp_lt + (temp_lt - mktime(&temp_gm)));
95 if(!tm) temp_lt=0;
else {tm->tm_isdst=-1; temp_lt=mktime(tm);}
96 temp_gm=gmtime(&temp_lt);
if(temp_gm) temp_gm->tm_isdst=-1;
97 return(time_t)(temp_lt + (temp_lt - mktime(temp_gm)));
118 if(buf==NULL)
return(NULL);
122 if(!strftime(buf, 20,
"%Y-%m-%d %H:%M:%S", &tm))
return(NULL);
130static const unsigned short __mon_yday[2][13] = {
132 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
134 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
151 if(strlen(str)<10)
return 1;
152 if(str[4]!=
'-' || str[7]!=
'-')
return 2;
153 if(strncasecmp(str,
"YYYY-MM-DD", 10)==0)
return -1;
154 n=sscanf(str,
"%4d-%2d-%2d", &Y, &M, &D);
if(n!=3)
return 3;
155 if(M>12 || D>31)
return -1;
156 if(Y<0 || M<1 || D<1)
return -1;
176 if(strlen(str)<10)
return 1;
177 if(str[2]!=
'.' && str[2]!=
'/')
return 2;
178 if(str[5]!=
'.' && str[5]!=
'/')
return 2;
179 if(isspace(str[8]))
return 2;
180 if(strncasecmp(str,
"DD.MM.YYYY", 10)==0 ||
181 strncasecmp(str,
"DD/MM/YYYY", 10)==0)
return -1;
182 n=sscanf(str,
"%2d/%2d/%4d", &D, &M, &Y);
183 if(n<3) n=sscanf(str,
"%2d.%2d.%4d", &D, &M, &Y);
185 if(M>12 || D>31)
return -1;
186 if(Y<0 || M<1 || D<1)
return -1;
187 if(intdate!=NULL) sprintf(intdate,
"%04d-%02d-%02d", Y, M, D);
207 if(strlen(str)<8)
return 1;
208 if(str[2]!=
'.' && str[2]!=
'/')
return 2;
209 if(str[5]!=
'.' && str[5]!=
'/')
return 2;
210 if(strncasecmp(str,
"DD.MM.YY", 8)==0 ||
211 strncasecmp(str,
"DD/MM/YY", 8)==0)
return -1;
212 n=sscanf(str,
"%2d/%2d/%2d", &D, &M, &Y);
213 if(n<3) n=sscanf(str,
"%2d.%2d.%2d", &D, &M, &Y);
215 if(Y>99 || M>12 || D>31)
return -1;
216 if(Y<0 || M<1 || D<1)
return -1;
218 if(Y>=70) Y+=1900;
else Y+=2000;
219 sprintf(intdate,
"%04d-%02d-%02d", Y, M, D);
241 if(dateint<1201 || dateint>99991231)
return 1;
242 n=dateint/100; D=dateint-100*n; Y=n/100; M=n-100*Y;
243 if(M>12 || D>31)
return -1;
244 if(Y<1 || M<1 || D<1)
return -1;
245 if(year!=NULL) *year=Y;
246 if(month!=NULL) *month=M;
247 if(day!=NULL) *day=D;
264 if(strlen(str)<8)
return 1;
265 if(str[2]!=
':' || str[5]!=
':')
return 2;
266 if(strncasecmp(str,
"hh:mm:ss", 8)==0)
return -1;
267 n=sscanf(str,
"%d:%d:%d", &h, &m, &s);
if(n!=3)
return 3;
268 if(h<0 || h>23 || m<0 || m>59 || s<0 || s>59)
return -1;
288 int ret1, ret2, len, type=0;
289 char correct_date[20], *time_ptr=NULL;
291 len=strlen(str);
if(len<17)
return 1;
292 if((ret1=
isdate(str))<=0) {
293 type=1; strncpy(correct_date, str, 10); correct_date[10]=(char)0;
294 }
else if((ret1=
isdate2(str, correct_date))<=0) type=2;
295 else if((ret1=
isdate3(str, correct_date))<=0) type=3;
297 if(type==1 || type==2) {
298 if(str[10]!=
' ' && str[10]!=
'\t')
return 3;
301 if(str[8]!=
' ' && str[8]!=
'\t')
return 3;
306 if(ret2>=0)
return(100*ret1+ret2);
else return(100*ret2);
308 if(ret2>0)
return(10*ret2);
309 if(ret1<0) {
if(ret2<0)
return(-3);
else return(-1);}
310 if(ret2<0)
return(-2);
312 sprintf(intdate,
"%10.10s %8.8s", correct_date, time_ptr);
333 int ret, n, YYYY=0, MM=0, DD=0, hh=0, mm=0, ss=0;
335 if(verbose>0) printf(
"get_datetime(%s)\n", str);
336 if(date==NULL)
return(1);
337 ret=
isdatetime(str, buf);
if(ret!=0)
return(ret);
338 n=sscanf(buf,
"%d-%d-%d %d:%d:%d", &YYYY, &MM, &DD, &hh, &mm, &ss);
340 date->tm_year=YYYY-1900; date->tm_mday=DD; date->tm_mon=MM-1;
341 date->tm_hour=hh; date->tm_min=mm; date->tm_sec=ss;
347 printf(
"tm_mday=%d\n", date->tm_mday);
348 printf(
"tm_mon=%d\n", date->tm_mon);
349 printf(
"tm_year=%d\n", date->tm_year);
350 printf(
"tm_hour=%d\n", date->tm_hour);
352 printf(
" tm_hour=%d\n", date->tm_hour);
353 printf(
"time_t=%ld\n", (
long)t);
354 printf(
"time_t_int=%d\n", (
int)t);
357 if(
timegm(date)==-1 && date->tm_year<70) {
358 if(verbose>2) printf(
"tm_mday=%d\n", date->tm_mday);
359 if(verbose>1) printf(
"mktime() fails\n");
362 date->tm_year=70; date->tm_mday=1; date->tm_mon=0;
363 date->tm_hour=1; date->tm_min=0; date->tm_sec=0;
368 if(verbose>1) printf(
" tm_hour=%d\n", date->tm_hour);
386 int ret, n, YYYY=0, MM=0, DD=0;
388 if(strlen(str)<8)
return 1;
389 if((ret=
isdate(str))<=0) {
390 strncpy(buf, str, 10); buf[10]=(char)0;
393 if(ret>0) ret=
isdate3(str, buf);
396 n=sscanf(buf,
"%d-%d-%d", &YYYY, &MM, &DD);
398 date->tm_year=YYYY-1900; date->tm_mday=DD; date->tm_mon=MM-1;
399 date->tm_hour=0; date->tm_min=0; date->tm_sec=0; date->tm_isdst=-1;
400 ret=strftime(buf, 32,
"%Y-%m-%d %H:%M:%S", date);
401 if(ret==0)
return(4);
415 return a/b - (a%b < 0);
427 return (year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0);
441 long int leaps1, leaps2;
445 return(leaps2-leaps1);
461 if(offset==0) {
gmtime_r(&totalsecs, result);
return;}
464 long int days, rem, y, yg;
465 const unsigned short *ip;
467 days=totalsecs/86400; rem=totalsecs%86400; rem+=offset;
468 while(rem<0) {rem+=86400; --days;}
469 while(rem>=86400) {rem-=86400; ++days;}
470 result->tm_hour=rem/3600;
471 rem%=3600; result->tm_min=rem/60;
472 result->tm_sec=rem%60;
475 result->tm_wday=(4+days)%7;
476 if(result->tm_wday<0) result->tm_wday+=7;
478 while(days<0 || days>=(
isleapyear(y)?366:365)) {
485 result->tm_year=y-1900; result->tm_yday=days;
487 for(y=11; days<ip[y]; y--)
continue;
490 result->tm_mday=days+1;
492#if defined(HAVE_TM_GMTOFF)
493 result->tm_gmtoff=0L;
508 return(difftime(mktime(tm1), mktime(tm0)));
int get_datetime(char *str, struct tm *date, int verbose)
char * ctime_r_int(const time_t *t, char *buf)
Convert calendard time t into a null-terminated string of the form YYYY-MM-DD hh:mm:ss,...
int isdate2(char *str, char *intdate)
time_t timegm(struct tm *tm)
Inverse of gmtime, converting struct tm to time_t.
void tmAdd(int s, struct tm *d)
int isleapyear(long int year)
void time_to_tm(time_t totalsecs, int offset, struct tm *result)
int get_date(char *str, struct tm *date)
int isdate4(int dateint, int *year, int *month, int *day)
long int math_div(long int a, long int b)
int isdatetime(char *str, char *intdate)
struct tm * gmtime_r(const time_t *t, struct tm *tm)
Convert time_t to GMT struct tm.
int isdate3(char *str, char *intdate)
long int leaps_between(long int year1, long int year2)
struct tm * localtime_r(const time_t *t, struct tm *tm)
Convert time_t to local time in struct tm.
double tmDifference(struct tm *tm1, struct tm *tm0)
Header file for libtpcmisc.