C语言中获取时间时需要添加头文件:time.h
普通情况下,下列程序足以满足需要。
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
int main()
{
time_t now;
now = time(&now);
char * timeStr = ctime(&now);
printf("Now Time is %s ",timeStr);
return 0;
}</span>
运行结果:Now Time is Sat Oct 10 14:12:30 2015
下边对一些比较常用的进行一下说明。
首先,我们来说明一下最重要的时间结构体。
<span style="font-family:KaiTi_GB2312;font-size:14px;">struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
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 */
};</span>
函数名:time
函数功能:返回从公元 1970 年1 月1 日的UTC 时间从0 时0 分0 秒算起到现在所经过的秒数。
函数原型:time_t __cdecl time(time_t *);
函数返回:long 参照 typedef long time_t;
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
int main()
{
time_t now;
now = time(&now);
printf("Now Time is %d ",now);
return 0;
}</span>
运行结果:Now Time is 1444458577
函数名:gmtime
函数功能:把日期和时间转换为格林威治(GMT)时间的函数。
函数原型:struct tm * __cdecl gmtime(const time_t *);
函数返回:当前的格林威治时间结构体指针struct tm *
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
int main()
{
time_t now;
now = time(&now);
struct tm* gmTime = gmtime(&now);
return 0;
}</span>
运行结果:可以看出来这里是格林威治时间 6:46:39 北京时间是东八区 +0800 加上8个小时正好是下午14:46:39
函数名:localtime
函数功能:把日期和时间转换为当地时间的函数。
函数原型:struct tm * __cdecl localtime(const time_t *);
函数返回:本地时间结构体指针struct tm *
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
int main()
{
time_t now;
now = time(&now);
struct tm* localTime = localtime(&now);
return 0;
}</span>
运行结果:可以看出来这里是北京时间 14:53:2
函数名:asctime
函数功能:把ttm结构体中储存的时间转换为字符串字符串格式:Www Mmm dd hh:mm:ss yyyy。
其中Www为星期;Mmm为月份;dd为日;hh为时;mm为分;ss为秒;yyyy为年份。
函数原型:char * __cdecl asctime(const struct tm *);
函数返回:时间字符串 Www Mmm dd hh:mm:ss yyyy。
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
int main()
{
time_t now;
now = time(&now);
struct tm* localTime = localtime(&now);
char * strTime = asctime(localTime);
printf("\r\n Now is the %s",strTime);
return 0;
}</span>
运行结果:Now is the Sat Oct 10 14:57:59 2015
函数名:ctime
函数功能:把秒时间转换为字符串字符串格式:Www Mmm dd hh:mm:ss yyyy。和asctime函数只是参数不同。
其中Www为星期;Mmm为月份;dd为日;hh为时;mm为分;ss为秒;yyyy为年份。
函数原型:char * __cdecl ctime(const time_t *);
函数返回:时间字符串 Www Mmm dd hh:mm:ss yyyy。
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
int main()
{
time_t now;
now = time(&now);
char * strTime = ctime(&now);
printf("\r\n Now is the %s",strTime);
return 0;
}</span>
运行结果: Now is the Sat Oct 10 15:03:10 2015
函数名:mktime
函数功能:将时间转换为自1970年1月1日以来持续时间的秒数 。 这里的参数必须是本地时间。
函数原型:time_t __cdecl mktime(struct tm *);
函数返回:自1970年1月1日以来持续时间的秒数
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
int main()
{
time_t now;
time_t now_after;
now = time(&now);
struct tm * gmTime = <span style="color:#FF0000;">gmtime</span>(&now);
now_after = mktime(gmTime);
printf("\r\n Before is %d, After is %d ",now , now_after);
return 0;
}</span>
运行结果: Before is 1444461251, After is 1444432451
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
int main()
{
<pre name="code" class="cpp"> time_t now;
time_t now_after;
now = time(&now);
struct tm * localTime = <span style="color:#FF0000;">localtime</span>(&now);
now_after = mktime(localTime);
printf("\r\n Before is %d, After is %d ",now , now_after);
return 0;
}</span>
运行结果:Before is 1444461359, After is 1444461359
函数名:strftime
函数功能:时间格式化
函数原型:size_t __cdecl strftime(char *, size_t, const char *, const struct tm *);
函数返回:格式化后字符串的长度 参照: typedef unsigned int size_t;
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
int main()
{
time_t now;
now = time(&now);
struct tm * localTime = localtime(&now);
char timeStr[255];
size_t num = strftime(timeStr,sizeof(timeStr),"%a",localTime);
printf("\r\n Now is %s ",timeStr);
return 0;
}</span>
运行结果:Now is Sat
函数名:difftime
函数功能:计算两个时刻之间的时间差。
函数原型:double __cdecl difftime(time_t, time_t);
函数返回:返回时间差
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
#include <windows.h>
int main()
{
time_t now;
now = time(&now);
Sleep(5000);
time_t now2;
now2 = time(&now2);
double dif = difftime(now,now2);
printf("\r\n The time differrence is %f ",dif);
return 0;
}</span>
运行结果:The time differrence is -5.000000
函数名:clock
函数功能:返回处理器调用某个进程或函数所花费的时间
函数原型:clock_t __cdecl clock(void);
函数返回:处理器调用某个进程或函数所花费的时间 typedef long clock_t;
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
int main()
{
clock_t times = clock();
printf("\r\n This program call time is %d ",times);
return 0;
}</span>
运行结果:This program call time is 10
函数名:_strdate
函数功能:获取当前系统日期(不包括时间),函数以字符指针形式为返回.
函数原型:char * __cdecl _strdate(char *);
函数返回:当前系统时间 DD/MM/YY
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
int main()
{
char time[100];
_strdate(time);
printf("\r\n The time is %s ",time);
return 0;
}</span>
运行结果: The time is 10/10/15
函数名:_strtime
函数功能:获取当前系统时间(不包括日期),函数以字符指针形式为返回.
函数原型:char * __cdecl _strtime(char *);
函数返回:当前系统时间 HH:MM:SS
<span style="font-family:KaiTi_GB2312;font-size:14px;">#include <stdio.h>
#include <time.h>
int main()
{
char time[100];
_strtime(time);
printf("\r\n The time is %s ",time);
return 0;
}</span>
运行结果:The time is 15:53:32
该篇文章参照了下边的博客: http://blog.csdn.net/wangluojisuan/article/details/7045592 。
本身想要直接转载,但因为原博客里边著名了“未经作者同意,严禁转摘”。这里,自己重新整理了一份。
这里边设计到的 格林威治时间 和 协调世界时(UTC时间)的介绍请参考照下边链接:
http://blog.sina.com.cn/s/blog_6575a3c20100jdqe.html