Skip to content

Instantly share code, notes, and snippets.

@marcosdalte
Created May 30, 2016 00:36
Show Gist options
  • Save marcosdalte/1c36377ef4b34068500f7dfc6c052d90 to your computer and use it in GitHub Desktop.
Save marcosdalte/1c36377ef4b34068500f7dfc6c052d90 to your computer and use it in GitHub Desktop.
Format Expiration Date from Whois to .br and .com
#include <stdio.h>
#include <string.h>
#include <time.h>
char *ret_num_month(char *nm_month);
char *ret_num_month(char *nm_month){
typedef struct param{
char *chave;
char *valor;
} param;
param month[] = {
{"jan","01"},
{"feb","02"},
{"mar","03"},
{"apr","04"},
{"may","05"},
{"jun","06"},
{"jul","07"},
{"aug","08"},
{"sep","09"},
{"oct","10"},
{"nov","11"},
{"dec","12"},
{NULL, NULL}
};
int i = 0;
while(month[i].chave != NULL){
if(!strcasecmp(month[i].chave, nm_month)){
printf("number month found %s:%s\n", month[i].chave, month[i].valor);
return month[i].valor;
}
i++;
}
return NULL;
}
int format_date_wcom(char *data_com, char *datetime);
int format_date_wcom(char *data_com, char *datetime){
data_com++;
printf("[%s:%d]:%s\n", __FUNCTION__, __LINE__, data_com);
char *day = strtok(data_com , "-");
day++;
printf("[%s:%d]:%s\n", __FUNCTION__, __LINE__, day);
char *month = strtok(NULL, "-");
printf("[%s:%d]:%s\n", __FUNCTION__, __LINE__, month);
month = ret_num_month(month);
printf("[%s:%d]:number month %s\n", __FUNCTION__, __LINE__, month);
char *year = strtok(NULL, "-");
printf("[%s:%d]:%s\n", __FUNCTION__, __LINE__, year);
snprintf(datetime, 40, "%s-%s-%sT12:12:12.0Z",year, month, day);
printf("datetime whois .com %s\n", datetime);
return 0;
}
int format_date_wbr(char *data_br, char *datetime);
int format_date_wbr(char *data_br, char *datetime){
printf("[%s:%d]:%s\n", __FUNCTION__, __LINE__, data_br);
int i,j = 0;
char *aux = NULL;
char day[3];
char month[3];
char year[5];
memset(year, '\0', sizeof(year));
memset(month, '\0', sizeof(month));
memset(day, '\0', sizeof(day));
for(i = 0; *(data_br+i) != '\0'; i++ ){
if(!isspace(data_br[i])){
if(j<4){
*(year+strlen(year)) = *(data_br+i);
} else if(j>3 && j<6){
*(month+strlen(month)) = *(data_br+i);
} else if(j>5 && j<8){
*(day+strlen(day)) = *(data_br+i);
}
j++;
}
}
*(year+strlen(year)) = '\0';
*(month+strlen(month)) = '\0';
printf("year %s\n", year);
printf("month %s\n", month);
printf("day %s\n", day);
snprintf(datetime, 40, "%s-%s-%sT12:12:12.0Z",year, month, day);
printf("datetime whois .br %s\n", datetime);
return 0;
}
int main(){
char dt_exp_br[100] = "expires: 20191117\
";
char dt_exp_com[100] = "Expiration Date: 05-aug-2019";
printf("%s\n", dt_exp_br);
printf("%s\n", dt_exp_com);
char *data_br = strtok(dt_exp_br,"expires:");
printf("dt_exp_br %s\n",data_br);
char *data_com = strchr(dt_exp_com, ':');
printf("dt_exp_com %s\n",data_com);
char datetime[40];
memset(datetime, '\0', sizeof(datetime));
format_date_wcom(data_com, datetime);
printf("datetime whois .com %s\n", datetime);
format_date_wbr(data_br, datetime);
printf("datetime whois .br %s\n", datetime);
return 0;
}
@marcosdalte
Copy link
Author

Output:

expires:     20191117        
Expiration Date: 05-aug-2019
dt_exp_br      20191117        
dt_exp_com : 05-aug-2019
[format_date_wcom:41]: 05-aug-2019
[format_date_wcom:44]:05
[format_date_wcom:47]:aug
number month found aug:08
[format_date_wcom:49]:number month 08
[format_date_wcom:52]:2019
datetime whois .com 2019-08-05T12:12:12.0Z
datetime whois .com 2019-08-05T12:12:12.0Z
[format_date_wbr:61]:     20191117        
year 2019
month 11
day 17
datetime whois .br 2019-11-17T12:12:12.0Z
datetime whois .br 2019-11-17T12:12:12.0Z

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment