| 1 | #include <stdio.h> |
|---|
| 2 | #include <string.h> |
|---|
| 3 | #include <ctype.h> |
|---|
| 4 | #include <time.h> |
|---|
| 5 | #include "convert.h" |
|---|
| 6 | #include "global.h" |
|---|
| 7 | |
|---|
| 8 | #define SIZE 128 |
|---|
| 9 | static char *mon[12] = {"JAN", "FEB", "MAR", "APR", "MAY", |
|---|
| 10 | "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; |
|---|
| 11 | |
|---|
| 12 | /* -------------------------------------------------------------------- */ |
|---|
| 13 | /* Function genbank_date(). |
|---|
| 14 | /* Convert the date to be in genbank date form. |
|---|
| 15 | */ |
|---|
| 16 | char |
|---|
| 17 | *genbank_date(other_date) |
|---|
| 18 | char *other_date; |
|---|
| 19 | { |
|---|
| 20 | char *gdate, token[SIZE], temp[SIZE], *Dupstr(); |
|---|
| 21 | int day, month, year; |
|---|
| 22 | int length, Lenstr(), index=0, is_genbank_date(); |
|---|
| 23 | void find_date(), find_date_long_form(), warning(); |
|---|
| 24 | |
|---|
| 25 | length = Lenstr(other_date); |
|---|
| 26 | if(other_date[length-1]=='\n') { |
|---|
| 27 | length--; |
|---|
| 28 | other_date[length]='\0'; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | if(length<=10&&length>=6) { |
|---|
| 32 | find_date(other_date, &month, &day, &year); |
|---|
| 33 | } else if(length>10) { |
|---|
| 34 | if(is_genbank_date(other_date)) { |
|---|
| 35 | if(Lenstr(other_date)>11) |
|---|
| 36 | other_date[11]='\0'; |
|---|
| 37 | gdate = Dupstr(other_date); |
|---|
| 38 | return(gdate); |
|---|
| 39 | } else find_date_long_form(other_date, &month, &day, &year); |
|---|
| 40 | } else { |
|---|
| 41 | sprintf(temp, "Unknown date format: %s, cannot convert.\n", other_date); |
|---|
| 42 | warning(149, temp); |
|---|
| 43 | return(Dupstr("??-???-????")); |
|---|
| 44 | } |
|---|
| 45 | if(day<=0||month<=0||year<=0||day>31||month>12) { |
|---|
| 46 | sprintf(temp, "Wrong date format: %s\n", other_date); |
|---|
| 47 | warning(150, temp); |
|---|
| 48 | return(Dupstr("??-???-????")); |
|---|
| 49 | } |
|---|
| 50 | if(day<10) |
|---|
| 51 | sprintf(temp, "0%d-%s-19%d", day, mon[month-1], year); |
|---|
| 52 | else sprintf(temp, "%d-%s-19%d", day, mon[month-1], year); |
|---|
| 53 | gdate = (char*)Dupstr(temp); |
|---|
| 54 | return(gdate); |
|---|
| 55 | |
|---|
| 56 | } |
|---|
| 57 | /* ----------------------------------------------------------- */ |
|---|
| 58 | /* Function find_date(). |
|---|
| 59 | /* Find day, month, year from date string. |
|---|
| 60 | */ |
|---|
| 61 | void |
|---|
| 62 | find_date(date_string, month, day, year) |
|---|
| 63 | char *date_string; |
|---|
| 64 | int *month, *day, *year; |
|---|
| 65 | { |
|---|
| 66 | int two_char(), Lenstr(); |
|---|
| 67 | int index, indi, count, nums[3]; |
|---|
| 68 | char token[20], determ; |
|---|
| 69 | |
|---|
| 70 | index = count = 0; |
|---|
| 71 | nums[0]=nums[1]=nums[2]=0; |
|---|
| 72 | determ=' '; |
|---|
| 73 | if(two_char(date_string, '.')) determ='.'; |
|---|
| 74 | else if(two_char(date_string, '/')) determ='/'; |
|---|
| 75 | else if(two_char(date_string, '-')) determ='-'; |
|---|
| 76 | if(determ!=' ') { |
|---|
| 77 | for(indi=0; indi<=Lenstr(date_string); indi++) { |
|---|
| 78 | if(date_string[indi]==determ |
|---|
| 79 | ||indi==Lenstr(date_string)) { |
|---|
| 80 | token[index++]='\0'; |
|---|
| 81 | nums[count++] = atoi(token); |
|---|
| 82 | index = 0; |
|---|
| 83 | } else token[index++]=date_string[indi]; |
|---|
| 84 | } |
|---|
| 85 | *month=nums[0]; |
|---|
| 86 | *day=nums[1]; |
|---|
| 87 | *year=(nums[2] % 100); |
|---|
| 88 | return; |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | /* ------------------------------------------------------------------ */ |
|---|
| 92 | /* Function two_char(). |
|---|
| 93 | /* Return true if string has two determinator char. |
|---|
| 94 | */ |
|---|
| 95 | int |
|---|
| 96 | two_char(string, determ) |
|---|
| 97 | char *string, determ; |
|---|
| 98 | { |
|---|
| 99 | int len, Lenstr(), indi; |
|---|
| 100 | |
|---|
| 101 | int count = 0; |
|---|
| 102 | |
|---|
| 103 | for(indi=0, len=Lenstr(string); indi<len; indi++) |
|---|
| 104 | if(string[indi]==determ) count++; |
|---|
| 105 | |
|---|
| 106 | if(count==2) return(1); |
|---|
| 107 | else return(0); |
|---|
| 108 | } |
|---|
| 109 | /* ----------------------------------------------------------------- */ |
|---|
| 110 | /* Function find_date_long_term(). |
|---|
| 111 | /* Find day, month, year in the long term date string |
|---|
| 112 | /* like day-of-week, month, day, time, year. |
|---|
| 113 | */ |
|---|
| 114 | void |
|---|
| 115 | find_date_long_form(date_string, month, day, year) |
|---|
| 116 | char *date_string; |
|---|
| 117 | int *month, *day, *year; |
|---|
| 118 | { |
|---|
| 119 | int indi, index, length, count, nums[3], num; |
|---|
| 120 | int Lenstr(), ismonth(), isdatenum(); |
|---|
| 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, Cmpcasestr(); |
|---|
| 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, Lenstr(), 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])) num=0; |
|---|
| 199 | if(num=1) num=atoi(string); |
|---|
| 200 | return(num); |
|---|
| 201 | } |
|---|
| 202 | /* ------------------------------------------------------------------ */ |
|---|
| 203 | /* Function is_genbank_date(). |
|---|
| 204 | /* Return true if it is genbank form of date, which is |
|---|
| 205 | /* day(2 digits)-MONTH(in letters)-year(4 digits). |
|---|
| 206 | */ |
|---|
| 207 | int |
|---|
| 208 | is_genbank_date(string) |
|---|
| 209 | char *string; |
|---|
| 210 | { |
|---|
| 211 | int Lenstr(); |
|---|
| 212 | |
|---|
| 213 | if(Lenstr(string)>=11&&string[2]=='-'&&string[6]=='-') return(1); |
|---|
| 214 | else return(0); |
|---|
| 215 | } |
|---|
| 216 | /* ---------------------------------------------------------------- */ |
|---|
| 217 | /* Function today_date(). |
|---|
| 218 | /* Get today's date. (SUN version) |
|---|
| 219 | */ |
|---|
| 220 | /* #### |
|---|
| 221 | char |
|---|
| 222 | *sun_today_date() { |
|---|
| 223 | struct tm *local_tm, *localtime(); |
|---|
| 224 | struct timeval tp; |
|---|
| 225 | struct timezone tzp; |
|---|
| 226 | char line[SIZE], *asctime(), *return_value, *Dupstr(); |
|---|
| 227 | |
|---|
| 228 | (void)gettimeofday(&tp, &tzp); |
|---|
| 229 | |
|---|
| 230 | Cpystr(line, ctime(&(tp.tv_sec))); |
|---|
| 231 | |
|---|
| 232 | return_value = Dupstr(line); |
|---|
| 233 | |
|---|
| 234 | return(return_value); |
|---|
| 235 | } |
|---|
| 236 | */ |
|---|
| 237 | /* ---------------------------------------------------------------- */ |
|---|
| 238 | /* Function today_date(). |
|---|
| 239 | /* Get today's date. (VAX/VMS version) |
|---|
| 240 | */ |
|---|
| 241 | char |
|---|
| 242 | *today_date() { |
|---|
| 243 | |
|---|
| 244 | int time_val; |
|---|
| 245 | char line[SIZE], *ctime(), *return_value, *Dupstr(); |
|---|
| 246 | |
|---|
| 247 | time(&time_val); |
|---|
| 248 | |
|---|
| 249 | Cpystr(line, ctime(&time_val)); |
|---|
| 250 | |
|---|
| 251 | return_value = Dupstr(line); |
|---|
| 252 | |
|---|
| 253 | return(return_value); |
|---|
| 254 | } |
|---|
| 255 | /* --------------------------------------------------------------- */ |
|---|
| 256 | /* Function gcg_date(). |
|---|
| 257 | /* Create gcg format of date. |
|---|
| 258 | */ |
|---|
| 259 | char |
|---|
| 260 | *gcg_date(date_string) |
|---|
| 261 | char *date_string; |
|---|
| 262 | { |
|---|
| 263 | char date[128], temp[128], time[128]; |
|---|
| 264 | int day, year, Cmpstr(); |
|---|
| 265 | void Catstr(); |
|---|
| 266 | |
|---|
| 267 | temp[0]='\0'; |
|---|
| 268 | date_string[7]='\0'; |
|---|
| 269 | if(Cmpstr("Jan", date_string+4)==1) |
|---|
| 270 | Catstr(temp, "January"); |
|---|
| 271 | else if(Cmpstr("Feb", date_string+4)==1) |
|---|
| 272 | Catstr(temp, "Feburary"); |
|---|
| 273 | else if(Cmpstr("Mar", date_string+4)==1) |
|---|
| 274 | Catstr(temp, "March"); |
|---|
| 275 | else if(Cmpstr("Apr", date_string+4)==1) |
|---|
| 276 | Catstr(temp, "April"); |
|---|
| 277 | else if(Cmpstr("May", date_string+4)==1) |
|---|
| 278 | Catstr(temp, "May"); |
|---|
| 279 | else if(Cmpstr("Jun", date_string+4)==1) |
|---|
| 280 | Catstr(temp, "June"); |
|---|
| 281 | else if(Cmpstr("Jul", date_string+4)==1) |
|---|
| 282 | Catstr(temp, "July"); |
|---|
| 283 | else if(Cmpstr("Aug", date_string+4)==1) |
|---|
| 284 | Catstr(temp, "August"); |
|---|
| 285 | else if(Cmpstr("Sep", date_string+4)==1) |
|---|
| 286 | Catstr(temp, "September"); |
|---|
| 287 | else if(Cmpstr("Oct", date_string+4)==1) |
|---|
| 288 | Catstr(temp, "October"); |
|---|
| 289 | else if(Cmpstr("Nov", date_string+4)==1) |
|---|
| 290 | Catstr(temp, "Novemember"); |
|---|
| 291 | else if(Cmpstr("Dec", date_string+4)==1) |
|---|
| 292 | Catstr(temp, "December"); |
|---|
| 293 | |
|---|
| 294 | sscanf(date_string+8, "%d %s %d", &day, time, &year); |
|---|
| 295 | sprintf(date, "%s %d, %d %s", temp, day, year, time); |
|---|
| 296 | |
|---|
| 297 | return(date); |
|---|
| 298 | } |
|---|