6#include "tpcclibConfig.h"
37 struct tm *ltm=gmtime(t);
38 if(ltm==NULL || tm==NULL)
return(NULL);
39 *tm=*ltm; tm->tm_isdst=-1;
46#ifndef HAVE_LOCALTIME_R
60 struct tm *ltm=localtime(t);
61 if(ltm==NULL || tm==NULL)
return(NULL);
62 *tm=*ltm; tm->tm_isdst=-1;
85 if(!tm) temp_lt=0;
else temp_lt=mktime(tm);
86 if(
gmtime_r(&temp_lt, &temp_gm)==NULL)
return((time_t)-1);
87 return(time_t)(temp_lt + (temp_lt - mktime(&temp_gm)));
95 if(!tm) temp_lt=0;
else temp_lt=mktime(tm);
96 if(gmtime_s(&temp_gm, &temp_lt)!=0)
return(time_t)-1);
97 return(time_t)(temp_lt + (temp_lt - mktime(&temp_gm)));
103 if(!tm) temp_lt=0;
else {tm->tm_isdst=-1; temp_lt=mktime(tm);}
104 temp_gm=gmtime(&temp_lt);
if(temp_gm) temp_gm->tm_isdst=-1;
105 return(time_t)(temp_lt + (temp_lt - mktime(temp_gm)));
126 if(buf==NULL)
return(NULL);
130 if(!strftime(buf, 20,
"%Y-%m-%d %H:%M:%S", &tm))
return(NULL);
138static const unsigned short __mon_yday[2][13] = {
140 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
142 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
159 if(str==NULL ||
strnlen(str, 10)<10)
return 1;
160 if(str[4]!=
'-' || str[7]!=
'-')
return 2;
161 if(strncasecmp(str,
"YYYY-MM-DD", 10)==0)
return -1;
163 n=sscanf(str,
"%4d-%2d-%2d", &Y, &M, &D);
if(n!=3)
return 3;
164 if(M>12 || D>31)
return -1;
165 if(Y<0 || M<1 || D<1)
return -1;
186 if(str==NULL ||
strnlen(str, 10)<8)
return 1;
188 cptr=(
char*)str; n=strcspn(cptr,
"./");
if(n<1 || n>2)
return 2;
189 cptr+=n+1; n=strcspn(cptr,
"./");
if(n<1 || n>2)
return 2;
190 cptr+=n+1; n=strcspn(cptr,
"./- \t\n\r");
if(n!=4)
return 2;
191 if(strncasecmp(str,
"DD.MM.YYYY", 10)==0 || strncasecmp(str,
"DD/MM/YYYY", 10)==0) {
195 n=sscanf(str,
"%d/%d/%4d", &D, &M, &Y);
196 if(n<3) n=sscanf(str,
"%d.%d.%4d", &D, &M, &Y);
198 if(M>12 || D>31)
return -1;
199 if(Y<0 || M<1 || D<1)
return -1;
200 if(intdate!=NULL) sprintf(intdate,
"%04d-%02d-%02d", Y, M, D);
221 if(str==NULL ||
strnlen(str, 8)<6)
return 1;
223 cptr=(
char*)str; n=strcspn(cptr,
"./");
if(n<1 || n>2)
return 2;
224 cptr+=n+1; n=strcspn(cptr,
"./");
if(n<1 || n>2)
return 2;
225 cptr+=n+1; n=strcspn(cptr,
"./- \t\n\r");
if(n!=2)
return 2;
226 if(strncasecmp(str,
"DD.MM.YY", 8)==0 || strncasecmp(str,
"DD/MM/YY", 8)==0) {
230 n=sscanf(str,
"%d/%d/%2d", &D, &M, &Y);
231 if(n<3) n=sscanf(str,
"%d.%d.%2d", &D, &M, &Y);
233 if(Y>99 || M>12 || D>31)
return -1;
234 if(Y<0 || M<1 || D<1)
return -1;
236 if(Y>=70) Y+=1900;
else Y+=2000;
237 sprintf(intdate,
"%04d-%02d-%02d", Y, M, D);
263 if(dateint<18000101 || dateint>99991231)
return 1;
265 n=dateint/100; D=dateint-100*n; Y=n/100; M=n-100*Y;
266 if(M>12 || D>31)
return -1;
267 if(Y<1 || M<1 || D<1)
return -1;
268 if(year!=NULL) *year=Y;
269 if(month!=NULL) *month=M;
270 if(day!=NULL) *day=D;
271 if(intdate!=NULL) {sprintf(intdate,
"%04d-%02d-%02d", Y, M, D);}
288 if(str==NULL ||
strnlen(str, 8)<8)
return 1;
289 if(str[2]!=
':' || str[5]!=
':')
return 2;
290 if(strncasecmp(str,
"hh:mm:ss", 8)==0)
return -1;
292 n=sscanf(str,
"%d:%d:%d", &h, &m, &s);
if(n!=3)
return 3;
293 if(h<0 || h>23 || m<0 || m>59 || s<0 || s>59)
return -1;
317 char correct_date[20], *time_ptr=NULL, tmp[20];
319 if(str==NULL)
return 1;
321 if(
strnlen(str, 19)<15)
return 1;
323 len=strcspn(str,
" \t");
324 if(len<6 || len>10)
return 1;
325 strlcpy(tmp, str, len+1); time_ptr=(
char*)str+len+1;
327 correct_date[0]=(char)0;
335 if(ret1>0) {
if(ret2>=0)
return(100*ret1+ret2);
else return(100*ret2);}
336 if(ret2>0)
return(10*ret2);
337 if(ret1<0) {
if(ret2<0)
return(-3);
else return(-1);}
338 if(ret2<0)
return(-2);
339 if(intdate!=NULL) sprintf(intdate,
"%10.10s %8.8s", correct_date, time_ptr);
359 int ret, n, YYYY=0, MM=0, DD=0, hh=0, mm=0, ss=0;
362 n=sscanf(buf,
"%d-%d-%d %d:%d:%d", &YYYY, &MM, &DD, &hh, &mm, &ss);
364 date->tm_year=YYYY-1900; date->tm_mday=DD; date->tm_mon=MM-1;
365 date->tm_hour=hh; date->tm_min=mm; date->tm_sec=ss; date->tm_isdst=-1;
366 ret=strftime(buf, 32,
"%Y-%m-%d %H:%M:%S", date);
367 if(ret==0)
return(50);
387 int ret, n, YYYY=0, MM=0, DD=0;
389 if(
strnlen(str, 10)<8)
return 1;
397 n=sscanf(buf,
"%d-%d-%d", &YYYY, &MM, &DD);
399 date->tm_year=YYYY-1900; date->tm_mday=DD; date->tm_mon=MM-1;
400 date->tm_hour=0; date->tm_min=0; date->tm_sec=0; date->tm_isdst=-1;
401 ret=strftime(buf, 32,
"%Y-%m-%d %H:%M:%S", date);
402 if(ret==0)
return(4);
410long int math_div(
long int a,
long int b)
412 return a/b - (a%b < 0);
418int isleapyear(
long year)
420 return (year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0);
426long int leaps_between(
long int year1,
long int year2)
428 long int leaps1, leaps2;
430 leaps1=math_div(year1-1, 4) - math_div(year1-1, 100) + math_div(year1-1, 400);
431 leaps2=math_div(year2-1, 4) - math_div(year2-1, 100) + math_div(year2-1, 400);
432 return(leaps2-leaps1);
452 long int days, rem, y, yg;
453 const unsigned short *ip;
455 days=totalsecs/86400; rem=totalsecs%86400; rem+=offset;
456 while(rem<0) {rem+=86400; --days;}
457 while(rem>=86400) {rem-=86400; ++days;}
458 result->tm_hour=rem/3600;
459 rem%=3600; result->tm_min=rem/60;
460 result->tm_sec=rem%60;
463 result->tm_wday=(4+days)%7;
464 if(result->tm_wday<0) result->tm_wday+=7;
466 while(days<0 || days>=(isleapyear(y)?366:365)) {
468 yg=y+math_div(days, 365);
470 days-=(yg-y)*365 + leaps_between(y, yg);
473 result->tm_year=y-1900; result->tm_yday=days;
474 ip=__mon_yday[isleapyear(y)];
475 for(y=11; days<ip[y]; y--)
continue;
476 days-=ip[y]; result->tm_mon=y; result->tm_mday=days+1;
492 return(difftime(mktime(tm1), mktime(tm0)));
525 if(dt1==NULL || dt0==NULL)
return(0.0);
544 if(dt==NULL)
return(1);
549 if(!strftime(dt, 20,
"%Y-%m-%d %H:%M:%S", &tm0))
return(2);
int strDateValid3(const char *str, char *intdate)
char * ctime_r_int(const time_t *t, char *buf)
Convert calendar time t into a null-terminated string of the form YYYY-MM-DD hh:mm:ss,...
int strDateValid(const char *str)
int strDateTimeRead(const char *str, struct tm *date)
int strDateRead(const char *str, struct tm *date)
time_t timegm(struct tm *tm)
Inverse of gmtime, converting struct tm to time_t.
double strDateTimeDifference(const char *dt1, const char *dt0)
void tmAdd(int s, struct tm *d)
int strTimeValid(const char *str)
void time_to_tm(time_t totalsecs, int offset, struct tm *result)
int strDateValid2(const char *str, char *intdate)
struct tm * gmtime_r(const time_t *t, struct tm *tm)
Convert time_t to GMT struct tm.
int strDateTimeValid(const char *str, char *intdate)
int strDateValid4(int dateint, char *intdate, int *year, int *month, int *day)
int strDateTimeAdd(int s, char *dt)
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)
size_t strnlen(const char *s, size_t n)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Header file for library libtpcextensions.