C语言中time(0)的意思是

C语言中time(0)的意思是,第1张

time是C语言获取当前系统时间函数,以秒作单位,代表当前时间自Unix标准时间戳(1970年1月1日0点0分0秒,GMT)经过了多少秒。

形式为time_t

time(time_t

t);

该函数提供两种返回方式,返回值,和指针参数。

可以根据需要选择。当参数t为空指针(NULL)时,只返回值。

而NULL的定义是(void

)

0,

所以time(0)也就是time(NULL)的另一种写法,表示只通过返回值获取时间值。

扩展资料:

time函数

函数名称:

localtime

函数原型:

struct

tm

localtime(const

time_t

timer)

函数功能:

返回一个以tm结构表达的机器时间信息

函数返回:

以tm结构表达的时间,结构tm定义如下:

#ifndef

_TM_DEFINED

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日开始的天数

取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推

/

int

tm_isdst;

/

夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。/

};

#define

_TM_DEFINED

#endif

参数说明:

timer-使用time()函数获得的机器时间

参考资料来源:百度百科-timeh

1、time_t t = time(0);

前半部分就是类型定义啊,就想int a 一样,后半部分就是一个函数呗,参数为0,具体的百度一下time函数就知道了, 这句话的意思和int x =add(1,2)一样的

后面的也都是这个样子,百度具体的函数就好了

#include <stdioh> #include <iostream> #include <timeh> using namespace std; #define MAX 100 int main(int argc, char argv[]) { srand( (unsigned)time( NULL ) ); for (int i=0;i<10;i++) cout<<rand()%MAX<<endl; return 0; } //srand()函数产生一个以当前时间开始的随机种子 //MAX 为最大值,其随机域为 0~MAX-1 延时 #include <ctime> #include <iostream> using namespace std; int main() { int OneMinute = CLOCKS_PER_SEC 1; clock_t Start, End; //一分钟所消耗的步跳数 int a=1; while(1){ Start = clock(); while( ( End = clock() ) - Start < OneMinute );//延时一分钟 cout<<a++<<endl; } return 0; } 一,获取日历时间 time_t 是定义在 timeh 中的一个类型,表示一个日历时间,也就是从 1970 年 1 月 1 日 0 时 0 分 0 秒到此时的秒数,原型是: typedef long time_t; 秒 函数 time 可以获取当前日历时间时间,time 的定义: time_t time(time_t ) / time value / 可以看出 time_t 其实是一个长整型, 由于长整型能表示的数值有限, 因此它能表示的最迟时间是 2038 年 1 月 18 日 19 时 14 分 07 #include <iostream> #include <timeh> using namespace std; int main(void) { time_t nowtime; nowtime = time(NULL); //获取当前时间 cout << nowtime << endl; return 0; } 输出结果:1268575163 二,获取本地时间 time_t 只是一个长整型,不符合我们的使用习惯,需要转换成本地时间,就要用到 tm 结构,timeh 中结构 tm 的原型是: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }; 可以看出,这个机构定义了年,月,日,时,分,秒,星期,当年中的某一天,夏令时可以用这个结构很方便的显示时间 用 localtime 获取当前系统时间,该函数将一个 time_t 时间转换成 tm 结构表示的时间,函数原型: struct tm localtime(const time_t ) 使用 gmtime 函数获取格林尼治时间,函数原型: struct tm gmtime(const time_t ) 为了方便显示时间,定义了一个函数 void dsptime(const struct tm ); #include <iostream> #include <timeh> using namespace std; void dsptime(const struct tm ); //输出时间 / seconds after the minute - [0,59] / / minutes after the hour - [0,59] / / hours since midnight - [0,23] / / day of the month - [1,31] / / months since January - [0,11] / / years since 1900 / / days since Sunday - [0,6] / / days since January 1 - [0,365] / / daylight savings time flag / int main(void) { time_t nowtime; nowtime = time(NULL); //获取日历时间 cout << nowtime << endl; //输出 nowtime struct tm local,gm; local=localtime(&nowtime); //获取当前系统时间 dsptime(local); gm=gmtime(&nowtime); //获取格林尼治时间 dsptime(gm); return 0; } void dsptime(const struct tm ptm) { char pxq[]={"日","一","二","三","四","五","六"}; cout << ptm->tm_year+1900 << "年" << ptm->tm_mon+1 << "月" << ptm->tm_mday << "日 " ; cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec <<" " ; cout << " 星期" <<pxq[ptm->tm_wday] << " 当年的第" << ptm->tm_yday << "天 " << endl; } 输出结果: 1268575163 2010 年 3 月 14 日 21:59:23 星期日 当年的第 72 天 2010 年 3 月 14 日 13:59:23 星期日 当年的第 72 天 三,输出时间 C/C++语言提供了用字符串格式表示时间的函数 char asctime(const struct tm ) char ctime(const time_t ) 这两个函数返回值都是一个表示时间的字符串,区别在于传入的参数不同 #include <iostream> #include <timeh> using namespace std; int main(void) { time_t nowtime; nowtime = time(NULL); //获取日历时间 cout << nowtime << endl; //输出 nowtime struct tm local; local=localtime(&nowtime); //获取当前系统时间 cout << asctime(local) ; cout << ctime(&nowtime) ; return 0; } 输出结果: 1268575163 Sun Mar 14 13:59:23 2010 Sun Mar 14 21:59:23 2010 四,计算时间间隔 可以通过 difftime 来计算两个时间的间隔,可以精确到秒,函数原型: double difftime(time_t, time_t) 要想精确到毫秒,就要使用 clock 函数了,函数原型: clock_t clock(void) 从定义可以看出 clock 返回一个 clock_t 类型,这个类型也定义在 timeh 中,原型是: typedef long clock_t clock_t 也是一个长整型,表示的是从程序开始运行到执行 clock 函数时所经过的 cpu 时钟计时单元数 #include <iostream> #include <timeh> using namespace std; int main(void) { time_t start, ends; clock_t cstart,cends; start=time(NULL); cstart=clock(); system("pause"); ends=time(NULL); cends=clock(); cout << "时间差:" << difftime(ends,start) << endl; cout << "Clock 时间差:" << cends-cstart << endl; return 0; } 输出结果: 请按任意键继续 时间差:3 Clock 时间差:3094 在 timeh 中定义了一个 CLOCKS_PER_SEC / Clock ticks macro - ANSI version / #define CLOCKS_PER_SEC 1000 表示 1 秒钟内有多少个时钟计时单元,在标准 C/C++中,最小的计时单位是 1 毫秒 五,自定义时间格式 C/C++在 timeh 中提供了一个自定义时间格式的函数 strftime,函数原型: size_t strftime(char strDest, size_t maxsize, const char format, const struct tm timeptr); 参数说明: char strDest:用来存放格式化后的字符串缓存, size_t maxsize:指定最多可以输出的字符数, const char format:格式化字符串, const struct tm timeptr:要转换的时间 可使用的格式化字符串: %a 星期几的简写 %A 星期几的全称 %b 月分的简写 %B 月份的全称 %c 标准的日期的时间串 %C 年份的后两位数字 %d 十进制表示的每月的第几天 %D 月/天/年 %e 在两字符域中,十进制表示的每月的第几天 %F 年-月-日 %g 年份的后两位数字,使用基于周的年 %G 年分,使用基于周的年 %h 简写的月份名 %H 24 小时制的小时 %I 12 小时制的小时 %j 十进制表示的每年的第几天 %m 十进制表示的月份 %M 十时制表示的分钟数 %n 新行符 %p 本地的 AM 或 PM 的等价显示 %r 12 小时的时间 %R 显示小时和分钟:hh:mm %S 十进制的秒数 %t 水平制表符 %T 显示时分秒:hh:mm:ss %u 每周的第几天,星期一为第一天 (值从 0 到 6,星期一为 0) %U 第年的第几周,把星期日做为第一天(值从 0 到 53) %V 每年的第几周,使用基于周的年 %w 十进制表示的星期几(值从 0 到 6,星期天为 0) %W 每年的第几周,把星期一做为第一天(值从 0 到 53) %x 标准的日期串 %X 标准的时间串 %y 不带世纪的十进制年份(值从 0 到 99) %Y 带世纪部分的十进制年份 %z,%Z 时区名称,如果不能得到时区名称则返回空字符 %% 百分号 #include <iostream> #include <timeh> using namespace std; int main(void) { time_t nowtime; nowtime = time(NULL); //获取日历时间 cout << nowtime << endl; //输出 nowtime struct tm local; local=localtime(&nowtime); //获取当前系统时间 char buf[80]; strftime(buf,80,"格式化输出:%Y-%m-%d %H:%M:%S",local); cout << buf << endl; return 0; } 输出结果: 1268575163 格式化输出:2010-03-14 21:59:23

#include <stdioh>

#include <timeh>

int main(void)

{

    time_t rawtime;

    struct tm timeinfo;

    time(&rawtime);

    timeinfo = localtime(&rawtime);

    printf("%s", asctime(timeinfo));

    return 0;

}

#include

#include

void

main()

{

time_t

ltime;

struct

tm

today;

time(

<ime

);

today

=

localtime(

<ime

);

printf("%04d-%02d-%02d

%02d:%02d:%02d\n",1900+today->tm_year,today->tm_mon+1,today->tm_mday,today->tm_hour,today->tm_min,today->tm_sec);

}

先调用time获得当前时间,这是个从1970-1-1午夜0点开始的秒数,然后调用localtime将该时间专为本地时间就可以打印了。其中tm_year需要加上1900,tm_mon需要加上1,看printf你就明白了。

可通过tm结构来获得日期和时间,tm结构在timeh中的定义如下:

#ifndef _TM_DEFINED

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日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 /

int tm_isdst; / 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。/

};

#define _TM_DEFINED

#endif

在DOSH中也有关于时间的定义,是这样写的

struct time {

unsigned char ti_min; / Minutes /

unsigned char ti_hour; / Hours /

unsigned char ti_hund; / Hundredths of seconds /

unsigned char ti_sec; / Seconds /

};

time结构中的ti_hund

其中英文的意思是1/100秒,也就是ti_hund是以10毫秒为单位的

可用gettime();获取系统时间

#include "timeh"

time_t time(time_t timer);

调用后将当前系统时间与1900年1月1日相差的秒数存入到timer中,timer可看成是一个长整型数

struct tm localtime(const time_t timer)

将time()函数调用的结果做为参数传入到localtime()函数中就能得到当前时间和日期,注意得到的年是和1970的差值,月份是和1月的差值

struct tm是一个结构体,定义如下

struct tm

{

int tm_sec; //当前秒

int tm_min; //当前分钟

int tm_hour; //当前小时

int tm_mday; //当前在本月中的天,如11月1日,则为1

int tm_mon; //当前月,范围是0~11

int tm_year; //当前年和1900的差值,如2006年则为36

int tm_wday; //当前在本星期中的天,范围0~6

int tm_yday; //当前在本年中的天,范围0~365

int tm_isdst; //这个我也不清楚

}

求当前时间的示例

int getSystemTime()

{

time_t timer;

struct tm t_tm;

time(&timer);

t_tm = localtime(&timer);

printf("today is %4d%02d%02d%02d%02d%02d\n", t_tmtm_year+1900,

t_tmtm_mon+1, t_tmtm_mday, t_tmtm_hour, t_tmtm_min, t_tmtm_sec);

return 0;

}

其他时间的函数和结构还有:

timeval结构

#include <include/linux/timeh>

struct timeval

{

time_t tv_sec;

susecond_t tv_usec; //当前妙内的微妙数

};

tms结构

保存着一个进程及其子进程使用的cpu时间

struct tms

{

clock_t tms_utime;

clock_t tms_stime;

clock_t tms_cutime;

clock_t tms_cstime;

}

timer_struct结构

#include <include/linux/timerh>

struct timer_struct

{

unsigned long expires; //定时器被激活的时刻

void (fn)(void); //定时器激活后的处理函数

}

utime函数

更改文件的存取和修改时间

int utime(const char pathname, const struct utimbuf times) // return value 0 or -1

times 为空指针,存取和修改时间设置为当前时间

struct utimbuf

{

time_t actime;

time_t modtime;

}

c++使用

头文件

timeh

,c使用ctime,具体使用方法搜一下

函数

sturct

tm

localtime(const

time_t

time)来获得本地时间

程序

既输出系统的本地时间

#include<timeh>

#include<stdioh>

int

main(void)

{

struct

tm

local;

time_t

t;

t=time(NULL);

local=localtime(&t);

printf("local

time

and

date:%s\n",asctime(local));

return

0;

}

几个常用的时间函数

time()//取得系统时间

gmtime()//转换成国标标准之间

localtime()//转换成本地时间

asctime()//转换成字符形式

ctime()//转换成字符形式

strftime()//类似于printf()

以上就是关于C语言中time(0)的意思是全部的内容,包括:C语言中time(0)的意思是、C语言获取系统时间的问题、C++时间函数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9611953.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存