美文网首页
C标准库——

C标准库——

作者: Jack_6a46 | 来源:发表于2018-10-08 23:41 被阅读0次

time.h 头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数。

库变量

size_t 是无符号整数类型,它是sizeof关键字的结果。
clock_t 是一个适合存储处理器时间的类型
time_t is 是一个适合存储日历时间类型
struct 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; /
夏令时 */
};

库宏

NULL 一个空指针常量的值
CLOCK_PER_SEC 表示每秒处理器时钟个数

库函数

char *asctime(const struct tm *timeptr)
返回一个指向字符串的指针,它代表了结构timeptr的日期和时间
返回值: 返回一个C字符串,包含了可读格式的日期和时间信息 Www Mmm dd hh:mm:ss yyyy

void main()
{
struct tm time;
time.tm_sec = 10;
time.tm_min = 45;
time.tm_hour = 22;
time.tm_mday = 8;
time.tm_mon = 10;
time.tm_year = 118;
time.tm_wday = 1;
puts(asctime(&time));
getchar();
}

C库函数clock_t clock(void) 返回程序执行起处理器时钟所用的时间。为了获取CPU所使用的秒数,你需要除以 CLOCKS_PER_SEC.
在32位系统中,CLOCKS_PER_SEC等于 1000 000,该函数大约每72分钟会返回相同的值。
返回值: 该函数返回自程序启动起,处理器时钟所使用的时间。如果失败,则返回 -1 值。

void main()
{
clock_t start_t, end_t;
double total_t;
int i;
start_t = clock();
printf("程序启动,start_t = %ld\n", start_t);

for(i = 0; i < 10000000; i++)
{
    5+3;
}
end_t = clock();
printf("循环结束, end_t = %ld\n", end_t);
total_t = (double)(end_t - start_t)/CLOCKS_PER_SEC;
printf("CPU占用的时间为:total_t = %f\n", total_t);
getchar();
}

C库函数 char *ctime(const time_t *timer)返回一个表示当地时间的字符串,当当地时间是基于参数 timer.

void main()
{
time_t currentTime;
printf("当前时间: %s\n", ctime(&currentTime));
getchar();
}
image.png

C库函数double difftime(time_t time1, time_t time2) 返回time 1 和 time 2之间相差的秒数(time1 - time2).

#include <stdio.h>
#include <time.h>
#include <unistd.h>
void main()
{
time_t start_t, end_t;
double diff_t;
printf("启动程序。。。\n");
time(&start_t);
sleep(6);
time(&end_t);
diff_t = difftime(end_t, start_t);
printf("执行时间: %f\n", diff_t);
getchar();
}

C库函数 struct tm *gmtime(const time_t *timer)使用timer 的值来填充tm结构,并用协调世界时间(UTC)也被称为格林尼治标准时间(GMT)表示。

#define CCT 8
void main()
{
time_t current_t;
struct tm *pstCurrentTime;
time(&current_t);
pstCurrentTime = gmtime(&current_t);
printf("当前的世界时钟为: \n");
printf("中国:%.2d: %02d \n", (pstCurrentTime->tm_hour+CCT)%24, pstCurrentTime-      >tm_min);
getchar();
}

C库函数struct *localtime(const time_t *timer)使用timer的值来填充tm结构。timer的值被分解为tm结构,并用本地时区表示。

C库函数 time_t mktime(struct tm *timerptr) 把timeptr所指向的结构转换为一个依据本地时区的time_t的值。

void main()
{
int year, month, day;
time_t rawTime;
struct tm * pstTime;
const char * weekday[] = {"周日", "周一", 
    "周二", "周三", "周四", "周五", "周六"};
printf("年:");;
scanf("%d", &year);
printf("月:");
scanf("%d", &month);
printf("日:");
scanf("%d", &day);

time(&rawTime);
pstTime = localtime(&rawTime);
pstTime->tm_year = year - 1900;
pstTime->tm_mon = month - 1;
pstTime->tm_mday = day;

mktime(pstTime);
printf("这一天是:%s\n", weekday[pstTime->tm_wday]);
getchar();
}
image.png

C库函数 size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)根据format中定义的格式化规则,格式化结构timeptr表示的时间,并把它存储在str中。

void main()
{
time_t currentTime;
struct tm *pstTime;
char cOut[52];
time(&currentTime);
pstTime = localtime(&currentTime);
strftime(cOut, 52, "%Y-%m-%d  %H:%M:%S", pstTime);
printf("格式化的日期时间为:%s\n", cOut);
getchar();
}

C库函数 time_t time(time_t *seconds)返回自Epoch起经过的时间,以秒为单位。
如果seconds不为空,则返回值也存储在seconds中。

void main()
{
time_t seconds;
seconds = time(NULL);
printf("自1970年起的小时数为:%ld\n", seconds/3600);
printf("自1970年起的天数为:%ld\n", seconds/3600/24);   
getchar();
}

相关文章

  • C标准库——

    time.h 头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数。 库变量 size_t 是无符号整...

  • Boolan C++标准库 第一周

    C++标准库 第一讲 一、认识headers、版本 1.C++标准库 vs STL C++标准库大于STL(标准...

  • 博览网:STL与泛型编程第一周笔记

    1.C++标准库和STL C++标准库以header files形式呈现: (1)C++标准库的header fi...

  • Boolan C++ STL与泛型编程_1

    c++标准库--体系结构与内核分析 主要内容: 本节主要对c++标准库学习的4个阶段,c++标准库和新旧式C的头文...

  • C++ time.h 使用

    C++ (time.h)库笔记 以及简便计算日期差等 头文件 1. 计算并格式化当前时间 2. 计算...

  • C++ STL(1)

    C++ STL(1) from my csdn blog C++标准模板库 容器C++标准模板库提供了10种容器基...

  • GeekBand之C++面向对象高级编程(下)第二周笔记

    关于C++标准库

  • C标准库

    今天总结一下C语言标准库。 C语言标准库(C89)包含15个头文件,新的C99以及C11又定义了一些其他的库,这里...

  • 1. C 标准库

    1.C 标准库 – | 菜鸟教程——string.h 2.C 标准库 – | 菜鸟教程——stdio.h 3....

  • [资源]C++ 程序员必收藏

    C++ 资源大全中文版 标准库 C++标准库,包括了STL容器,算法和函数等。 C++ Standard Libr...

网友评论

      本文标题:C标准库——

      本文链接:https://www.haomeiwen.com/subject/wmlsaftx.html