57 #define COPY1(src, srcidx, dst, dstidx) \
59 (dst)[(dstidx)] = (src)[(srcidx)];\
61 #define COPY2(src, srcidx, dst, dstidx) \
63 COPY1((src), (srcidx), (dst), (dstidx)); \
64 COPY1((src), (srcidx) + 1, (dst), (dstidx) + 1); \
66 #define COPY3(src, srcidx, dst, dstidx) \
68 COPY2((src), (srcidx), (dst), (dstidx)); \
69 COPY1((src), (srcidx) + 2, (dst), (dstidx) + 2); \
71 #define COPY4(src, srcidx, dst, dstidx) \
73 COPY3((src), (srcidx), (dst), (dstidx)); \
74 COPY1((src), (srcidx) + 3, (dst), (dstidx) + 3); \
100 #ifdef ENFORCER_TIMESHIFT
104 override = getenv(
"ENFORCER_TIMESHIFT");
110 printf(
"Couldn't turn \"%s\" into a date, quitting...\n",
override);
118 (void) time(&curtime);
119 ptr = localtime_r(&curtime, datetime);
120 return (ptr ? 0 : 1);
163 length = strlen(
string);
164 if ((length >= 8) && (length <= 14) && ((length % 2) == 0)) {
168 strlcpy(buffer,
string, 15);
169 for (i = length; i < (int) (
sizeof(buffer) - 1); ++i) {
172 buffer[
sizeof(buffer) - 1] =
'\0';
176 memset(ebuffer,
' ',
sizeof(ebuffer));
177 ebuffer[
sizeof(ebuffer) - 1] =
'\0';
179 COPY4(buffer, 0, ebuffer, 0);
180 COPY2(buffer, 4, ebuffer, 5);
181 COPY2(buffer, 6, ebuffer, 8);
182 COPY2(buffer, 8, ebuffer, 11);
183 COPY2(buffer, 10, ebuffer, 14);
184 COPY2(buffer, 12, ebuffer, 17);
188 ptr = strptime(ebuffer,
"%Y %m %d %H %M %S", datetime);
189 status = ptr ? 0 : 1;
243 if (fulldt == NULL) {
247 if ((timepart == NULL) || (*timepart ==
'\0')) {
251 strcat(fulldt,
" 00:00:00");
254 if ((*timepart ==
' ') || (*timepart ==
':')) {
258 length = strlen(timepart);
267 strcat(fulldt, timepart);
268 strcat(fulldt,
":00:00");
270 else if (length == 6) {
271 strcat(fulldt, timepart);
272 strcat(fulldt,
":00");
274 else if (length == 9) {
275 strcat(fulldt, timepart);
341 if (
string == NULL || *
string ==
'\0') {
354 length = strlen(
string);
363 memset(copy, 0,
sizeof(copy));
373 if ((copy[1] ==
'-') && (copy[5] ==
'-')) {
375 strlcat(fulldt + 1, copy, 11);
380 else if ((copy[1] ==
'-') && (copy[4] ==
'-')) {
382 strlcat(fulldt + 1, copy, 10);
387 else if ((copy[2] ==
'-') && (copy[6] ==
'-')) {
388 strlcpy(fulldt, copy, 12);
393 else if ((copy[2] ==
'-') && (copy[5] ==
'-')) {
394 strlcpy(fulldt, copy, 11);
399 else if ((copy[4] ==
'-') && (copy[8] ==
'-')) {
400 COPY2(copy, 9, fulldt, 0);
402 COPY3(copy, 5, fulldt, 3);
404 COPY4(copy, 0, fulldt, 7);
405 *(fulldt + 11) =
'\0';
409 else if ((copy[4] ==
'-') && (copy[7] ==
'-')) {
410 COPY2(copy, 8, fulldt, 0);
412 COPY2(copy, 5, fulldt, 3);
414 COPY4(copy, 0, fulldt, 6);
415 *(fulldt + 10) =
'\0';
426 if (copy[timeoff] ==
':') {
433 ptr = strptime(fulldt,
"%d-%b-%Y %H:%M:%S", datetime);
436 ptr = strptime(fulldt,
"%d-%m-%Y %H:%M:%S", datetime);
438 status = ptr ? 0 : 2;
487 if (
string == NULL) {
493 snprintf(buffer,
KSM_TIME_LENGTH,
"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
494 datetime.tm_year + 1900, datetime.tm_mon + 1, datetime.tm_mday,
495 datetime.tm_hour, datetime.tm_min, datetime.tm_sec);
569 if (strcmp(text,
"now") == 0) {
570 status =
DtNow(datetime);
624 if (
string && *
string) {
628 "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
629 datetime.tm_year + 1900, datetime.tm_mon + 1,
630 datetime.tm_mday, datetime.tm_hour, datetime.tm_min,
685 if (text && interval && *text) {
689 length = strlen(text);
690 if (isdigit(text[length - 1])) {
694 switch (text[length - 1]) {
704 multiplier = 60 * 60;
708 multiplier = 24 * 60 * 60;
712 multiplier = 7 * 24 * 60 * 60;
725 if (length <= (
long) (
sizeof(number) - 1)) {
726 (void) memcpy(number, text, length);
727 number[length] =
'\0';
733 *interval *= multiplier;
781 if (text && (textlen > 0)) {
783 if (interval % (60 * 60 * 24 * 7) == 0) {
784 snprintf(buffer, 64,
"%dw", interval / (60 * 60 * 24 * 7));
786 else if (interval % (60 * 60 * 24) == 0) {
787 snprintf(buffer, 64,
"%dd", interval / (60 * 60 * 24));
789 else if (interval % (60 * 60) == 0) {
790 snprintf(buffer, 64,
"%dh", interval / (60 * 60));
792 else if (interval % 60 == 0) {
793 snprintf(buffer, 64,
"%dm", interval / 60);
796 snprintf(buffer, 64,
"%ds", interval);
800 strcpy(buffer,
"0s");
828 int DtDateDiff(
const char* date1,
const char* date2,
int* result)
830 static const char* FORMAT =
"%Y-%m-%d %H:%M:%S";
839 if (result == NULL) {
843 if (date1 && *date1 && date2 && *date2) {
847 memset(&tm1, 0,
sizeof(tm1));
848 cstatus = strptime(date1, FORMAT, &tm1);
850 memset(&tm2, 0,
sizeof(tm2));
851 cstatus = strptime(date2, FORMAT, &tm2);
861 *result = (int) (t1 - t2);
936 const char *ptr = text;
940 long long temp_interval = 0;
942 if (text && interval && *text) {
943 length = strlen(text);
948 if (ptr && length && interval) {
949 const char *end = text + length;
961 temp_interval += temp;
972 temp_interval += 60 * temp;
974 temp_interval += 31 * 24 * 60 * 60 * temp;
986 temp_interval += 60 * 60 * temp;
996 temp_interval += 24 * 60 * 60 * temp;
1006 temp_interval += 7 * 24 * 60 * 60 * temp;
1016 temp_interval += 365 * 24 * 60 * 60 * temp;
1042 if ((temp_interval <= INT_MIN) || (temp_interval >= INT_MAX)) {
1053 }
while (ptr++ < end);
1061 temp_interval += temp;
1066 temp_interval = 0 - temp_interval;
1073 if ((temp_interval >= INT_MIN) && (temp_interval <= INT_MAX)) {
1074 *interval = (int) temp_interval;