File: dr_time.c

package info (click to toggle)
kamailio 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 56,100 kB
  • sloc: ansic: 552,832; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (1164 lines) | stat: -rw-r--r-- 21,891 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
/*
 * $Id$
 *
 * Copyright (C) 2005-2009 Voice Sistem SRL
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * Kamailio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Kamailio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * History:
 * ---------
 *  2005-02-27  ported to 0.9.0 (bogdan)
 */

#include <stdio.h>
#include <string.h>
#include <time.h>

#include "../../mem/shm_mem.h"
#include "dr_time.h"


/************************ imported from "utils.h"  ***************************/

static inline int strz2int(char *_bp)
{
	int _v;
	char *_p;
	if(!_bp)
		return 0;
	_v = 0;
	_p = _bp;
	while(*_p && *_p>='0' && *_p<='9')
	{
		_v += *_p - '0';
		_p++;
	}
	return _v;
}


static inline char* trim(char* _s)
{
	int len;
	char* end;

	     /* Null pointer, there is nothing to do */
	if (!_s) return _s;

	     /* Remove spaces and tabs from the beginning of string */
	while ((*_s == ' ') || (*_s == '\t')) _s++;

	len = strlen(_s);

        end = _s + len - 1;

	     /* Remove trailing spaces and tabs */
	while ((*end == ' ') || (*end == '\t')) end--;
	if (end != (_s + len - 1)) {
		*(end+1) = '\0';
	}

	return _s;
}




/************************ imported from "ac_tm.c"  ***************************/

/* #define USE_YWEEK_U		// Sunday system
 * #define USE_YWEEK_V		// ISO 8601
 */
#ifndef USE_YWEEK_U
#ifndef USE_YWEEK_V
#ifndef USE_YWEEK_W
#define USE_YWEEK_W		/* Monday system */
#endif
#endif
#endif

#ifdef USE_YWEEK_U
#define SUN_WEEK(t)	(int)(((t)->tm_yday + 7 - \
				((t)->tm_wday)) / 7)
#else
#define MON_WEEK(t)	(int)(((t)->tm_yday + 7 - \
				((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7)
#endif

#define ac_get_wday_yr(t) (int)((t)->tm_yday/7)
#define ac_get_wday_mr(t) (int)(((t)->tm_mday-1)/7)

ac_tm_p ac_tm_new(void)
{
	ac_tm_p _atp = NULL;
	_atp = (ac_tm_p)shm_malloc(sizeof(ac_tm_t));
	if(!_atp)
		return NULL;
	memset(_atp, 0, sizeof(ac_tm_t));
	
	return _atp;
}

int ac_tm_fill(ac_tm_p _atp, struct tm* _tm)
{
	if(!_atp || !_tm)
		return -1;
	_atp->t.tm_sec = _tm->tm_sec;       /* seconds */
	_atp->t.tm_min = _tm->tm_min;       /* minutes */
	_atp->t.tm_hour = _tm->tm_hour;     /* hours */
	_atp->t.tm_mday = _tm->tm_mday;     /* day of the month */
	_atp->t.tm_mon = _tm->tm_mon;       /* month */
	_atp->t.tm_year = _tm->tm_year;     /* year */
	_atp->t.tm_wday = _tm->tm_wday;     /* day of the week */
	_atp->t.tm_yday = _tm->tm_yday;     /* day in the year */
	_atp->t.tm_isdst = _tm->tm_isdst;   /* daylight saving time */
	
	_atp->mweek = ac_get_mweek(_tm);
	_atp->yweek = ac_get_yweek(_tm);
	_atp->ywday = ac_get_wday_yr(_tm);
	_atp->mwday = ac_get_wday_mr(_tm);
	return 0;
}

int ac_tm_set_time(ac_tm_p _atp, time_t _t)
{
	if(!_atp)
		return -1;
	_atp->time = _t;
	return ac_tm_fill(_atp, localtime(&_t));
}

int ac_get_mweek(struct tm* _tm)
{
	if(!_tm)
		return -1;
#ifdef USE_YWEEK_U
	return ((_tm->tm_mday-1)/7 + (7-_tm->tm_wday+(_tm->tm_mday-1)%7)/7);
#else
	return ((_tm->tm_mday-1)/7 + (7-(6+_tm->tm_wday)%7+(_tm->tm_mday-1)%7)/7);
#endif
}

int ac_get_yweek(struct tm* _tm)
{
	int week = -1;
#ifdef USE_YWEEK_V
	int days;
#endif
	
	if(!_tm)
		return -1;
	
#ifdef USE_YWEEK_U
	week = SUN_WEEK(_tm);
#else
	week = MON_WEEK(_tm);
#endif

#ifdef USE_YWEEK_V
	days = ((_tm->tm_yday + 7 - (_tm->tm_wday ? _tm->tm_wday-1 : 6)) % 7);

	if(days >= 4) 
		week++;
	else 
		if(week == 0) 
			week = 53;
#endif
	return week;
}

int ac_get_wkst(void)
{
#ifdef USE_YWEEK_U
	return 0;
#else
	return 1;
#endif
}

int ac_tm_reset(ac_tm_p _atp)
{
	if(!_atp)
		return -1;
	memset(_atp, 0, sizeof(ac_tm_t));
	return 0;
}

int ac_tm_free(ac_tm_p _atp)
{
	if(!_atp)
		return -1;
	if(_atp->mv)
		shm_free(_atp->mv);
	shm_free(_atp);
	return 0;
}

ac_maxval_p ac_get_maxval(ac_tm_p _atp)
{
	struct tm _tm;
	int _v;
	ac_maxval_p _amp = NULL;

	if(!_atp)
		return NULL;
	_amp = (ac_maxval_p)shm_malloc(sizeof(ac_maxval_t));
	if(!_amp)
		return NULL;
	
	/* the number of the days in the year */
	_amp->yday = 365 + is_leap_year(_atp->t.tm_year+1900);

	/* the number of the days in the month */
	switch(_atp->t.tm_mon)
	{
		case 1:
			if(_amp->yday == 366)
				_amp->mday = 29;
			else
				_amp->mday = 28;
		break;
		case 3: case 5: case 8: case 10:
			_amp->mday = 30;
		break;
		default:
			_amp->mday = 31;
	}
	
	/* maximum occurrences of a week day in the year */
	memset(&_tm, 0, sizeof(struct tm));
	_tm.tm_year = _atp->t.tm_year;
	_tm.tm_mon = 11;
	_tm.tm_mday = 31;
	mktime(&_tm);
	_v = 0;
	if(_atp->t.tm_wday > _tm.tm_wday)
		_v = _atp->t.tm_wday - _tm.tm_wday + 1;
	else
		_v = _tm.tm_wday - _atp->t.tm_wday;
	_amp->ywday = (int)((_tm.tm_yday-_v)/7) + 1;
	
	/* maximum number of weeks in the year */
	_amp->yweek = ac_get_yweek(&_tm) + 1;
	
	/* maximum number of the week day in the month */
	_amp->mwday=(int)((_amp->mday-1-(_amp->mday-_atp->t.tm_mday)%7)/7)+1;
	
	/* maximum number of weeks in the month */
	_v = (_atp->t.tm_wday + (_amp->mday - _atp->t.tm_mday)%7)%7;
#ifdef USE_YWEEK_U
	_amp->mweek = (int)((_amp->mday-1)/7+(7-_v+(_amp->mday-1)%7)/7)+1;
#else
	_amp->mweek = (int)((_amp->mday-1)/7+(7-(6+_v)%7+(_amp->mday-1)%7)/7)+1;
#endif

	_atp->mv = _amp;
	return _amp;
}





/************************ imported from "tmrec.c"  ***************************/

#define _D(c) ((c) -'0')

tr_byxxx_p tr_byxxx_new(void)
{
	tr_byxxx_p _bxp = NULL;
	_bxp = (tr_byxxx_p)shm_malloc(sizeof(tr_byxxx_t));
	if(!_bxp)
		return NULL;
	memset(_bxp, 0, sizeof(tr_byxxx_t));
	return _bxp;
}

int tr_byxxx_init(tr_byxxx_p _bxp, int _nr)
{
	if(!_bxp)
		return -1;
	_bxp->nr = _nr;
	_bxp->xxx = (int*)shm_malloc(_nr*sizeof(int));
	if(!_bxp->xxx)
		return -1;
	_bxp->req = (int*)shm_malloc(_nr*sizeof(int));
	if(!_bxp->req)
	{
		shm_free(_bxp->xxx);
		return -1;
	}
	
	memset(_bxp->xxx, 0, _nr*sizeof(int));
	memset(_bxp->req, 0, _nr*sizeof(int));
	
	return 0;
}


int tr_byxxx_free(tr_byxxx_p _bxp)
{
	if(!_bxp)
		return -1;
	if(_bxp->xxx)
		shm_free(_bxp->xxx);
	if(_bxp->req)
		shm_free(_bxp->req);
	shm_free(_bxp);
	return 0;
}

tmrec_p tmrec_new(void)
{
	tmrec_p _trp = NULL;
	_trp = (tmrec_p)shm_malloc(sizeof(tmrec_t));
	if(!_trp)
		return NULL;
	memset(_trp, 0, sizeof(tmrec_t));
	localtime_r(&_trp->dtstart,&(_trp->ts));
	return _trp;
}

int tmrec_free(tmrec_p _trp)
{
	if(!_trp)
		return -1;
	
	tr_byxxx_free(_trp->byday);
	tr_byxxx_free(_trp->bymday);
	tr_byxxx_free(_trp->byyday);
	tr_byxxx_free(_trp->bymonth);
	tr_byxxx_free(_trp->byweekno);

	shm_free(_trp);
	return 0;
}

int tr_parse_dtstart(tmrec_p _trp, char *_in)
{
	if(!_trp || !_in)
		return -1;
	_trp->dtstart = ic_parse_datetime(_in, &(_trp->ts));
	return (_trp->dtstart==0)?-1:0;
}

int tr_parse_dtend(tmrec_p _trp, char *_in)
{
	struct tm _tm;
	if(!_trp || !_in)
		return -1;
	_trp->dtend = ic_parse_datetime(_in,&_tm);
	return (_trp->dtend==0)?-1:0;
}

int tr_parse_duration(tmrec_p _trp, char *_in)
{
	if(!_trp || !_in)
		return -1;
	_trp->duration = ic_parse_duration(_in);
	return 0;
}

int tr_parse_until(tmrec_p _trp, char *_in)
{
	struct tm _tm;
	if(!_trp || !_in)
		return -1;
	_trp->until = ic_parse_datetime(_in, &_tm);
	return 0;
}

int tr_parse_freq(tmrec_p _trp, char *_in)
{
	if(!_trp || !_in)
		return -1;
	if(strlen(_in)<5)
	{
		_trp->freq = FREQ_NOFREQ;
		return 0;
	}
	if(!strcasecmp(_in, "daily"))
	{
		_trp->freq = FREQ_DAILY;
		return 0;
	}
	if(!strcasecmp(_in, "weekly"))
	{
		_trp->freq = FREQ_WEEKLY;
		return 0;
	}
	if(!strcasecmp(_in, "monthly"))
	{
		_trp->freq = FREQ_MONTHLY;
		return 0;
	}
	if(!strcasecmp(_in, "yearly"))
	{
		_trp->freq = FREQ_YEARLY;
		return 0;
	}

	_trp->freq = FREQ_NOFREQ;
	return 0;
}

int tr_parse_interval(tmrec_p _trp, char *_in)
{
	if(!_trp || !_in)
		return -1;
	_trp->interval = strz2int(_in);
	return 0;
}

int tr_parse_byday(tmrec_p _trp, char *_in)
{
	if(!_trp || !_in)
		return -1;
	_trp->byday = ic_parse_byday(_in); 
	return 0;
}

int tr_parse_bymday(tmrec_p _trp, char *_in)
{
	if(!_trp || !_in)
		return -1;
	_trp->bymday = ic_parse_byxxx(_in); 
	return 0;
}

int tr_parse_byyday(tmrec_p _trp, char *_in)
{
	if(!_trp || !_in)
		return -1;
	_trp->byyday = ic_parse_byxxx(_in); 
	return 0;
}

int tr_parse_bymonth(tmrec_p _trp, char *_in)
{
	if(!_trp || !_in)
		return -1;
	_trp->bymonth = ic_parse_byxxx(_in); 
	return 0;
}

int tr_parse_byweekno(tmrec_p _trp, char *_in)
{
	if(!_trp || !_in)
		return -1;
	_trp->byweekno = ic_parse_byxxx(_in); 
	return 0;
}

int tr_parse_wkst(tmrec_p _trp, char *_in)
{
	if(!_trp || !_in)
		return -1;
	_trp->wkst = ic_parse_wkst(_in);
	return 0;
}


time_t ic_parse_datetime(char *_in, struct tm *_tm)
{
	if(!_in || !_tm || strlen(_in)!=15)
		return 0;
	
	memset(_tm, 0, sizeof(struct tm));
	_tm->tm_year = _D(_in[0])*1000 + _D(_in[1])*100 
			+ _D(_in[2])*10 + _D(_in[3]) - 1900;
	_tm->tm_mon = _D(_in[4])*10 + _D(_in[5]) - 1;
	_tm->tm_mday = _D(_in[6])*10 + _D(_in[7]);
	_tm->tm_hour = _D(_in[9])*10 + _D(_in[10]);
	_tm->tm_min = _D(_in[11])*10 + _D(_in[12]);
	_tm->tm_sec = _D(_in[13])*10 + _D(_in[14]);
	_tm->tm_isdst = -1 /*daylight*/;
	return mktime(_tm);
}

time_t ic_parse_duration(char *_in)
{
	time_t _t, _ft;
	char *_p;
	int _fl;
	
	if(!_in || strlen(_in)<2)
		return 0;
	
	if(*_in == 'P' || *_in=='p')
	{
		_p = _in+1;
		_fl = 1;
	} else {
		_p = _in;
		_fl = 0;
	}
	
	_t = _ft = 0;
	
	while(*_p)
	{
		switch(*_p)
		{
			case '0': case '1': case '2':
			case '3': case '4': case '5':
			case '6': case '7': case '8':
			case '9':
				_t = _t*10 + *_p - '0';
			break;
			
			case 'w':
			case 'W':
				if(!_fl)
				{
					LM_ERR("week duration not allowed"
						" here (%d) [%s]\n", (int)(_p-_in), _in);
					return 0;
				}
				_ft += _t*7*24*3600;
				_t = 0;
			break;
			case 'd':
			case 'D':
				if(!_fl)
				{
					LM_ERR("day duration not allowed"
						" here (%d) [%s]\n", (int)(_p-_in), _in);
					return 0;
				}
				_ft += _t*24*3600;
				_t = 0;
			break;
			case 'h':
			case 'H':
				if(_fl)
				{
					LM_ERR("hour duration not allowed"
						" here (%d) [%s]\n", (int)(_p-_in), _in);
					return 0;
				}
				_ft += _t*3600;
				_t = 0;
			break;
			case 'm':
			case 'M':
				if(_fl)
				{
					LM_ERR("minute duration not allowed"
						" here (%d) [%s]\n", (int)(_p-_in), _in);
					return 0;
				}
				_ft += _t*60;
				_t = 0;
			break;
			case 's':
			case 'S':
				if(_fl)
				{
					LM_ERR("second duration not allowed"
						" here (%d) [%s]\n", (int)(_p-_in), _in);
					return 0;
				}
				_ft += _t;
				_t = 0;
			break;
			case 't':
			case 'T':
				if(!_fl)
				{
					LM_ERR("'T' not allowed"
						" here (%d) [%s]\n", (int)(_p-_in), _in);
					return 0;
				}
				_fl = 0;
			break;
			default:
				LM_ERR("bad character here (%d) [%s]\n",
					(int)(_p-_in), _in);
				return 0;
		}
		_p++;
	}

	return _ft;
}

tr_byxxx_p ic_parse_byday(char *_in)
{
	tr_byxxx_p _bxp = NULL;
	int _nr, _s, _v;
	char *_p;

	if(!_in)
		return NULL;
	_bxp = tr_byxxx_new();
	if(!_bxp)
		return NULL;
	_p = _in;
	_nr = 1;
	while(*_p)
	{
		if(*_p == ',')
			_nr++;
		_p++;
	}
	if(tr_byxxx_init(_bxp, _nr) < 0)
	{
		tr_byxxx_free(_bxp);
		return NULL;
	}
	_p = _in;
	_nr = _v = 0;
	_s = 1;
	while(*_p && _nr < _bxp->nr)
	{
		switch(*_p)
		{
			case '0': case '1': case '2':
			case '3': case '4': case '5':
			case '6': case '7': case '8':
			case '9':
				_v = _v*10 + *_p - '0';
			break;
			
			case 's':
			case 'S':
				_p++;
				switch(*_p)
				{
					case 'a':
					case 'A':
						_bxp->xxx[_nr] = WDAY_SA;
						_bxp->req[_nr] = _s*_v;
					break;
					case 'u':
					case 'U':
						_bxp->xxx[_nr] = WDAY_SU;
						_bxp->req[_nr] = _s*_v;
					break;
					default:
						goto error;
				}
				_s = 1;
				_v = 0;
			break;
			case 'm':
			case 'M':
				_p++;
				if(*_p!='o' && *_p!='O')
					goto error;
				_bxp->xxx[_nr] = WDAY_MO;
				_bxp->req[_nr] = _s*_v;
				_s = 1;
				_v = 0;
			break;
			case 't':
			case 'T':
				_p++;
				switch(*_p)
				{
					case 'h':
					case 'H':
						_bxp->xxx[_nr] = WDAY_TH;
						_bxp->req[_nr] = _s*_v;
					break;
					case 'u':
					case 'U':
						_bxp->xxx[_nr] = WDAY_TU;
						_bxp->req[_nr] = _s*_v;
					break;
					default:
						goto error;
				}
				_s = 1;
				_v = 0;
			break;
			case 'w':
			case 'W':
				_p++;
				if(*_p!='e' && *_p!='E')
					goto error;
				_bxp->xxx[_nr] = WDAY_WE;
				_bxp->req[_nr] = _s*_v;
				_s = 1;
				_v = 0;
			break;
			case 'f':
			case 'F':
				_p++;
				if(*_p!='r' && *_p!='R')
					goto error;
				_bxp->xxx[_nr] = WDAY_FR;
				_bxp->req[_nr] = _s*_v;
				_s = 1;
				_v = 0;
			break;
			case '-':
				_s = -1;
			break;
			case '+':
			case ' ':
			case '\t':
			break;
			case ',':
				_nr++;
			break;
			default:
				goto error;
		}
		_p++;
	}

	return _bxp;

error:
	tr_byxxx_free(_bxp);
	return NULL;
}

tr_byxxx_p ic_parse_byxxx(char *_in)
{
	tr_byxxx_p _bxp = NULL;
	int _nr, _s, _v;
	char *_p;

	if(!_in)
		return NULL;
	_bxp = tr_byxxx_new();
	if(!_bxp)
		return NULL;
	_p = _in;
	_nr = 1;
	while(*_p)
	{
		if(*_p == ',')
			_nr++;
		_p++;
	}
	if(tr_byxxx_init(_bxp, _nr) < 0)
	{
		tr_byxxx_free(_bxp);
		return NULL;
	}
	_p = _in;
	_nr = _v = 0;
	_s = 1;
	while(*_p && _nr < _bxp->nr)
	{
		switch(*_p)
		{
			case '0': case '1': case '2':
			case '3': case '4': case '5':
			case '6': case '7': case '8':
			case '9':
				_v = _v*10 + *_p - '0';
			break;
			
			case '-':
				_s = -1;
			break;
			case '+':
			case ' ':
			case '\t':
			break;
			case ',':
				_bxp->xxx[_nr] = _v;
				_bxp->req[_nr] = _s;
				_s = 1;
				_v = 0;
				_nr++;
			break;
			default:
				goto error;
		}
		_p++;
	}
	if(_nr < _bxp->nr)
	{
		_bxp->xxx[_nr] = _v;
		_bxp->req[_nr] = _s;
	}
	return _bxp;

error:
	tr_byxxx_free(_bxp);
	return NULL;
}

int ic_parse_wkst(char *_in)
{
	if(!_in || strlen(_in)!=2)
		goto error;
	
	switch(_in[0])
	{
		case 's':
		case 'S':
			switch(_in[1])
			{
				case 'a':
				case 'A':
					return WDAY_SA;
				case 'u':
				case 'U':
					return WDAY_SU;
				default:
					goto error;
			}
		case 'm':
		case 'M':
			if(_in[1]!='o' && _in[1]!='O')
				goto error;
			return WDAY_MO;
		case 't':
		case 'T':
			switch(_in[1])
			{
				case 'h':
				case 'H':
					return WDAY_TH;
				case 'u':
				case 'U':
					return WDAY_TU;
				default:
					goto error;
			}
		case 'w':
		case 'W':
			if(_in[1]!='e' && _in[1]!='E')
				goto error;
			return WDAY_WE;
		case 'f':
		case 'F':
			if(_in[1]!='r' && _in[1]!='R')
				goto error;
			return WDAY_FR;
		break;
		default:
			goto error;
	}
	
error:
#ifdef USE_YWEEK_U
	return WDAY_SU;
#else
	return WDAY_MO;
#endif
}






/*********************** imported from "checktr.c"  **************************/

#define REC_ERR    -1
#define REC_MATCH   0
#define REC_NOMATCH 1

#define _IS_SET(x) (((x)>0)?1:0)

/*** local headers ***/
int get_min_interval(tmrec_p);
int check_min_unit(tmrec_p, ac_tm_p, tr_res_p);
int check_freq_interval(tmrec_p _trp, ac_tm_p _atp);
int check_byxxx(tmrec_p, ac_tm_p);

/**
 *
 * return 0/REC_MATCH - the time falls in
 *       -1/REC_ERR - error
 *        1/REC_NOMATCH - the time falls out
 */
int check_tmrec(tmrec_p _trp, ac_tm_p _atp, tr_res_p _tsw)
{
	if(!_trp || !_atp)
		return REC_ERR;

	/* it is before start date */
	if(_atp->time < _trp->dtstart)
		return REC_NOMATCH;

	/* no duration or end -> for ever */
	if (!_IS_SET(_trp->duration) && !_IS_SET(_trp->dtend))
		return REC_MATCH;

	/* compute the duration of the recurrence interval */
	if(!_IS_SET(_trp->duration))
		_trp->duration = _trp->dtend - _trp->dtstart;
	
	if(_atp->time <= _trp->dtstart+_trp->duration)
	{
		if(_tsw)
		{
			if(_tsw->flag & TSW_RSET)
			{
				if(_tsw->rest>_trp->dtstart+_trp->duration-_atp->time)
					_tsw->rest = _trp->dtstart+_trp->duration - _atp->time;
			}
			else
			{
				_tsw->flag |= TSW_RSET;
				_tsw->rest = _trp->dtstart+_trp->duration - _atp->time;
			}
		}
		return REC_MATCH;
	}
	
	/* after the bound of recurrence */
	if(_IS_SET(_trp->until) && _atp->time >= _trp->until + _trp->duration)
		return REC_NOMATCH;
	
	/* check if the instance of recurrence matches the 'interval' */
	if(check_freq_interval(_trp, _atp)!=REC_MATCH)
		return REC_NOMATCH;

	if(check_min_unit(_trp, _atp, _tsw)!=REC_MATCH)
		return REC_NOMATCH;

	if(check_byxxx(_trp, _atp)!=REC_MATCH)
		return REC_NOMATCH;

	return REC_MATCH;
}


int check_freq_interval(tmrec_p _trp, ac_tm_p _atp)
{
	int _t0, _t1;
	struct tm _tm;
	if(!_trp || !_atp)
		return REC_ERR;
	
	if(!_IS_SET(_trp->freq))
		return REC_NOMATCH;
	
	if(!_IS_SET(_trp->interval) || _trp->interval==1)
		return REC_MATCH;
	
	switch(_trp->freq)
	{
		case FREQ_DAILY:
		case FREQ_WEEKLY:
			memset(&_tm, 0, sizeof(struct tm));
			_tm.tm_year = _trp->ts.tm_year;
			_tm.tm_mon = _trp->ts.tm_mon;
			_tm.tm_mday = _trp->ts.tm_mday;
			_t0 = (int)mktime(&_tm);
			memset(&_tm, 0, sizeof(struct tm));
			_tm.tm_year = _atp->t.tm_year;
			_tm.tm_mon = _atp->t.tm_mon;
			_tm.tm_mday = _atp->t.tm_mday;
			_t1 = (int)mktime(&_tm);
			if(_trp->freq == FREQ_DAILY)
				return (((_t1-_t0)/(24*3600))%_trp->interval==0)?
					REC_MATCH:REC_NOMATCH;
#ifdef USE_YWEEK_U
			_t0 -= _trp->ts.tm_wday*24*3600;
			_t1 -= _atp->t.tm_wday*24*3600;
#else
			_t0 -= ((_trp->ts.tm_wday+6)%7)*24*3600;
			_t1 -= ((_atp->t.tm_wday+6)%7)*24*3600;
#endif
			return (((_t1-_t0)/(7*24*3600))%_trp->interval==0)?
					REC_MATCH:REC_NOMATCH;
		case FREQ_MONTHLY:
			_t0 = (_atp->t.tm_year-_trp->ts.tm_year)*12
					+ _atp->t.tm_mon-_trp->ts.tm_mon;
			return (_t0%_trp->interval==0)?REC_MATCH:REC_NOMATCH;
		case FREQ_YEARLY:
			return ((_atp->t.tm_year-_trp->ts.tm_year)%_trp->interval==0)?
					REC_MATCH:REC_NOMATCH;
	}
	
	return REC_NOMATCH;
}

int get_min_interval(tmrec_p _trp)
{
	if(!_trp)
		return FREQ_NOFREQ;
	
	if(_trp->freq == FREQ_DAILY || _trp->byday || _trp->bymday || _trp->byyday)
		return FREQ_DAILY;
	if(_trp->freq == FREQ_WEEKLY || _trp->byweekno) 
		return FREQ_WEEKLY;
	if(_trp->freq == FREQ_MONTHLY || _trp->bymonth)
		return FREQ_MONTHLY;
	if(_trp->freq == FREQ_YEARLY)
		return FREQ_YEARLY;
	
	return FREQ_NOFREQ;
}

int check_min_unit(tmrec_p _trp, ac_tm_p _atp, tr_res_p _tsw)
{
	int _v0, _v1;
	if(!_trp || !_atp)
		return REC_ERR;
	switch(get_min_interval(_trp))
	{
		case FREQ_DAILY:
		break;
		case FREQ_WEEKLY:
			if(_trp->ts.tm_wday != _atp->t.tm_wday)
				return REC_NOMATCH;
		break;
		case FREQ_MONTHLY:
			if(_trp->ts.tm_mday != _atp->t.tm_mday)
				return REC_NOMATCH;
		break;
		case FREQ_YEARLY:
			if(_trp->ts.tm_mon != _atp->t.tm_mon 
					|| _trp->ts.tm_mday != _atp->t.tm_mday)
				return REC_NOMATCH;
		break;
		default:
			return REC_NOMATCH;
	}
	_v0 = _trp->ts.tm_hour*3600 + _trp->ts.tm_min*60 + _trp->ts.tm_sec;
	_v1 = _atp->t.tm_hour*3600 + _atp->t.tm_min*60 + _atp->t.tm_sec;
	if(_v1 >= _v0 && _v1 < _v0 + _trp->duration)
	{
		if(_tsw)
		{
			if(_tsw->flag & TSW_RSET)
			{
				if(_tsw->rest>_v0+_trp->duration-_v1)
					_tsw->rest = _v0 + _trp->duration - _v1;
			}
			else
			{
				_tsw->flag |= TSW_RSET;
				_tsw->rest = _v0 + _trp->duration - _v1;
			}
		}
		return REC_MATCH;
	}
	
	return REC_NOMATCH;
}

int check_byxxx(tmrec_p _trp, ac_tm_p _atp)
{
	int i;
	ac_maxval_p _amp = NULL;
	if(!_trp || !_atp)
		return REC_ERR;
	if(!_trp->byday && !_trp->bymday && !_trp->byyday && !_trp->bymonth 
			&& !_trp->byweekno)
		return REC_MATCH;
	
	_amp = ac_get_maxval(_atp);
	if(!_amp)
		return REC_NOMATCH;
	
	if(_trp->bymonth)
	{
		for(i=0; i<_trp->bymonth->nr; i++)
		{
			if(_atp->t.tm_mon == 
					(_trp->bymonth->xxx[i]*_trp->bymonth->req[i]+12)%12)
				break;
		}
		if(i>=_trp->bymonth->nr)
			return REC_NOMATCH;
	}
	if(_trp->freq==FREQ_YEARLY && _trp->byweekno)
	{
		for(i=0; i<_trp->byweekno->nr; i++)
		{
			if(_atp->yweek == (_trp->byweekno->xxx[i]*_trp->byweekno->req[i]+
							_amp->yweek)%_amp->yweek)
				break;
		}
		if(i>=_trp->byweekno->nr)
			return REC_NOMATCH;
	}
	if(_trp->byyday)
	{
		for(i=0; i<_trp->byyday->nr; i++)
		{
			if(_atp->t.tm_yday == (_trp->byyday->xxx[i]*_trp->byyday->req[i]+
						_amp->yday)%_amp->yday)
				break;
		}
		if(i>=_trp->byyday->nr)
			return REC_NOMATCH;
	}
	if(_trp->bymday)
	{
		for(i=0; i<_trp->bymday->nr; i++)
		{
#ifdef EXTRA_DEBUG
			LM_DBG("%d == %d\n", _atp->t.tm_mday,
				(_trp->bymday->xxx[i]*_trp->bymday->req[i]+
				_amp->mday)%_amp->mday + ((_trp->bymday->req[i]<0)?1:0));
#endif
			if(_atp->t.tm_mday == (_trp->bymday->xxx[i]*_trp->bymday->req[i]+
						_amp->mday)%_amp->mday + (_trp->bymday->req[i]<0)?1:0)
				break;
		}
		if(i>=_trp->bymday->nr)
			return REC_NOMATCH;
	}
	if(_trp->byday)
	{
		for(i=0; i<_trp->byday->nr; i++)
		{
			if(_trp->freq==FREQ_YEARLY)
			{
#ifdef EXTRA_DEBUG
				LM_DBG("%d==%d && %d==%d\n", _atp->t.tm_wday,
					_trp->byday->xxx[i], _atp->ywday+1, 
					(_trp->byday->req[i]+_amp->ywday)%_amp->ywday);
#endif
				if(_atp->t.tm_wday == _trp->byday->xxx[i] &&
						_atp->ywday+1 == (_trp->byday->req[i]+_amp->ywday)%
						_amp->ywday)
					break;
			}
			else
			{
				if(_trp->freq==FREQ_MONTHLY)
				{
#ifdef EXTRA_DEBUG
					LM_DBG("%d==%d && %d==%d\n", _atp->t.tm_wday,
						_trp->byday->xxx[i], _atp->mwday+1, 
						(_trp->byday->req[i]+_amp->mwday)%_amp->mwday);
#endif
					if(_atp->t.tm_wday == _trp->byday->xxx[i] &&
							_atp->mwday+1==(_trp->byday->req[i]+
							_amp->mwday)%_amp->mwday)
						break;
				}
				else
				{
					if(_atp->t.tm_wday == _trp->byday->xxx[i])
						break;
				}
			}
		}
		if(i>=_trp->byday->nr)
			return REC_NOMATCH;
	}

	return REC_MATCH;
}