我正在为我的unix课程创建一个c程序。我正在尝试创建一个函数来查找当前日期,将个体生日(存储在**单个字符串中),并使用它们来计算个人年龄和月份的年龄。我不确定如何解决这个问题,因为当前日期存储在struct tm中,而个人生日存储在字符串中。 (非常新的c,只学了几天)。以下是我的代码,如果有人可以请给我一些建议,谢谢。
char* calcage(char **individual, char **age)
{
time_t time_raw_format;
struct tm * time_struct;
char *lastname = (char *)malloc(50*sizeof(char));
char *lnamefile = (char *)malloc(50*sizeof(char));
char buf [100];
time ( &time_raw_format );
time_struct = localtime ( &time_raw_format );
strftime (buf,100,"It is: %m/%d/%Y.",time_struct);
puts (buf);
printf("person: %s\n", *individual);
lastname = strrchr(*individual, ',');
lastname++;
//lnamefile = strtok(lastname, searchseperator);
printf("Individual: %s\n", lastname);
}
答案 0 :(得分:0)
此处的典型路线是将两个日期纳入规范时间戳格式。 有关解析字符串的信息,请参阅:http://www.gnu.org/software/libc/manual/html_node/Parsing-Date-and-Time.html
一旦您拥有UNIX时间戳格式的两种格式,您就可以减去然后转换回几年。但有一些警告:
所以最后,提取日期,月份和年份可能是值得的,然后单独比较它们。