00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #define _GNU_SOURCE
00037 #include "config.h"
00038
00039 #include <assert.h>
00040 #include <ctype.h>
00041 #include <stdio.h>
00042 #include <string.h>
00043 #include <time.h>
00044 #include <limits.h>
00045
00046 #include "compat.h"
00047
00048 #include "ksm/ksm.h"
00049 #include "ksm/datetime.h"
00050 #include "ksm/message.h"
00051 #include "ksm/kmedef.h"
00052 #include "ksm/string_util.h"
00053 #include "ksm/string_util2.h"
00054
00055
00056
00057 #define COPY1(src, srcidx, dst, dstidx) \
00058 { \
00059 (dst)[(dstidx)] = (src)[(srcidx)];\
00060 }
00061 #define COPY2(src, srcidx, dst, dstidx) \
00062 { \
00063 COPY1((src), (srcidx), (dst), (dstidx)); \
00064 COPY1((src), (srcidx) + 1, (dst), (dstidx) + 1); \
00065 }
00066 #define COPY3(src, srcidx, dst, dstidx) \
00067 { \
00068 COPY2((src), (srcidx), (dst), (dstidx)); \
00069 COPY1((src), (srcidx) + 2, (dst), (dstidx) + 2); \
00070 }
00071 #define COPY4(src, srcidx, dst, dstidx) \
00072 { \
00073 COPY3((src), (srcidx), (dst), (dstidx)); \
00074 COPY1((src), (srcidx) + 3, (dst), (dstidx) + 3); \
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 int DtNow(struct tm* datetime)
00096 {
00097 time_t curtime;
00098 struct tm *ptr;
00099
00100 #ifdef ENFORCER_TIMESHIFT
00101 char *override;
00102 int status;
00103
00104 override = getenv("ENFORCER_TIMESHIFT");
00105 if (override) {
00106 (void) MsgLog(KME_TIMESHIFT, override);
00107 status = DtParseDateTime(override, datetime);
00108
00109 if (status) {
00110 printf("Couldn't turn \"%s\" into a date, quitting...\n", override);
00111 exit(1);
00112 }
00113
00114 return status;
00115 }
00116 #endif
00117
00118 (void) time(&curtime);
00119 ptr = localtime_r(&curtime, datetime);
00120 return (ptr ? 0 : 1);
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 int DtNumeric(const char* string, struct tm* datetime)
00147 {
00148 int i;
00149 int length;
00150 char buffer[15];
00151 char ebuffer[20];
00152 int status = 0;
00153 char* ptr;
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 length = strlen(string);
00164 if ((length >= 8) && (length <= 14) && ((length % 2) == 0)) {
00165
00166
00167
00168 strlcpy(buffer, string, 15);
00169 for (i = length; i < (int) (sizeof(buffer) - 1); ++i) {
00170 buffer[i] = '0';
00171 }
00172 buffer[sizeof(buffer) - 1] = '\0';
00173
00174
00175
00176 memset(ebuffer, ' ', sizeof(ebuffer));
00177 ebuffer[sizeof(ebuffer) - 1] = '\0';
00178
00179 COPY4(buffer, 0, ebuffer, 0);
00180 COPY2(buffer, 4, ebuffer, 5);
00181 COPY2(buffer, 6, ebuffer, 8);
00182 COPY2(buffer, 8, ebuffer, 11);
00183 COPY2(buffer, 10, ebuffer, 14);
00184 COPY2(buffer, 12, ebuffer, 17);
00185
00186
00187
00188 ptr = strptime(ebuffer, "%Y %m %d %H %M %S", datetime);
00189 status = ptr ? 0 : 1;
00190 }
00191 else {
00192
00193
00194
00195 status = 1;
00196 }
00197
00198 return status;
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 int DtAppendTime(char* fulldt, const char* timepart)
00239 {
00240 int length;
00241 int status = 0;
00242
00243 if (fulldt == NULL) {
00244 return 1;
00245 }
00246
00247 if ((timepart == NULL) || (*timepart == '\0')) {
00248
00249
00250
00251 strcat(fulldt, " 00:00:00");
00252 }
00253 else {
00254 if ((*timepart == ' ') || (*timepart == ':')) {
00255
00256
00257
00258 length = strlen(timepart);
00259
00260
00261
00262
00263
00264
00265
00266 if (length == 3) {
00267 strcat(fulldt, timepart);
00268 strcat(fulldt, ":00:00");
00269 }
00270 else if (length == 6) {
00271 strcat(fulldt, timepart);
00272 strcat(fulldt, ":00");
00273 }
00274 else if (length == 9) {
00275 strcat(fulldt, timepart);
00276 }
00277 else {
00278 status = 1;
00279 }
00280 }
00281 else {
00282
00283
00284
00285 status = 1;
00286 }
00287 }
00288
00289 return status;
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 int DtGeneral(const char* string, struct tm* datetime)
00330 {
00331 int alphadate = 0;
00332 int length;
00333 char copy[32];
00334 char fulldt[32];
00335 char* ptr;
00336 int status = 0;
00337 int timeoff;
00338
00339
00340
00341 if (string == NULL || *string == '\0') {
00342 return 1;
00343 }
00344
00345
00346
00347 if (StrIsDigits(string)) {
00348
00349
00350
00351 status = DtNumeric(string, datetime);
00352 }
00353 else {
00354 length = strlen(string);
00355 if (length >= 9) {
00356
00357
00358
00359
00360
00361
00362
00363 memset(copy, 0, sizeof(copy));
00364 StrStrncpy(copy, string, sizeof(copy));
00365
00366
00367
00368
00369
00370
00371
00372
00373 if ((copy[1] == '-') && (copy[5] == '-')) {
00374 strcpy(fulldt, "0");
00375 strlcat(fulldt + 1, copy, 11);
00376
00377 timeoff = 10;
00378 alphadate = 1;
00379 }
00380 else if ((copy[1] == '-') && (copy[4] == '-')) {
00381 strcpy(fulldt, "0");
00382 strlcat(fulldt + 1, copy, 10);
00383
00384 timeoff = 9;
00385 alphadate = 0;
00386 }
00387 else if ((copy[2] == '-') && (copy[6] == '-')) {
00388 strlcpy(fulldt, copy, 12);
00389
00390 timeoff = 11;
00391 alphadate = 1;
00392 }
00393 else if ((copy[2] == '-') && (copy[5] == '-')) {
00394 strlcpy(fulldt, copy, 11);
00395
00396 timeoff = 10;
00397 alphadate = 0;
00398 }
00399 else if ((copy[4] == '-') && (copy[8] == '-')) {
00400 COPY2(copy, 9, fulldt, 0);
00401 *(fulldt + 2) = '-';
00402 COPY3(copy, 5, fulldt, 3);
00403 *(fulldt + 6) = '-';
00404 COPY4(copy, 0, fulldt, 7);
00405 *(fulldt + 11) = '\0';
00406 timeoff = 11;
00407 alphadate = 1;
00408 }
00409 else if ((copy[4] == '-') && (copy[7] == '-')) {
00410 COPY2(copy, 8, fulldt, 0);
00411 *(fulldt + 2) = '-';
00412 COPY2(copy, 5, fulldt, 3);
00413 *(fulldt + 5) = '-';
00414 COPY4(copy, 0, fulldt, 6);
00415 *(fulldt + 10) = '\0';
00416 timeoff = 10;
00417 alphadate = 0;
00418 }
00419 else {
00420 status = 1;
00421 }
00422
00423 if (status == 0) {
00424
00425
00426 if (copy[timeoff] == ':') {
00427 copy[timeoff] = ' ';
00428 }
00429
00430 status = DtAppendTime(fulldt, ©[timeoff]);
00431 if (status == 0) {
00432 if (alphadate) {
00433 ptr = strptime(fulldt, "%d-%b-%Y %H:%M:%S", datetime);
00434 }
00435 else {
00436 ptr = strptime(fulldt, "%d-%m-%Y %H:%M:%S", datetime);
00437 }
00438 status = ptr ? 0 : 2;
00439 }
00440 }
00441 else {
00442
00443
00444
00445 status = 3;
00446 }
00447 }
00448 else {
00449 status = 3;
00450 }
00451 }
00452
00453 return status;
00454 }
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480 char* DtGeneralString(const char* string)
00481 {
00482 struct tm datetime;
00483 char buffer[KSM_TIME_LENGTH];
00484 char* retval = NULL;
00485 int status;
00486
00487 if (string == NULL) {
00488 return NULL;
00489 }
00490
00491 status = DtGeneral(string, &datetime);
00492 if (status == 0) {
00493 snprintf(buffer, KSM_TIME_LENGTH, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
00494 datetime.tm_year + 1900, datetime.tm_mon + 1, datetime.tm_mday,
00495 datetime.tm_hour, datetime.tm_min, datetime.tm_sec);
00496 retval = StrStrdup(buffer);
00497 }
00498
00499 return retval;
00500 }
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 int DtParseDateTime(const char* string, struct tm* datetime)
00547 {
00548 char* buffer;
00549 int len;
00550 int status = 0;
00551 char* text;
00552
00553
00554
00555 if (string) {
00556
00557
00558
00559 buffer = StrStrdup(string);
00560 StrTrimR(buffer);
00561 text = StrTrimL(buffer);
00562 StrToLower(text);
00563
00564 len = strlen(text);
00565 if (len != 0) {
00566
00567
00568
00569 if (strcmp(text, "now") == 0) {
00570 status = DtNow(datetime);
00571 }
00572 else {
00573 status = DtGeneral(text, datetime);
00574 }
00575 }
00576 else {
00577
00578
00579
00580 status = 1;
00581 }
00582
00583
00584
00585 StrFree(buffer);
00586 }
00587 else {
00588
00589
00590
00591 status = 1;
00592 }
00593
00594 return status;
00595 }
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617 char* DtParseDateTimeString(const char* string)
00618 {
00619 char buffer[KSM_TIME_LENGTH];
00620 struct tm datetime;
00621 char* retval = NULL;
00622 int status;
00623
00624 if (string && *string) {
00625 status = DtParseDateTime(string, &datetime);
00626 if (status == 0) {
00627 snprintf(buffer, KSM_TIME_LENGTH,
00628 "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
00629 datetime.tm_year + 1900, datetime.tm_mon + 1,
00630 datetime.tm_mday, datetime.tm_hour, datetime.tm_min,
00631 datetime.tm_sec);
00632 retval = StrStrdup(buffer);
00633 }
00634 }
00635
00636 return retval;
00637 }
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678 int DtIntervalSeconds(const char* text, int* interval)
00679 {
00680 char number[32];
00681 int status = 0;
00682 int length;
00683 long multiplier = 1;
00684
00685 if (text && interval && *text) {
00686
00687
00688
00689 length = strlen(text);
00690 if (isdigit(text[length - 1])) {
00691 multiplier = 1;
00692 }
00693 else {
00694 switch (text[length - 1]) {
00695 case 's':
00696 multiplier = 1;
00697 break;
00698
00699 case 'm':
00700 multiplier = 60;
00701 break;
00702
00703 case 'h':
00704 multiplier = 60 * 60;
00705 break;
00706
00707 case 'd':
00708 multiplier = 24 * 60 * 60;
00709 break;
00710
00711 case 'w':
00712 multiplier = 7 * 24 * 60 * 60;
00713 break;
00714
00715 default:
00716 status = 1;
00717 }
00718 --length;
00719 }
00720
00721 if (status == 0) {
00722
00723
00724
00725 if (length <= (long) (sizeof(number) - 1)) {
00726 (void) memcpy(number, text, length);
00727 number[length] = '\0';
00728 status = StrStrtoi(number, interval);
00729 if (status == 0) {
00730
00731
00732
00733 *interval *= multiplier;
00734 }
00735 else {
00736 status = 2;
00737 }
00738 }
00739 else {
00740
00741
00742
00743 status = 3;
00744 }
00745 }
00746 }
00747 else {
00748
00749
00750
00751 status = 4;
00752 }
00753
00754 return status;
00755 }
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777 void DtSecondsInterval(int interval, char* text, size_t textlen)
00778 {
00779 char buffer[64];
00780
00781 if (text && (textlen > 0)) {
00782 if (interval != 0) {
00783 if (interval % (60 * 60 * 24 * 7) == 0) {
00784 snprintf(buffer, 64, "%dw", interval / (60 * 60 * 24 * 7));
00785 }
00786 else if (interval % (60 * 60 * 24) == 0) {
00787 snprintf(buffer, 64,"%dd", interval / (60 * 60 * 24));
00788 }
00789 else if (interval % (60 * 60) == 0) {
00790 snprintf(buffer, 64, "%dh", interval / (60 * 60));
00791 }
00792 else if (interval % 60 == 0) {
00793 snprintf(buffer, 64, "%dm", interval / 60);
00794 }
00795 else {
00796 snprintf(buffer, 64, "%ds", interval);
00797 }
00798 }
00799 else {
00800 strcpy(buffer, "0s");
00801 }
00802
00803 StrStrncpy(text, buffer, textlen);
00804 }
00805
00806 return;
00807 }
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828 int DtDateDiff(const char* date1, const char* date2, int* result)
00829 {
00830 static const char* FORMAT = "%Y-%m-%d %H:%M:%S";
00831 char* cstatus;
00832 int status;
00833 struct tm tm1;
00834 struct tm tm2;
00835 time_t t1;
00836 time_t t2;
00837
00838
00839 if (result == NULL) {
00840 return 4;
00841 }
00842
00843 if (date1 && *date1 && date2 && *date2) {
00844
00845
00846
00847 memset(&tm1, 0, sizeof(tm1));
00848 cstatus = strptime(date1, FORMAT, &tm1);
00849 if (cstatus) {
00850 memset(&tm2, 0, sizeof(tm2));
00851 cstatus = strptime(date2, FORMAT, &tm2);
00852 if (cstatus) {
00853
00854
00855
00856
00857
00858
00859 t1 = mktime(&tm1);
00860 t2 = mktime(&tm2);
00861 *result = (int) (t1 - t2);
00862 status = 0;
00863 }
00864 else {
00865 status = 2;
00866 }
00867 }
00868 else {
00869 status = 1;
00870 }
00871 }
00872 else {
00873 status = 3;
00874 }
00875
00876 return status;
00877 }
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928 int DtXMLIntervalSeconds(const char* text, int* interval)
00929 {
00930 int length = 0;
00931 short is_time = 0;
00932 short is_neg = 0;
00933 short warning = 0;
00934 short got_temp = 0;
00935 long long temp = 0;
00936 const char *ptr = text;
00937
00938 int status = 0;
00939
00940 long long temp_interval = 0;
00941
00942 if (text && interval && *text) {
00943 length = strlen(text);
00944 } else {
00945 return(4);
00946 }
00947
00948 if (ptr && length && interval) {
00949 const char *end = text + length;
00950 if (*ptr == '-') {
00951 is_neg = 1;
00952 ptr++;
00953 }
00954 if (*ptr == 'P') {
00955 ptr++;
00956 }
00957 do {
00958 switch (*ptr) {
00959 case 'S':
00960 if (got_temp) {
00961 temp_interval += temp;
00962 temp = 0;
00963 got_temp = 0;
00964 } else {
00965 return(2);
00966 }
00967 break;
00968
00969 case 'M':
00970 if (got_temp) {
00971 if (is_time) {
00972 temp_interval += 60 * temp;
00973 } else {
00974 temp_interval += 31 * 24 * 60 * 60 * temp;
00975 warning = 1;
00976 }
00977 temp = 0;
00978 got_temp = 0;
00979 } else {
00980 return(2);
00981 }
00982 break;
00983
00984 case 'H':
00985 if (got_temp) {
00986 temp_interval += 60 * 60 * temp;
00987 temp = 0;
00988 got_temp = 0;
00989 } else {
00990 return(2);
00991 }
00992 break;
00993
00994 case 'D':
00995 if (got_temp) {
00996 temp_interval += 24 * 60 * 60 * temp;
00997 temp = 0;
00998 got_temp = 0;
00999 } else {
01000 return(2);
01001 }
01002 break;
01003
01004 case 'W':
01005 if (got_temp) {
01006 temp_interval += 7 * 24 * 60 * 60 * temp;
01007 temp = 0;
01008 got_temp = 0;
01009 } else {
01010 return(2);
01011 }
01012 break;
01013
01014 case 'Y':
01015 if (got_temp) {
01016 temp_interval += 365 * 24 * 60 * 60 * temp;
01017 temp = 0;
01018 warning = 1;
01019 got_temp = 0;
01020 } else {
01021 return(2);
01022 }
01023 break;
01024
01025 case 'T':
01026 is_time = 1;
01027 break;
01028
01029 case '0':
01030 case '1':
01031 case '2':
01032 case '3':
01033 case '4':
01034 case '5':
01035 case '6':
01036 case '7':
01037 case '8':
01038 case '9':
01039 if (!temp) {
01040 temp = atoll(ptr);
01041 got_temp = 1;
01042 if ((temp_interval <= INT_MIN) || (temp_interval >= INT_MAX)) {
01043 return(3);
01044 }
01045 }
01046 break;
01047
01048 default:
01049 if (ptr != end) {
01050 return(2);
01051 }
01052 }
01053 } while (ptr++ < end);
01054 }
01055 else {
01056 status = 2;
01057 }
01058
01059
01060 if (temp) {
01061 temp_interval += temp;
01062 temp = 0;
01063 }
01064
01065 if (is_neg == 1) {
01066 temp_interval = 0 - temp_interval;
01067 }
01068
01069 if (warning == 1) {
01070 status = -1;
01071 }
01072
01073 if ((temp_interval >= INT_MIN) && (temp_interval <= INT_MAX)) {
01074 *interval = (int) temp_interval;
01075 }
01076 else {
01077 status = 3;
01078 }
01079
01080 return status;
01081 }