- time函数:
- 功能:获取系统时间(返回的是距离
1970-01-01 00:00:00 的秒数); - 具体内容:
#include
time_t time(time_t *tloc);
- localtime函数:
- 功能:把
time函数获取的系统时间(秒数)转换成日常生活中的(年月日时分秒); - 具体内容:
#include
struct tm *localtime(const time_t *timep);
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
#include
#include
int main(int argc, const char *argv[]){
time_t sec;
time(&sec);
printf("%ld\n",sec);
struct tm *lk = localtime(&sec);
printf("%4d-%02d-%02d %02d:%02d:%02d\n",\
lk->tm_year+1900,lk->tm_mon+1,lk->tm_mday,\
lk->tm_hour,lk->tm_min,lk->tm_sec);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
1695036506
2023-09-18 04:28:26