| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <string.h> |
|---|
| 4 | #include <ctype.h> |
|---|
| 5 | #include <time.h> |
|---|
| 6 | #include <sys/time.h> |
|---|
| 7 | #include "convert.h" |
|---|
| 8 | #include "global.h" |
|---|
| 9 | |
|---|
| 10 | #define SIZE 128 |
|---|
| 11 | static const char *mon[12] = {"JAN", "FEB", "MAR", "APR", "MAY", |
|---|
| 12 | "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; |
|---|
| 13 | |
|---|
| 14 | /* -------------------------------------------------------------------- |
|---|
| 15 | * Function genbank_date(). |
|---|
| 16 | * Convert the date to be in genbank date form. |
|---|
| 17 | */ |
|---|
| 18 | char |
|---|
| 19 | *genbank_date(other_date) |
|---|
| 20 | char *other_date; |
|---|
| 21 | { |
|---|
| 22 | char *gdate, /*token[SIZE],*/ temp[SIZE]; |
|---|
| 23 | int day, month, year; |
|---|
| 24 | int length /*, index=0*/; |
|---|
| 25 | |
|---|
| 26 | length = Lenstr(other_date); |
|---|
| 27 | if(other_date[length-1]=='\n') { |
|---|
| 28 | length--; |
|---|
| 29 | other_date[length]='\0'; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | if(length<=10&&length>=6) { |
|---|
| 33 | find_date(other_date, &month, &day, &year); |
|---|
| 34 | } else if(length>10) { |
|---|
| 35 | if(is_genbank_date(other_date)) { |
|---|
| 36 | if(Lenstr(other_date)>11) |
|---|
| 37 | other_date[11]='\0'; |
|---|
| 38 | gdate = Dupstr(other_date); |
|---|
| 39 | return(gdate); |
|---|
| 40 | } else find_date_long_form(other_date, &month, &day, &year); |
|---|
| 41 | } else { |
|---|
| 42 | sprintf(temp, "Unknown date format: %s, cannot convert.\n", other_date); |
|---|
| 43 | warning(146, temp); |
|---|
| 44 | return(Dupstr("\?\?-\?\?\?-\?\?\?\?")); |
|---|
| 45 | } |
|---|
| 46 | if(day<=0||month<=0||year<=0||day>31||month>12) { |
|---|
| 47 | sprintf(temp, "Wrong date format: %s\n", other_date); |
|---|
| 48 | warning(147, temp); |
|---|
| 49 | return(Dupstr("\?\?-\?\?\?-\?\?\?\?")); |
|---|
| 50 | } |
|---|
| 51 | if(day<10) |
|---|
| 52 | sprintf(temp, "0%d-%s-19%d", day, mon[month-1], year); |
|---|
| 53 | else sprintf(temp, "%d-%s-19%d", day, mon[month-1], year); |
|---|
| 54 | gdate = (char*)Dupstr(temp); |
|---|
| 55 | return(gdate); |
|---|
| 56 | |
|---|
| 57 | } |
|---|
| 58 | /* ----------------------------------------------------------- |
|---|
| 59 | * Function find_date(). |
|---|
| 60 | * Find day, month, year from date string. |
|---|
| 61 | */ |
|---|
| 62 | void |
|---|
| 63 | find_date(date_string, month, day, year) |
|---|
| 64 | char *date_string; |
|---|
| 65 | int *month, *day, *year; |
|---|
| 66 | { |
|---|
| 67 | /* int two_char(), Lenstr(); */ |
|---|
| 68 | int index, indi, count, nums[3]; |
|---|
| 69 | char token[20], determ; |
|---|
| 70 | |
|---|
| 71 | index = count = 0; |
|---|
| 72 | nums[0]=nums[1]=nums[2]=0; |
|---|
| 73 | determ=' '; |
|---|
| 74 | if(two_char(date_string, '.')) determ='.'; |
|---|
| 75 | else if(two_char(date_string, '/')) determ='/'; |
|---|
| 76 | else if(two_char(date_string, '-')) determ='-'; |
|---|
| 77 | if(determ!=' ') { |
|---|
| 78 | for(indi=0; indi<=Lenstr(date_string); indi++) { |
|---|
| 79 | if(date_string[indi]==determ |
|---|
| 80 | ||indi==Lenstr(date_string)) { |
|---|
| 81 | token[index++]='\0'; |
|---|
| 82 | nums[count++] = atoi(token); |
|---|
| 83 | index = 0; |
|---|
| 84 | } else token[index++]=date_string[indi]; |
|---|
| 85 | } |
|---|
| 86 | *month=nums[0]; |
|---|
| 87 | *day=nums[1]; |
|---|
| 88 | *year=(nums[2] % 100); |
|---|
| 89 | return; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | /* ------------------------------------------------------------------ |
|---|
| 93 | * Function two_char(). |
|---|
| 94 | * Return true if string has two determinator char. |
|---|
| 95 | */ |
|---|
| 96 | int |
|---|
| 97 | two_char(string, determ) |
|---|
| 98 | char *string, determ; |
|---|
| 99 | { |
|---|
| 100 | int len, indi; |
|---|
| 101 | |
|---|
| 102 | int count = 0; |
|---|
| 103 | |
|---|
| 104 | for(indi=0, len=Lenstr(string); indi<len; indi++) |
|---|
| 105 | if(string[indi]==determ) count++; |
|---|
| 106 | |
|---|
| 107 | if(count==2) return(1); |
|---|
| 108 | else return(0); |
|---|
| 109 | } |
|---|
| 110 | /* ----------------------------------------------------------------- |
|---|
| 111 | * Function find_date_long_term(). |
|---|
| 112 | * Find day, month, year in the long term date string |
|---|
| 113 | * like day-of-week, month, day, time, year. |
|---|
| 114 | */ |
|---|
| 115 | void |
|---|
| 116 | find_date_long_form(date_string, month, day, year) |
|---|
| 117 | char *date_string; |
|---|
| 118 | int *month, *day, *year; |
|---|
| 119 | { |
|---|
| 120 | int indi, index, length, /*count,*/ nums[3], num; |
|---|
| 121 | char token[SIZE]; |
|---|
| 122 | |
|---|
| 123 | nums[0]=nums[1]=nums[2]=0; |
|---|
| 124 | length = Lenstr(date_string); |
|---|
| 125 | for(indi=0, index=0; index<=length; index++) { |
|---|
| 126 | if(index==length||date_string[index]==' ' |
|---|
| 127 | ||date_string[index]=='\0'||date_string[index]=='\n' |
|---|
| 128 | ||date_string[index]=='\t'||date_string[index]=='(' |
|---|
| 129 | ||date_string[index]==')'||date_string[index]==',') { |
|---|
| 130 | if(indi==0) continue; /* empty token */ |
|---|
| 131 | token[indi] = '\0'; |
|---|
| 132 | if((num=ismonth(token))>0&&nums[0]==0) |
|---|
| 133 | nums[0] = num; |
|---|
| 134 | else if((num=isdatenum(token))>0) { |
|---|
| 135 | if(nums[0]==0&&num<12) nums[0]=num; |
|---|
| 136 | else if(nums[1]==0&&num<31) nums[1]=num; |
|---|
| 137 | else if(nums[2]==0) nums[2]=(num % 100); |
|---|
| 138 | } |
|---|
| 139 | indi=0; |
|---|
| 140 | } else token[indi++]=date_string[index]; |
|---|
| 141 | } /* for loop */ |
|---|
| 142 | *month=nums[0]; |
|---|
| 143 | *day=nums[1]; |
|---|
| 144 | *year=nums[2]; |
|---|
| 145 | return; |
|---|
| 146 | } |
|---|
| 147 | /* -------------------------------------------------------------------- |
|---|
| 148 | * Function ismonth(). |
|---|
| 149 | * Return true if the char string is one of 12 months. |
|---|
| 150 | * Case insensitive. |
|---|
| 151 | */ |
|---|
| 152 | int |
|---|
| 153 | ismonth(string) |
|---|
| 154 | char *string; |
|---|
| 155 | { |
|---|
| 156 | int num=0; |
|---|
| 157 | |
|---|
| 158 | if(Cmpcasestr(string, "JAN")==0) |
|---|
| 159 | num=1; |
|---|
| 160 | else if(Cmpcasestr(string, "FEB")==0) |
|---|
| 161 | num=2; |
|---|
| 162 | else if(Cmpcasestr(string, "MAR")==0) |
|---|
| 163 | num=3; |
|---|
| 164 | else if(Cmpcasestr(string, "APR")==0) |
|---|
| 165 | num=4; |
|---|
| 166 | else if(Cmpcasestr(string, "MAY")==0) |
|---|
| 167 | num=5; |
|---|
| 168 | else if(Cmpcasestr(string, "JUN")==0) |
|---|
| 169 | num=6; |
|---|
| 170 | else if(Cmpcasestr(string, "JUL")==0) |
|---|
| 171 | num=7; |
|---|
| 172 | else if(Cmpcasestr(string, "AUG")==0) |
|---|
| 173 | num=8; |
|---|
| 174 | else if(Cmpcasestr(string, "SEP")==0) |
|---|
| 175 | num=9; |
|---|
| 176 | else if(Cmpcasestr(string, "OCT")==0) |
|---|
| 177 | num=10; |
|---|
| 178 | else if(Cmpcasestr(string, "NOV")==0) |
|---|
| 179 | num=11; |
|---|
| 180 | else if(Cmpcasestr(string, "DEC")==0) |
|---|
| 181 | num=12; |
|---|
| 182 | return(num); |
|---|
| 183 | } |
|---|
| 184 | /* ------------------------------------------------------------------ |
|---|
| 185 | * Function isdatenum(). |
|---|
| 186 | * Return number of day or year the string represents. |
|---|
| 187 | * If not day or year, return 0. |
|---|
| 188 | */ |
|---|
| 189 | int |
|---|
| 190 | isdatenum(string) |
|---|
| 191 | char *string; |
|---|
| 192 | { |
|---|
| 193 | int length, num, indi; |
|---|
| 194 | |
|---|
| 195 | length = Lenstr(string); |
|---|
| 196 | if(length>4||length<1) return(0); |
|---|
| 197 | for(indi=0, num=1; indi<length&&num==1; indi++) { |
|---|
| 198 | if(!isdigit(string[indi])) { |
|---|
| 199 | num = 0; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | if(num == 1) num = atoi(string); |
|---|
| 203 | return(num); |
|---|
| 204 | } |
|---|
| 205 | /* ------------------------------------------------------------------ |
|---|
| 206 | * Function is_genbank_date(). |
|---|
| 207 | * Return true if it is genbank form of date, which is |
|---|
| 208 | * day(2 digits)-MONTH(in letters)-year(4 digits). |
|---|
| 209 | */ |
|---|
| 210 | int |
|---|
| 211 | is_genbank_date(string) |
|---|
| 212 | char *string; |
|---|
| 213 | { |
|---|
| 214 | /* int Lenstr(); */ |
|---|
| 215 | |
|---|
| 216 | if(Lenstr(string)>=11&&string[2]=='-'&&string[6]=='-') return(1); |
|---|
| 217 | else return(0); |
|---|
| 218 | } |
|---|
| 219 | /* ---------------------------------------------------------------- |
|---|
| 220 | * Function today_date(). |
|---|
| 221 | * Get today's date. |
|---|
| 222 | */ |
|---|
| 223 | char |
|---|
| 224 | *today_date() { |
|---|
| 225 | /* struct tm *local_tm, *localtime(); */ |
|---|
| 226 | struct timeval tp; |
|---|
| 227 | struct timezone tzp; |
|---|
| 228 | char line[SIZE], *return_value; |
|---|
| 229 | |
|---|
| 230 | (void)gettimeofday(&tp, &tzp); |
|---|
| 231 | |
|---|
| 232 | Cpystr(line, ctime(&(tp.tv_sec))); |
|---|
| 233 | |
|---|
| 234 | return_value = Dupstr(line); |
|---|
| 235 | |
|---|
| 236 | return(return_value); |
|---|
| 237 | } |
|---|
| 238 | /* --------------------------------------------------------------- |
|---|
| 239 | * Function gcg_date(). |
|---|
| 240 | * Create gcg format of date. |
|---|
| 241 | */ |
|---|
| 242 | char |
|---|
| 243 | *gcg_date(date_string) |
|---|
| 244 | char *date_string; |
|---|
| 245 | { |
|---|
| 246 | static char date[128], temp[128], time[128]; |
|---|
| 247 | int day, year; |
|---|
| 248 | /* char * Catstr(); */ |
|---|
| 249 | |
|---|
| 250 | temp[0]='\0'; |
|---|
| 251 | date_string[7]='\0'; |
|---|
| 252 | if(Cmpstr("Jan", date_string+4)==1) |
|---|
| 253 | Catstr(temp, "January"); |
|---|
| 254 | else if(Cmpstr("Feb", date_string+4)==1) |
|---|
| 255 | Catstr(temp, "Feburary"); |
|---|
| 256 | else if(Cmpstr("Mar", date_string+4)==1) |
|---|
| 257 | Catstr(temp, "March"); |
|---|
| 258 | else if(Cmpstr("Apr", date_string+4)==1) |
|---|
| 259 | Catstr(temp, "April"); |
|---|
| 260 | else if(Cmpstr("May", date_string+4)==1) |
|---|
| 261 | Catstr(temp, "May"); |
|---|
| 262 | else if(Cmpstr("Jun", date_string+4)==1) |
|---|
| 263 | Catstr(temp, "June"); |
|---|
| 264 | else if(Cmpstr("Jul", date_string+4)==1) |
|---|
| 265 | Catstr(temp, "July"); |
|---|
| 266 | else if(Cmpstr("Aug", date_string+4)==1) |
|---|
| 267 | Catstr(temp, "August"); |
|---|
| 268 | else if(Cmpstr("Sep", date_string+4)==1) |
|---|
| 269 | Catstr(temp, "September"); |
|---|
| 270 | else if(Cmpstr("Oct", date_string+4)==1) |
|---|
| 271 | Catstr(temp, "October"); |
|---|
| 272 | else if(Cmpstr("Nov", date_string+4)==1) |
|---|
| 273 | Catstr(temp, "Novemember"); |
|---|
| 274 | else if(Cmpstr("Dec", date_string+4)==1) |
|---|
| 275 | Catstr(temp, "December"); |
|---|
| 276 | |
|---|
| 277 | sscanf(date_string+8, "%d %s %d", &day, time, &year); |
|---|
| 278 | sprintf(date, "%s %d, %d %s", temp, day, year, time); |
|---|
| 279 | |
|---|
| 280 | return(date); |
|---|
| 281 | } |
|---|