File: char_date.c

package info (click to toggle)
date 1.2.43-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 128 kB
  • sloc: ansic: 125; makefile: 2
file content (135 lines) | stat: -rw-r--r-- 3,291 bytes parent folder | download | duplicates (2)
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
/*
 * Tear down a date string into its component parts
 */

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

char * id(char *str, int array[], int k);

void
char_date(int *n, int *order, char **cdate,
	  int *month, int *day, int *year)
{
    register int i,k, ii;
    register char *j;
    register char *cc;
    
    int what[3];
    int len;
    char tdate[11];

    for (i=0; i< *n; i++) {
	cc = cdate[i];
	for (j=cc; *j != '\0'; j++)  /* upper case to lower case */
	    if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *j) !=NULL)
		*j += 'a' - 'A';

	/*
	** If it is pure numeric, put in some delimiters based on
	**  assumptions
	*/
	len = strlen(cc);
	for (k=0; k<len; k++)
	    if ((cc[k] < '0') || (cc[k] > '9')) break;
	if (k>=len && (len>=5 && len<=8)) {
	    if (len==5)
		snprintf(tdate, 11, "0%c/%c%c/%c%c", 
			 cc[0], cc[1], cc[2], cc[3], cc[4]);
	    else if (len==6)
		snprintf(tdate, 11, "%c%c/%c%c/%c%c", 
			 cc[0], cc[1], cc[2], cc[3], cc[4], cc[5]);
	    else {
		if (len==7) {
		    for (ii=7; ii>0; ii--) cc[ii]= cc[ii-1];
		    cc[0] = '0';
		    }
		if (order[0]==1)
		    snprintf(tdate, 11, "%c%c%c%c/%c%c/%c%c", 
			     cc[0], cc[1], cc[2], cc[3], cc[4], cc[5],
			     cc[6], cc[7]);
		else if (order[1]==1)
		    snprintf(tdate, 11, "%c%c/%c%c%c%c/%c%c",
			     cc[0], cc[1], cc[2], cc[3], cc[4], cc[5],
			     cc[6], cc[7]);
		else
		    snprintf(tdate, 11, "%c%c/%c%c/%c%c%c%c",
			     cc[0], cc[1], cc[2], cc[3], cc[4], cc[5],
			     cc[6], cc[7]);
	    }
	    cc = tdate;
	}

	cc = id(cc, what, 0);
	cc = id(cc, what, 1);
	cc = id(cc, what, 2);
	if (*cc != '\0')  what[2] =0;

	if (what[0] <0) {
	    month[i] = -1*what[0];
	    day[i]   =  what[1];
	    year[i]  =  what[2];
	}
	else if (what[1] <0) {
	    month[i] = -1*what[1];
	    day[i]   =  what[0];
	    year[i]  =  what[2];
	}
	else for (k=0; k<3; k++) {
	    switch (order[k]) {
		case 1: year[i] = what[k]; break;
		case 2: month[i]= what[k]; break;
		case 3: day[i]  = what[k]; break;
	    }
	    }
    }
}


char *
id(char *str, int array[], int k)
{
    register int i;
    register char *j;

    /*skip any delimiters (leading blanks are always ok) */
    while (*str==' ') str++;
    if (k>0)
	if (strchr(" -/,", *str) !=NULL) str++;
    while (*str==' ') str++;

    if (*str=='\0') {
	array[k]=0;
	return(str);
    }

    if (strchr("0123456789", *str) ==NULL) {
	if      (strstr(str, "jan")==str) array[k] = -1;
	else if (strstr(str, "feb")==str) array[k] = -2;
	else if (strstr(str, "mar")==str) array[k] = -3;
	else if (strstr(str, "apr")==str) array[k] = -4;
	else if (strstr(str, "may")==str) array[k] = -5;
	else if (strstr(str, "jun")==str) array[k] = -6;
	else if (strstr(str, "jul")==str) array[k] = -7;
	else if (strstr(str, "aug")==str) array[k] = -8;
	else if (strstr(str, "sep")==str) array[k] = -9;
	else if (strstr(str, "oct")==str) array[k] = -10;
	else if (strstr(str, "nov")==str) array[k] = -11;
	else if (strstr(str, "dec")==str) array[k] = -12;
	else array[k] =0;

	/* pass over the rest of the string */
	while( *str!= '\0' && strchr("januaryfebmrchpilgstovd", *str)!=NULL)
		 str++;
    }
    else { /*is a number */
	i =0;
	while (*str!= '\0' &&  (j=strchr("0123456789", *str)) !=NULL) {
	    str++;
	    i = (10*i) +(*j - '0');
	}
	array[k] = i;
    }

    return(str);
}