C的标准库<time.h>包含了一些处理时间与日期的函数。

1.clock_t clock(void);

函数返回程序自开始执行后的处理器时间,类型是clock_t,单位是tick。如果有错误,clock()函数就返回-1。

类型clock_t在<time.h>中定义,等价于size_t类型。CLOCKS_PER_SEC是<time.h>中定义的宏,表示一秒内的tick数,且是clock_t类型。将clock()函数返回值除以CLOCKS_PER_SEC,得到处理器运行时间。

代码示例:

#include <stdio.h>
#include<time.h>
#include<windows.h>
#include<stdlib.h>
main()
{
clock_t start,end;
double cpu_time;
start = clock();
Sleep();
end = clock();
cpu_time = (double)(end-start)/CLOCKS_PER_SEC;
printf("%.2lf",cpu_time);
system("pause");
}

输出结果:2.00

2. time_t time(time_t *time_t)
time()函数返回从1970年1月1日格林威治时间0点0分0秒到现在的秒数。类型time_t在<time.h>中定义,等价于long。
如果变元不是NULL,则返回值就存在time_t变元指向的位置中。

double difftime(time_t T2,time_t T1)
返回T2-T1的数值,类型是double,单位是秒。

代码示例:

#include <stdio.h>
#include<time.h>
#include<windows.h>
#include<stdlib.h>
main()
{
time_t calendar_start,calendar_end;
calendar_start = time(NULL);
Sleep();
calendar_end = time(NULL);
printf("start:%d\n",calendar_start);
printf("end:%d\n",calendar_end);
printf("diff:%.0lf\n",difftime(calendar_end,calendar_start));
system("pause");
}

输出结果:
start:1460010918
end:1460010920
diff:2

3.char *ctime(const time_t *timer)
函数接收一个time_t变量的指针作为变元,返回一个指向26个字符的字符串指针。其中有星期,日期,时间以及年,最后用一个\n和\0终止。如:"Mon Aug 25 10:45:36 2015\n\0"

代码示例:

#include <stdio.h>
#include<time.h>
#include<stdlib.h>
main()
{
time_t calendar;
calendar = time(NULL);
printf("%s\n",ctime(&calendar));
system("pause");
}

输出结果:Thu Apr 07 14:34:10 2016

4.struct tm *localtime(const time_t* timer)
函数接收一个time_t值的指针,返回结构类型tm的指针。

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代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一 */
int tm_yday; /* 从每年的1月1日开始的天数 */
int tm_isdst; /* 夏令时标识符。*/
}

代码示例:

#include <stdio.h>
#include<time.h>
#include<stdlib.h>
main()
{
time_t calendar;
struct tm *time_data;
char *month[] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
char *week[] = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
char *suffix[] = {"st","nd","rd","th"};
enum suffixIndex {st,nd,rd,th} sufsel = th;
calendar = time(NULL);
time_data = localtime(&calendar);
printf("format date:%d/%d/%d\n",time_data->tm_year+,time_data->tm_mon,time_data->tm_mday);
switch(time_data->tm_mday)
{
case :
case :
case :
sufsel = st;
break;
case :
case :
sufsel = nd;
break;
case :
case :
sufsel = rd;
break;
default:
sufsel = th;
break;
}
printf("today is %s,%s,%d%s,%d",week[time_data->tm_wday],month[time_data->tm_mon],time_data->tm_mday,suffix[sufsel],time_data->tm_year+);
system("pause");
}

输出结果:
format date:2016/3/7
today is 星期五,四月,7th,2016

4.localtime()函数生成的是本地时间,若要使用UTC(世界调整时间),可使用gmtime()函数

5.time_t mktime(struct tm *ptime)
函数接收一个tm结构的变元指针,返回值类型为timt_t。

c--日期和时间函数的更多相关文章

  1. Sql Server函数全解(四)日期和时间函数

      日期和时间函数主要用来处理日期和时间值,本篇主要介绍各种日期和时间函数的功能和用法,一般的日期函数除了使用date类型的参数外,也可以使用datetime类型的参数,但会忽略这些值的时间部分.相同 ...

  2. MySQL数据库9 - 日期与时间函数

    一 日期和时间函数 函数的概念:按指定格式输入参数,返回正确结果的运算单元 1. 返回当前日期:curdate() current_date() current_date()+0可以将当前日期转换为数 ...

  3. sqlserver常用日期、时间函数和格式

    Sql Server中常用的日期与时间函数1.  当前系统日期.时间    select getdate() 2. dateadd  在向指定日期加上一段时间的基础上,返回新的 datetime 值  ...

  4. 20101102--SQL字符串函数 ,日期和时间函数

    --------------------字符串函数------------------------- --ASCII 返回字符串的首字母的ASCII编码 select ASCII('w') selec ...

  5. Sql Server函数全解<四>日期和时间函数

    原文:Sql Server函数全解<四>日期和时间函数   日期和时间函数主要用来处理日期和时间值,本篇主要介绍各种日期和时间函数的功能和用法,一般的日期函数除了使用date类型的参数外, ...

  6. MySQL 笔记 —— 日期和时间函数

    [TOC] 获取当前日期的函数和获取当前时间的函数 CURDATE()和CURRENT_DATE()函数获取当前日期:CURTIME()和CURRENT_TIME()函数获取当前时间. mysql&g ...

  7. MySQL数据库—日期与时间函数

    一. 日期和时间函数 函数的概念:按指定格式输入参数,返回正确结果的运算单元 1. 返回当前日期:curdate() current_date() current_date()+0可以将当前日期转换为 ...

  8. web报表工具FineReport常用函数的用法总结(日期和时间函数)

    web报表工具FineReport常用函数的用法总结(日期和时间函数) 说明:凡函数中以日期作为参数因子的,其中日期的形式都必须是yy/mm/dd.而且必须用英文环境下双引号(" " ...

  9. MySql日期与时间函数

    select DATE_FORMAT(date_sub(current_date(), interval 1 day), '%Y-%m-%d') -- 2018-05-29(昨天) select DA ...

  10. sql的日期和时间函数–date_format

    Mysql的日期和时间函数–date_format   DATE_FORMAT(date,format)依照 format 字符串格式化 date 值.下面的修饰符可被用于 format 字符串中:修 ...

随机推荐

  1. 浅谈ELK日志分析平台

    作者:珂珂链接:https://zhuanlan.zhihu.com/p/22104361来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 小编的话 “技术干货”系列文章 ...

  2. JavaWeb学习笔记1

    表现层实现:先画静态界面(1定义界面须要显示的组件,2初始化组件并设置组件的属性,3把组件放入panel,把panel放入frame)写界面通过代码实现动态功能.  展示数据的步骤:1,i调用逻辑接口 ...

  3. apache安装mod_ssl.so 出现 undefined symbol: ssl_cmd_SSLPassPhraseDialog错误解决

    很久很久以前,安装Apache的时候,根本没想过将来的某一天会使用到ssl,所以也就没有安装那个模块,结果今天需要用到的时候,却无从下手了. 由于在安装Apache的时候,mod_ssl.so这个文件 ...

  4. php安装 出现Sorry, I cannot run apxs. ***错误解决方法

    # tar zvxf php-5.1.2.tar.gz# cd php-5.1.2# ./configure --prefix=/usr/local/php --with-mysql=/usr/loc ...

  5. TestNg的IReporter接口的使用

    IReporter接口是干嘛的?就是让用户自定义报告的,很多人想要自定义报告,于是乎找各种插件,比如什么testng-xslt啊,reportng啊,各种配置,最后出来的结果,还不能定制化,但为什么不 ...

  6. 彻底解决asp.net mvc5.2.2:vs2013 cshtml视图文件报错(当前上下文中不存在名称“model”,ViewBag,Url)

    最近遇到一个奇葩的问题,在vs2013下cshtml视图文件报错,出现当前上下文中不存在名称“model”,ViewBag,Url等等),在视图中也没有智能提示了,用@model声明视图的model类 ...

  7. 【HTML】WWW简介

    www 什么是WWW www(world wide web),又称为万维网,或通常称为web,是一个基于超文本方式的信息检索服务工具. WWW的工作模式 C/S结构(client/server结构), ...

  8. numpy的生成网格矩阵 meshgrid()

    numpy模块中的meshgrid函数用来生成网格矩阵,最简单的网格矩阵为二维矩阵 meshgrid函数可以接受 x1, x2,..., xn 等 n 个一维向量,生成 N-D 矩阵. 1 基本语法 ...

  9. Mapped Statements collection does not contain value for com.xxxx.dao.impl.AreaDAOImpl.findByCode

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Persiste ...

  10. freeswitch订阅会议相关通知

    一. freeswitch订阅会议相关通知 event plain CUSTOM conference::maintenance 这时会收到各种通知,会议创建.成员加入.成员离开.成员开始讲话,成员停 ...