初步认识和使用C语言时间日期函数

在C语言的头文件time.h中,定义了日期和时间操作的数据类型和操作。

在此处,我们参考MinGW(Minimalist GNU for Windows)中的time.h。

首先我们看看time.h所声明的数据类型:

 1 typedef    long    clock_t;
 2
 3 typedef __int32 __time32_t;
 4 typedef    __time32_t time_t;
 5
 6 /*
 7  * A structure for storing all kinds of useful information about the
 8  * current (or another) time.
 9  */
10 struct tm
11 {
12     int    tm_sec;        /* Seconds: 0-59 (K&R says 0-61?) */
13     int    tm_min;        /* Minutes: 0-59 */
14     int    tm_hour;    /* Hours since midnight: 0-23 */
15     int    tm_mday;    /* Day of the month: 1-31 */
16     int    tm_mon;        /* Months *since* january: 0-11 */
17     int    tm_year;    /* Years since 1900 */
18     int    tm_wday;    /* Days since Sunday (0-6) */
19     int    tm_yday;    /* Days since Jan. 1: 0-365 */
20     int    tm_isdst;    /* +1 Daylight Savings Time, 0 No DST,
21                  * -1 don‘t know */
22 };

clock_t 是毫秒数。

time_t 是秒数。

struct tm是存放当前时间和日期信息的结构体。

然后我们来看看各个函数的声明和作用。(参考《C程序设计语言》(第二版))

  1. clock_t clock( void ) :返回程序开始执行以后相应的进程占用处理器的时间(毫秒数),我们可以使用clock()/CLOCKS_PER_SEC来得到秒数。其中宏CLOCKS_PER_SEC就是1000。
  2. time_t time(time_t *tp) :返回当前的时间距离1970年1月1日0时的秒数,同时把这个秒数赋值给tp指向的地址。如果不能获得时间,则返回-1。
  3. double difftime(time_t time2, time_t time1) :返回time2和time1相差的秒数。
  4. time_t mktime(struct tm *tp) :读入tp所指向的struct tm类型,返回当前的时间距离1970年1月1日0时的秒数。如果不能表示,则返回-1。
  5. char *asctime(const struct tm *tp) :将tp指向的结构转换为字符串形式,如Fri Nov 07  11:41:56 2014。此函数返回一个指针,其指向一块静态的缓存,可能被其他调用所覆盖。下面6,7,8说明的函数的返回值也有同样的问题。
  6. char *ctime(const time_t *tp) :将tp指向的时间转换为字符串形式。它等价于asctime(localtime(tp))。
  7. struct tm *localtime(const time_t *tp):将tp指向的时间转换当地时间,数据为struct tm结构,返回一个指向静态缓存的指针(可能被其他调用所覆盖,特别是gmtime(),请注意!)
  8. struct tm *gmtime(const time_t *tp) :将tp指向的时间转换为当地时间,数据为struct tm结构,返回一个指向静态缓存的指针(可能被其他调用所覆盖,特别是localtime(),请注意!)
  9. size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp) :函数根据fmt中的格式把tp所指向的时间结构转化为字符串,复制在s指向的内存中,最多smax个字符写入到s中,包括‘\0‘。如果fmt格式对应的内容加上‘\0‘超过smax,则函数返回0。否则函数返回复制的字符个数,不包括‘\0‘。如fmt为"%a"时,应该向s写入“Fri”,如果smax大于等于4,则函数返回3,否则复制失败,函数返回0。fmt的格式说明见《C程序设计语言》(第二版)的第235页。

简单的代码来调用上述函数:

 1 #include <time.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4
 5 #define ROUND_TO_INT(x) ((int)((x) + (((x)>0)?0.5:-0.5)))
 6
 7 int
 8 main(int argc, char** argv)
 9 {
10     // time()
11     time_t t1;
12     time_t* tp;
13     tp = (time_t*)malloc(sizeof(time_t));
14     t1 = time(tp);
15     printf("当前日历时间(距离1970年1月1日0时的秒数):%u\n", t1);
16     // time(), difftime()
17     time_t t2;
18     t2 = time(tp);
19     double difft = difftime(t2, t1);
20     printf("当前日历时间(距离1970年1月1日0时的秒数):%u,两次日历时间的差值:%f(秒)\n", t2, difft);
21     // localtime(),struct tm , asctime(), ctime()
22     // mktime(),将struct tm转换为time_t类型
23     struct tm *localtp;
24     localtp = localtime(tp);
25     printf("是否夏令时:%d,距离1月1日:%d(天),距离星期天:%d(天),距离1900年:%d年,\n"
26            "距离1月:%d(月),今天是:%d日%d时%d分%d秒\n", localtp->tm_isdst, localtp->tm_yday,
27            localtp->tm_wday, localtp->tm_year, localtp->tm_mon, localtp->tm_mday, localtp->tm_hour,
28            localtp->tm_min, localtp->tm_sec);
29     printf("以asctime呈现本地时间:%s", asctime(localtp));
30     printf("以ctime呈现本地时间:%s", ctime(tp)); // ctime(tp) == asctime(localtime(tp))
31     time_t t3 = mktime(localtp);
32     printf("当前日历时间(距离1970年1月1日0时的秒数):%u\n", t3);
33     // gmtime(),当我们使用 gmtime()之后,其实改变的是localtp所指向的地址。
34     // 因为asctime(), ctime(), gmtime(), localtime()返回的是指向可被其他调用覆盖的静态对象的指针
35     struct tm *gmtp;
36     gmtp = gmtime(tp);
37     printf("UTC时间:%s", asctime(gmtp));
38     time_t t4 = mktime(gmtp);
39     printf("当前日历时间(距离1970年1月1日0时的秒数):%u\n", t4);
40     printf("本地时间和UTC时间相差:%d(小时)\n", (int)(difftime(t3, t4)/3600));
41     // strftime()
42     char* buf = (char*)malloc(1024);
43     if (buf == NULL) {
44         printf("Out of space!\n");
45         return -1;
46     }
47     int count;
48     if ((count = strftime(buf, 1024, "%Z %Y年%m月%d日 %H时%M分%S秒", localtime(tp))) != 0) {
49         printf("格式化字数(中文算2个):%d\n格式化的时间:%s\n", count, buf);
50     }
51     // clock()
52     //sleep(1000);
53     int nclock = clock();
54     printf("程序开始执行后占用的处理器时间:%d(毫秒),约为%d(秒)\n",
55                     nclock, ROUND_TO_INT((double)nclock/CLOCKS_PER_SEC));
56     system("pause");
57     return 0;
58 }
时间: 2024-07-29 23:24:10

初步认识和使用C语言时间日期函数的相关文章

SQL-数学、字符串、时间日期函数和类型转换

--数学函数 --ABS绝对值,select ABS(-99)--ceiling取上限,select CEILING(4.5)--floor去下限select FLOOR(4.5)--power 几次方,select POWER(2,2)--round四舍五入,select round (6.45,1)--sqrt开平方select SQRT(9)--square平方select SQUARE(5) --字符串函数--ASCII 返回字符串最左边的字符ascii码select ASCII('na

聚合函数,数学、字符串、函数,时间日期函数

create database lianxi0425--创建一个名字为lianxi0425的数据库 go use lianxi0425 --使用练习0425这个数据库 go --创建一个学生xinxi1的表,填写学号.名字.出生年份.性别.分数.班级 create table xinxi1 ( code int not null, name varchar(50) not null, birth varchar(50) not null, sex char(10) not null, score

js 格式化时间日期函数小结

下面是脚本之家为大家整理的一些格式化时间日期的函数代码,需要的朋友可以参考下. 代码如下: Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(

SQl Server 函数篇 数学函数,字符串函数,转换函数,时间日期函数

数据库中的函数和c#中的函数很相似 按顺序来, 这里价格特别的 print  可以再消息栏里打印东西 数学函数 ceiling()  取上限   不在乎小数点后面有多大,直接忽略 floor()     取下限   同上 round(列名,保留的位数)   四舍五入   保留小数最后那位数进不进一只看保留位数的后一位数够不够条件,再往后的就不管了 ABS()     绝对值---防抱死233 PI()        圆周率   就是查询一个圆周率 SQRT()平方根 字符串函数 upper()

数据库开发基础-教案-5-字符串函数、时间日期函数、数据转换,函数转换

字符串函数: 时间日期函数: SET DATEFIRST 1 SELECT @@DATEFIRST AS '1st Day', DATEPART(dw, GETDATE()) AS 'Today' SELECT GETDATE() 数据转换.函数转换: 练习:查看名字,生日

时间日期函数,类型转化,子查询,分页查询

1.时间日期函数: SET DATEFIRST 1 --设置星期一为第一天--datepart函数,返回时间日期中的某一个部分--参数1是指返回哪一个部分,dw表示dayofweek--参数2是指哪个时间日期里面去返回--datefirst是系统常量,在使用时需要加上@@SELECT @@DATEFIRST AS '1st Day', DATEPART(dw, GETDATE()) AS 'Today'--getdate指在执行时获取当前系统时间SELECT GETDATE()--在执行时取当前

SQL server 模糊查询 排序 聚合函数 数学函数 字符串函数 时间日期函数 转换、函数转换

create database lianxi831  --创建数据库gouse lianxi831  --引用数据库gocreate table xs  --插入表格( code int not null,  --写入内容 name varchar(10), cid varchar(18), banji varchar(10), yufen decimal(18,2), shufen decimal(18,2), yingfen decimal(18,2),)goinsert into xs v

SQL 时间日期函数

1.时间日期函数

语句、聚合函数、数学函数、字符串函数、时间日期函数

  --添加列 alter table shuiguo add price decimal(18,2) --删除列 alter table shuiguo drop column price --更改数据库的名称,逗号前面是之前的,逗号后是要改成的名字 sp_renamedb student,xuesheng 更改数据库的名称 表中有数据的情况下再添加列.删除列 语句示例 --查询所有数据 select * from xuesheng --查询开头是王的所有数据 select * from xu