c_获取系统本地时间
涉及到的系统函数
| 函数名 | 函数功能 | 函数参数 | 函数返回值 | 使用举例 |
| time | 获取1970-01-01到当前日期的秒数。 |
time(time_t *tloc) 调用时,需要传递一个time_t类型变量的地址。 |
time_t time(time_t *tloc) 成功: 失败:(time_t)-1 |
|
| localtime | 将秒数时间转换为本地时间。 |
struct tm *localtime(const time_t *timep) 需要一个类型为time_t变量的地址。 |
返回值为结构体指针,类型为struct tm | |
| gettimeofday | 获取当前时间,一般用来获取微秒。 | gettimeofday(struct timeval *restrict tp, void *restrict tzp) | 使用时,一般第二个参数都为NULL。 | |
| strftime | 对struct tm结构中的时间进行格式化输出。 |
size_t strftime(char *s, size_t max, const char *format, |
time_t v_time; time(&v_time); struct tm *pst_tm = localtime(&v_time); char date[8+1]; strftime(date, sizeof(date), "%Y%m%d", pst_tm); |
涉及到的数据类型
time_t、struct tm、struct timeval
struct tm { int tm_sec; /* seconds */ int tm_min; /* minutes */ int tm_hour; /* hours */ int tm_mday; /* day of the month */ int tm_mon; /* month */ int tm_year; /* year */ int tm_wday; /* day of the week */ int tm_yday; /* day in the year */ int tm_isdst; /* daylight saving time */ };
struct timeval { __time_t tv_sec; /* Seconds. */ __suseconds_t tv_usec; /* Microseconds. */ }
功能实现代码:
posted on 2021-02-09 19:15 XiaoXiaoli 阅读(108) 评论(0) 收藏 举报
浙公网安备 33010602011771号