[英]Why is C struct tm (time.h) returning the wrong month?
現在是 2020 年 4 月 10 日。我在 C 中制作了這個 integer 月到字符串月轉換器 function。 它接收一個 integer 並返回一個字符串。 由於某種原因,它認為是三月。 我調查了問題是我的轉換器還是我打印出myTime->tm_mon
的其他問題,當它應該返回3
(4 月)時它返回2
(3 月)。 任何人都可以找到(我假設是)我的錯誤並向我指出嗎?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct tm tm;
void *numberToLetters(int month) {
char *smonth;
switch (month) {
case (0):
smonth = "January";
break;
case (1):
smonth = "February";
break;
case (2):
smonth = "March";
break;
case (3):
smonth = "April";
break;
case (4):
smonth = "May";
break;
case (5):
smonth = "June";
break;
case (6):
smonth = "July";
break;
case (7):
smonth = "August";
break;
case (8):
smonth = "September";
break;
case (9):
smonth = "October";
break;
case (10):
smonth = "November";
break;
case (11):
smonth = "December";
break;
default:
return NULL;
}
return smonth;
}
int main() {
time_t present;
time(&present);
tm *myTime = &present;
void *month = (char *)numberToLetters(myTime->tm_mon);
printf("%s\n", month);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.