55 #define COPY1(src, srcidx, dst, dstidx) \
57 (dst)[(dstidx)] = (src)[(srcidx)];\
59 #define COPY2(src, srcidx, dst, dstidx) \
61 COPY1((src), (srcidx), (dst), (dstidx)); \
62 COPY1((src), (srcidx) + 1, (dst), (dstidx) + 1); \
64 #define COPY3(src, srcidx, dst, dstidx) \
66 COPY2((src), (srcidx), (dst), (dstidx)); \
67 COPY1((src), (srcidx) + 2, (dst), (dstidx) + 2); \
69 #define COPY4(src, srcidx, dst, dstidx) \
71 COPY3((src), (srcidx), (dst), (dstidx)); \
72 COPY1((src), (srcidx) + 3, (dst), (dstidx) + 3); \
98 #ifdef ENFORCER_TIMESHIFT
102 override = getenv(
"ENFORCER_TIMESHIFT");
108 printf(
"Couldn't turn \"%s\" into a date, quitting...\n",
override);
116 (void) time(&curtime);
117 ptr = localtime_r(&curtime, datetime);
118 return (ptr ? 0 : 1);
161 length = strlen(
string);
162 if ((length >= 8) && (length <= 14) && ((length % 2) == 0)) {
166 strlcpy(buffer,
string, 15);
167 for (i = length; i < (int) (
sizeof(buffer) - 1); ++i) {
170 buffer[
sizeof(buffer) - 1] =
'\0';
174 memset(ebuffer,
'-',
sizeof(ebuffer));
175 ebuffer[
sizeof(ebuffer) - 1] =
'\0';
177 COPY4(buffer, 0, ebuffer, 0);
178 COPY2(buffer, 4, ebuffer, 5);
179 COPY2(buffer, 6, ebuffer, 8);
180 COPY2(buffer, 8, ebuffer, 11);
181 COPY2(buffer, 10, ebuffer, 14);
182 COPY2(buffer, 12, ebuffer, 17);
185 ptr = strptime(ebuffer,
"%Y-%m-%d-%H-%M-%S", datetime);
186 status = ptr ? 0 : 1;
240 if (fulldt == NULL) {
244 if ((timepart == NULL) || (*timepart ==
'\0')) {
248 strcat(fulldt,
" 00:00:00");
251 if ((*timepart ==
' ') || (*timepart ==
':')) {
255 length = strlen(timepart);
264 strcat(fulldt, timepart);
265 strcat(fulldt,
":00:00");
267 else if (length == 6) {
268 strcat(fulldt, timepart);
269 strcat(fulldt,
":00");
271 else if (length == 9) {
272 strcat(fulldt, timepart);
338 if (
string == NULL || *
string ==
'\0') {
351 length = strlen(
string);
360 memset(copy, 0,
sizeof(copy));
370 if ((copy[1] ==
'-') && (copy[5] ==
'-')) {
372 strlcat(fulldt + 1, copy, 11);
377 else if ((copy[1] ==
'-') && (copy[4] ==
'-')) {
379 strlcat(fulldt + 1, copy, 10);
384 else if ((copy[2] ==
'-') && (copy[6] ==
'-')) {
385 strlcpy(fulldt, copy, 12);
390 else if ((copy[2] ==
'-') && (copy[5] ==
'-')) {
391 strlcpy(fulldt, copy, 11);
396 else if ((copy[4] ==
'-') && (copy[8] ==
'-')) {
397 COPY2(copy, 9, fulldt, 0);
399 COPY3(copy, 5, fulldt, 3);
401 COPY4(copy, 0, fulldt, 7);
402 *(fulldt + 11) =
'\0';
406 else if ((copy[4] ==
'-') && (copy[7] ==
'-')) {
407 COPY2(copy, 8, fulldt, 0);
409 COPY2(copy, 5, fulldt, 3);
411 COPY4(copy, 0, fulldt, 6);
412 *(fulldt + 10) =
'\0';
423 if (copy[timeoff] ==
':') {
430 ptr = strptime(fulldt,
"%d-%b-%Y %H:%M:%S", datetime);
433 ptr = strptime(fulldt,
"%d-%m-%Y %H:%M:%S", datetime);
435 status = ptr ? 0 : 2;
484 if (
string == NULL) {
490 snprintf(buffer,
KSM_TIME_LENGTH,
"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
491 datetime.tm_year + 1900, datetime.tm_mon + 1, datetime.tm_mday,
492 datetime.tm_hour, datetime.tm_min, datetime.tm_sec);
566 if (strcmp(text,
"now") == 0) {
567 status =
DtNow(datetime);
621 if (
string && *
string) {
625 "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
626 datetime.tm_year + 1900, datetime.tm_mon + 1,
627 datetime.tm_mday, datetime.tm_hour, datetime.tm_min,
682 if (text && interval && *text) {
686 length = strlen(text);
687 if (isdigit(text[length - 1])) {
691 switch (text[length - 1]) {
701 multiplier = 60 * 60;
705 multiplier = 24 * 60 * 60;
709 multiplier = 7 * 24 * 60 * 60;
722 if (length <= (
long) (
sizeof(number) - 1)) {
723 (void) memcpy(number, text, length);
724 number[length] =
'\0';
730 *interval *= multiplier;
778 if (text && (textlen > 0)) {
780 if (interval % (60 * 60 * 24 * 7) == 0) {
781 snprintf(buffer, 64,
"%dw", interval / (60 * 60 * 24 * 7));
783 else if (interval % (60 * 60 * 24) == 0) {
784 snprintf(buffer, 64,
"%dd", interval / (60 * 60 * 24));
786 else if (interval % (60 * 60) == 0) {
787 snprintf(buffer, 64,
"%dh", interval / (60 * 60));
789 else if (interval % 60 == 0) {
790 snprintf(buffer, 64,
"%dm", interval / 60);
793 snprintf(buffer, 64,
"%ds", interval);
797 strcpy(buffer,
"0s");
825 int DtDateDiff(
const char* date1,
const char* date2,
int* result)
827 static const char* FORMAT =
"%Y-%m-%d %H:%M:%S";
836 if (result == NULL) {
840 if (date1 && *date1 && date2 && *date2) {
844 memset(&tm1, 0,
sizeof(tm1));
845 cstatus = strptime(date1, FORMAT, &tm1);
847 memset(&tm2, 0,
sizeof(tm2));
848 cstatus = strptime(date2, FORMAT, &tm2);
858 *result = (int) (t1 - t2);
933 const char *ptr = text;
935 long temp_interval = 0;
937 if (!text || !interval || !*text)
return 4;
938 length = strlen(text);
939 if (length < 2)
return 2;
945 if (*ptr ==
'P') ptr++;
951 if (!got_temp)
return 2;
952 temp_interval += temp;
958 if (!got_temp)
return 2;
960 temp_interval += 60 * temp;
962 temp_interval += 31 * 24 * 60 * 60 * temp;
970 if (!got_temp)
return 2;
971 temp_interval += 60 * 60 * temp;
977 if (!got_temp)
return 2;
978 temp_interval += 24 * 60 * 60 * temp;
984 if (!got_temp)
return 2;
985 temp_interval += 7 * 24 * 60 * 60 * temp;
991 if (!got_temp)
return 2;
992 temp_interval += 365 * 24 * 60 * 60 * temp;
1014 temp = strtol(ptr, &endptr, 10);
1015 if (temp <= INT_MIN || temp >= INT_MAX)
1024 if (ptr != end)
return 2;
1031 if (temp && is_time)
return 2;
1032 temp_interval += temp;
1034 if (is_neg) temp_interval *= -1;
1035 *interval = (int) temp_interval;
1036 return warning?-1:0;
int StrIsDigits(const char *string)
#define COPY3(src, srcidx, dst, dstidx)
int MsgLog(int status,...)
#define COPY4(src, srcidx, dst, dstidx)
char * StrStrdup(const char *string)
int DtGeneral(const char *string, struct tm *datetime)
char * DtParseDateTimeString(const char *string)
void StrStrncpy(char *dest, const char *src, size_t destlen)
char * DtGeneralString(const char *string)
void StrTrimR(char *text)
size_t StrToLower(char *text)
int DtAppendTime(char *fulldt, const char *timepart)
int StrStrtoi(const char *string, int *value)
int DtIntervalSeconds(const char *text, int *interval)
#define COPY2(src, srcidx, dst, dstidx)
char * StrTrimL(char *text)
int DtParseDateTime(const char *string, struct tm *datetime)
int DtNumeric(const char *string, struct tm *datetime)
void DtSecondsInterval(int interval, char *text, size_t textlen)
int DtNow(struct tm *datetime)
int DtXMLIntervalSeconds(const char *text, int *interval)
int DtDateDiff(const char *date1, const char *date2, int *result)