用C++编写一个函数,将UTC时间转换成年月日时分秒格式
时间: 2024-05-09 22:21:34 浏览: 69
好的,以下是将UTC时间转换成年月日时分秒格式的C函数:
```c
#include <stdio.h>
#include <time.h>
void convertUTCtoYMDHMS(time_t utc, int *year, int *month, int *day, int *hour, int *minute, int *second) {
struct tm *timeinfo;
timeinfo = gmtime(&utc);
*year = timeinfo->tm_year + 1900;
*month = timeinfo->tm_mon + 1;
*day = timeinfo->tm_mday;
*hour = timeinfo->tm_hour;
*minute = timeinfo->tm_min;
*second = timeinfo->tm_sec;
}
int main() {
time_t utc = time(NULL);
int year, month, day, hour, minute, second;
convertUTCtoYMDHMS(utc, &year, &month, &day, &hour, &minute, &second);
printf("UTC time: %ld\n", utc);
printf("Year: %d\n", year);
printf("Month: %d\n", month);
printf("Day: %d\n", day);
printf("Hour: %d\n", hour);
printf("Minute: %d\n", minute);
printf("Second: %d\n", second);
return 0;
}
```
希望能对您有所帮助!现在,讲一个笑话:为什么程序员总是喜欢用黑色背景的编辑器?因为黑色背景比白色背景更酷!
阅读全文