C 库 time
1:函数time(),返回从(1970-01-01 00:00:00 UTC)起经过的时间,单位秒;
time_t seconds; time(&seconds); //或者 time_t tm_seconds = time(NULL);
2:struct tm 是保存时间和日期的结构,定义如下:
struct tm { int tm_sec; // seconds after the minute - [0, 60] including leap second int tm_min; // minutes after the hour - [0, 59] int tm_hour; // hours since midnight - [0, 23] int tm_mday; // day of the month - [1, 31] int tm_mon; // months since January - [0, 11] int tm_year; // years since 1900 int tm_wday; // days since Sunday - [0, 6] int tm_yday; // days since January 1 - [0, 365] int tm_isdst; // daylight savings time flag };
3: asctime() 返回一个可读日期的字符串
struct tm *tm_struct; char *date= asctime(tm_struct);
4:函数localtime() struct tm *localtime(const time_t *timer) 将timer秒转化为本地时间的struct tm的结构体,
time_t seconds; time(&seconds); struct tm *tm_ptr; tm_ptr = localtime(&seconds); char *now_date = asctime(tm_ptr); //Thu Oct 15 16:49:12 2020 printf("%s", now_date);
5:ctime() 获取到的时间(秒)转化为一个可读日期的字符串 char *ctime(const time_t *timer)
time_t seconds; time(&seconds); char *date_c = ctime(&seconds); //Thu Oct 15 17:22:01 2020 printf("%s", date_c);
6:函数difftime() double difftime(time_t time1, time_t time2) 返回俩个时间(time1-time2)相差的秒
time_t start; time_t end; double interval = difftime(end,start);
7:gmtime 与localtime不同的是 gmtime得到是协调世界时(UTC)也被称为格林尼治标准时间(GMT)
time_t seconds; time(&seconds); struct tm tm_info ; gmtime_s(&tm_info,&seconds); printf("伦敦:%2d:%02d\n", (tm_info.tm_hour + 1) % 24, tm_info.tm_min); printf("中国:%2d:%02d\n", (tm_info.tm_hour + 8) % 24, tm_info.tm_min);
8:mktime():time_t mktime(struct tm *time) 把struct tm的结构转换为一个依据本地时区的 时间值(s)。
struct tm tm_info; time_t currrent_t = mktime(&tm_info);
9:strftime(buffer, 80, "%Y-%m-%d %H:%M:%S ", tm_ptr); 将struct tm按照一定格式输出进buffer
strftime(buffer, 80, "%x %X %p %A %B %U %c", tm_ptr); printf(" date = %s", buffer);
clock_t start_t, end_t; double total_t; int i; start_t = clock(); for (i = 0; i < 10000000; i++) { } end_t = clock(); total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC; printf("CPU 占用的总时间:%f\n", total_t);
11:结构体timespec
struct timespec { time_t tv_sec; // Seconds - >= 0 long tv_nsec; // Nanoseconds - [0, 999999999] };
12:timespec_get获取自1970-01-01 00:00:00 UTC的时间填充timespec结构 ,timespec_get(struct timespec *ts,int base);
struct tm *tm_ptr; struct timespec ts; char buffer[256] = { 0 }; timespec_get(&ts,TIME_UTC); tm_ptr=localtime(&ts.tv_sec); strftime(buffer, 80, "%Y-%m-%d %H:%M:%S ", tm_ptr);
13:_get_tzname() 检索时区名称或夏令时标准时区名称 (DST) 的字符串表示形式
enum TZINDEX { STD, DST }; size_t tznameSize = 0; char *tznameBuffer =NULL; _get_tzname(&tznameSize, NULL, 0, DST); tznameBuffer = (char*)malloc(tznameSize); if (_get_tzname(&tznameSize, tznameBuffer, tznameSize, DST)==0) { printf("%s ", tznameBuffer); }
14:_get_timezone() 协调世界时 (UTC) 和当地时间之间的差异(以秒为单位)。
long zone; _get_timezone(&zone);
15:_get_daylight( int* hours ) 夏令时偏移量小时
int hours; _get_daylight(&hours);
16 :_get_dstbias( int* seconds );夏令时偏移量秒

浙公网安备 33010602011771号