一、C 语言处理时间
时间处理需要导入
<time.h>
头文件。
1 库变量
变量 | 描述 |
---|---|
size_t | 是无符号整数类型,它是 sizeof 关键字的结果。 |
clock_t | 这是一个适合存储处理器时间的类型。 |
time_t is | 这是一个适合存储日历时间类型。 |
struct tm | 这是一个用来保存时间和日期的结构。 |
2 tm结构体
struct tm {
int tm_sec; /* 秒,范围从 0 到 59 */
int tm_min; /* 分,范围从 0 到 59 */
int tm_hour; /* 小时,范围从 0 到 23 */
int tm_mday; /* 一月中的第几天,范围从 1 到 31 */
int tm_mon; /* 月,范围从 0 到 11 */
int tm_year; /* 自 1900 年起的年数 */
int tm_wday; /* 一周中的第几天,范围从 0 到 6 */
int tm_yday; /* 一年中的第几天,范围从 0 到 365 */
int tm_isdst; /* 夏令时 */
};
3 宏
NULL 这个宏是一个空指针常量的值。
CLOCKS_PER_SEC 这个宏表示每秒的处理器时钟个数。
4 库函数
char *asctime(const struct tm *timeptr)
返回一个指向字符串的指针,它代表了结构 timeptr 的日期和时间。
clock_t clock(void)
返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。
char *ctime(const time_t *timer)
返回一个表示当地时间的字符串,当地时间是基于参数 timer。
double difftime(time_t time1, time_t time2)
返回 time1 和 time2 之间相差的秒数 (time1-time2)。
struct tm *gmtime(const time_t *timer)
timer 的值被分解为 tm 结构,并用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
struct tm *localtime(const time_t *timer)
timer 的值被分解为 tm 结构,并用本地时区表示。
time_t mktime(struct tm *timeptr)
把 timeptr 所指向的结构转换为一个依据本地时区的 time_t 值。
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
根据 format 中定义的格式化规则,格式化结构 timeptr 表示的时间,并把它存储在 str 中。
time_t time(time_t *timer)
计算当前日历时间,并把它编码成 time_t 格式。
5 示例
#include <stdio.h>
#include <time.h>
int main () {
struct tm *sysTime;
time_t nowTime;
time(&nowTime);
sysTime = localtime(&nowTime);
printf("%ld\n", nowTime);
printf("%d-%d-%d %d:%d", sysTime->tm_year + 1900, sysTime->tm_mon + 1,
sysTime->tm_mday, sysTime->tm_hour, sysTime->tm_min);
return 0;
}
二、windows上获取时间
#include <stdio.h>
#include <windows.h>
int main() {
SYSTEMTIME currentTime;
// GetSystemTime返回UTC时间
// GetLocalTime返回本地时间
GetLocalTime(¤tTime);
printf("time: %u/%u/%u %u:%u:%u:%u %d\n",
currentTime.wYear,currentTime.wMonth,currentTime.wDay,
currentTime.wHour,currentTime.wMinute,currentTime.wSecond,
currentTime.wMilliseconds,currentTime.wDayOfWeek);
return 0;
}
三、格式化时间
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
//#define FILLP_LINUX
#define FILLP_WIN
int main() {
struct tm sysTime;
time_t nowTime = time(&nowTime);
#ifdef FILLP_LINUX
localtime_r(&time_seconds, &sysTime);
#endif
#ifdef FILLP_WIN
localtime_s(&sysTime,&nowTime);
#endif
char *log = (char*)malloc(sizeof(int)*6);
strftime(log, strlen(log), "%Y-%m-%d %H:%M:%S", &sysTime);
printf("%s", log);
return 1;
}