当前位置:首页 » 编程语言 » c语言tm结构
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言tm结构

发布时间: 2022-11-25 11:12:23

c语言时间函数time_t

1、time_t // 时间类型(time.h 定义)
struct tm { // 时间结构,time.h 定义如下:
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;
}
time ( &rawtime ); // 获取时间,以秒计,从1970年1月一日起算,存于rawtime
localtime ( &rawtime ); //转为当地时间,tm 时间结构
asctime() // 转为标准ASCII时间格式:
//就是直接打印tm,tm_year 从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1

2、time函数使用示例

#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));

return0;
}

② C语言 time()

头文件time.h

@函数名称: localtime
函数原型: struct tm *localtime(const time_t *timer)
函数功能: 返回一个以tm结构表达的机器时间信息
函数返回: 以tm结构表达的时间,结构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;
};
参数说明: timer-使用time()函数获得的机器时间

#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
time_t timer;
struct tm *tblock;
timer=time(NULL);
tblock=localtime(&timer);
printf("Local time is: %s",asctime(tblock));
return 0;
}

@函数名称: asctime
函数原型: char* asctime(struct tm * ptr)
函数功能: 得到机器时间(日期时间转换为ASCII码)
函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年
参数说明: 结构指针ptr应通过函数localtime()和gmtime()得到
所属文件: <time.h>

#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
struct tm t;
char str[80];
t.tm_sec=1;
t.tm_min=3;
t.tm_hour=7;
t.tm_mday=22;
t.tm_mon=11;
t.tm_year=56;
t.tm_wday=4;
t.tm_yday=0;
t.tm_isdst=0;
strcpy(str,asctime(&t));
printf("%s",str);
return 0;
}

@函数名称: ctime
函数原型: char *ctime(long time)
函数功能: 得到日历时间
函数返回: 返回字符串格式:星期,月,日,小时:分:秒,年
参数说明: time-该参数应由函数time获得
所属文件: <time.h>

#include <stdio.h>
#include <time.h>
int main()
{
time_t t;
time(&t);
printf("Today's date and time: %s",ctime(&t));
return 0;
}

@函数名称: difftime
函数原型: double difftime(time_t time2, time_t time1)
函数功能: 得到两次机器时间差,单位为秒
函数返回: 时间差,单位为秒
参数说明: time1-机器时间一,time2-机器时间二.该参数应使用time函数获得
所属文件: <time.h>

#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
int main()
{
time_t first, second;
clrscr();
first=time(NULL);
delay(2000);
second=time(NULL);
printf("The difference is: %f seconds",difftime(second,first));
getch();
return 0;
}

@函数名称: gmtime
函数原型: struct tm *gmtime(time_t *time)
函数功能: 得到以结构tm表示的时间信息
函数返回: 以结构tm表示的时间信息指针
参数说明: time-用函数time()得到的时间信息
所属文件: <time.h>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
char *tzstr="TZ=PST8PDT";
int main()
{
time_t t;
struct tm *gmt, *area;
putenv(tzstr);
tzset();
t=time(NULL);
area=localtime(&t);
printf("Local time is:%s", asctime(area));
gmt=gmtime(&t);
printf("GMT is:%s", asctime(gmt));
return 0;
}

@函数名称: time
函数原型: time_t time(time_t *timer)
函数功能: 得到机器的日历时间或者设置日历时间
函数返回: 机器日历时间
参数说明: timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型
所属文件: <time.h>

#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
time_t t;
t=time();
printf("The number of seconds since January 1,1970 is %ld",t);
return 0;
}

@函数名称: tzset
函数原型: void tzset(void)
函数功能: UNIX兼容函数,用于得到时区,在DOS环境下无用途
函数返回:
参数说明:
所属文件: <time.h>

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
time_t td;
putenv("TZ=PST8PDT");
tzset();
time(&td);
printf("Current time=%s",asctime(localtime(&td)));
return 0;
}

③ C语言问题

1. sys/types.h

#define __need_timer_t
#define __need_clockid_t
#include <time.h>

2.time.h

typedef __time_t time_t;

# include <bits/types.h> /* This defines __time_t for us. */

3.bits/types.h
__STD_TYPE __TIME_T_TYPE __time_t; /* Seconds since the Epoch. */

# define __STD_TYPE __extension__ typedef

4.bits/typesizes.h

#define __TIME_T_TYPE __SLONGWORD_TYPE

5. bits/types.h
#define __SLONGWORD_TYPE long int

这里,基本就可以得出结论了:

__extension__ typedef long int time_t

则time_t类型的变量最大值为0x7fffffff

网上当来的,其实不必在意这些细节,记住是一个无符号长整形就可以,通俗点就是很大的一个正整数。表示从1970年1 月1日0点0分0秒到某一时刻的秒数即可。

④ C语言编写 输入 年 月 日 ,输出这个日期的下一天的日期

利用C标准函数time,localtime,mktime来实现
先将输入的年月日放到tm结构中
用mktime()函数生成一个整数
用这个整数+86400(24*60*60)
然后再转换成日期就OK了

⑤ c语言time_t ,tm都是些什么类型

下面是粘贴的哈,将就着看,time_t ---long tm是结构体。
typedef __kernel_time_t time_t;
typedef long __kernel_time_t;

struct tm {
164 /*
165 * the number of seconds after the minute, normally in the range
166 * 0 to 59, but can be up to 60 to allow for leap seconds
167 */
168 int tm_sec;
169 /* the number of minutes after the hour, in the range 0 to 59*/
170 int tm_min;
171 /* the number of hours past midnight, in the range 0 to 23 */
172 int tm_hour;
173 /* the day of the month, in the range 1 to 31 */
174 int tm_mday;
175 /* the number of months since January, in the range 0 to 11 */
176 int tm_mon;
177 /* the number of years since 1900 */
178 long tm_year;
179 /* the number of days since Sunday, in the range 0 to 6 */
180 int tm_wday;
181 /* the number of days since January 1, in the range 0 to 365 */
182 int tm_yday;
183 }

⑥ 如何用C语言编写一个显示时间的函数,要求时间显示精度到毫秒级别。


#include <cstdio>

#include <ctime>

using namespace std;


/* run this program using the console pauser or add your own getch, system("pause") or input loop */


void printTime() {

struct tm t; //tm结构指针

time_t now; //声明time_t类型变量

time(&now); //获取系统日期和时间

localtime_s(&t, &now); //获取当地日期和时间


//格式化输出本地时间

printf("年-月-日-时-分-秒:%d-%d-%d %d:%d:%d ", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);


}


int main(int argc, char** argv) {

printTime();

}

⑦ c语言程序解答(在线等)

根据题意:

1、项目序号应为唯一值,用自增变量填充。

2、时间使用struct tm结构体(考虑如需时间运算,可使用相关函数)。

3、自定义结构类型SIINFO,分别实现插入链表和打印链表两个功能。

4、由于这个演示程序执行完就结束程序了。所以链表我没有写free释放内存,如你后期扩充代码,自己写释放(除程序结束,malloc申请内存不会自动释放)。

下面是演示代码:

#include <stdio.h>

#include <malloc.h>

#include <time.h>

#define MS 4//最大类型个数

#define MN 20//名称最大字符长度

char stypes[MS][10]={"速度型","力量型","耐力型","对抗型"};//项目类别,自行扩展,对应修改MS常量

typedef struct SportsItemInfo//定义一个体育项目结构类型

{

int id;//项目序号,从数据结构考虑,该项不能重复,应设为自增从0开始(实际开发,这个值由数据库分配)。

char name[MN+1];//项目名称

int stype;//项目类(对应stypes行下标)

int n;//参赛人数

struct tm sDate;//竞赛时间

struct SportsItemInfo *next;

}SIINFO;

SIINFO *insert2List(SIINFO *p_sHead);//插入新数据,首次插入参数传NULL。参数:链表头节点地址(不是首节点)。成功返回头节点,失败返回NULL。

void selectFList(SIINFO *p_sHead);//查询链表

int main()

{

char c;

SIINFO *p_sHead=NULL,*stemp=NULL;

printf("新增体育项目: ");

while(1)

{

stemp=insert2List(p_sHead);

if(!stemp)

{

printf("ERROR! ");

return 1;

}

p_sHead=stemp;

printf("是否继续输入(Y/N):");

c=0;

while(c!='Y' && c!='N')scanf("%c",&c);

if(c=='N') break;

}

selectFList(p_sHead);

return 0;

}

void selectFList(SIINFO *pht)

{

int i=0;

if(pht)

{

printf(" 输出链表信息: ");

while(pht->next)

{

printf("-----------项目%d--------- ",++i);

printf("项目序号:%d ",pht->next->id);

printf("项目名称:%s ",pht->next->name);

printf("项目类别:%s ",stypes[pht->next->stype]);

printf("参赛人数:%d ",pht->next->n);

printf("参赛时间:%04d-%02d-%02d %02d:%02d:%02d ",pht->next->sDate.tm_year+1900,pht->next->sDate.tm_mon+1,pht->next->sDate.tm_mday,pht->next->sDate.tm_hour,pht->next->sDate.tm_min,pht->next->sDate.tm_sec);

printf("-------------------------- ");

pht=pht->next;

}

}

}

SIINFO *insert2List(SIINFO *p_sHead)

{

static int x=0;

static SIINFO *p_sTail=NULL;

int i;

SIINFO *p_new=NULL;

if(!p_sHead){

p_sHead=(SIINFO*)malloc(sizeof(SIINFO));

if(!p_sHead)

return NULL;

p_sHead->next=NULL;

p_sTail=NULL;

}

p_new=(SIINFO*)malloc(sizeof(SIINFO));

if(!p_new)

return NULL;

p_new->next=NULL;

p_new->id=x++;

printf("-------------------------- ");

printf("项目名称:"),scanf("%s",p_new->name);

for(i=0,printf("项目类(");i<MS-1;printf("%d、%s,",i,stypes[i]),i++);

printf("%d、%s):",i,stypes[i]);

p_new->stype=-1;

while(p_new->stype<0 || p_new->stype>MS-1)scanf("%d",&p_new->stype);

printf("参赛人数:"),scanf("%d",&p_new->n);

printf("参赛时间(输入格式:年-月-日 时:分:秒):");

scanf("%d-%d-%d %d:%d:%d",&p_new->sDate.tm_year,&p_new->sDate.tm_mon,&p_new->sDate.tm_mday,&p_new->sDate.tm_hour,&p_new->sDate.tm_min,&p_new->sDate.tm_sec);

p_new->sDate.tm_mon--;//tm结构的月份是从0开始对应1月

p_new->sDate.tm_year=p_new->sDate.tm_year-1900;//tm结构的年份是实际年份-1900

if(!p_sHead->next)

p_sHead->next=p_new;

else

p_sTail->next=p_new;

p_sTail=p_new;

printf("-------------------------- ");

return p_sHead;

}

⑧ C语言中的时间

以前实际上用过,很想对C语言中的时间函数了解多一点,趁着这个寒假,查了些资料,大概把我现在能用到的关于时间的操作在此记录下来。通过几个函数来熟悉C语言中对时间的操作。(注:以下程序均在VS2010上编译通过。)①time()函数。可以通过time()函数来获得日历时间。其原型为: time_t time(time_t *timer);一般参数为空,返回值类型time_t是一个长整型数,函数将返回现在的日历时间,即从一个时间点(所有不同版本的Visual C++都是从1970年1月1日0时0分0秒)到现在的经过的秒数。例子程序:#include<stdio.h>#include<time.h>void main(){ time_t lt; lt=time(NULL); printf("1970年1月1日0时0分0秒到现在经历了%ld秒%A",lt);}运行结果(结果与程序运行的时间有关,贴出我此时运行出的结果):1970年1月1日0时0分0秒到现在经历了1326975564秒请按任意键继续. . .②clock()函数。C语言中的计时函数。函数原型为: clock_t clock(void);clock()函数返回从“开启这个程序进程\”到“程序中调用clock()函数”时之间的CPU时钟计时单元数。返回值类型clock_t也是一个长整型数。在time.h头文件中定义了一个符号常量CLOCKS_PER_SEC,表示一秒钟会有多少个计时单元。所以通过简单的数学知识,可以知道在程序中用clock()/CLOCKS_PER_SEC来表示程序从开始到调用clock()函数时用了多少秒。例子程序:#include<stdio.h>#include<time.h>void main(){ clock_t lt; for(int i=0;i<1000000000;i++); lt=clock(); printf("循环执行1000000000个空操作需要%f秒%A",(double)lt/CLOCKS_PER_SEC);}运行结果(在不同的机器上运行的结果可能不一样,下面是在我自己的笔记本上运行的结果):循环执行1000000000个空操作需要3.484000秒请按任意键继续. . .③使用C库函数来显示日期和时间。首先要介绍一下C语言中的一个日期的结构体类型,tm类型。其在time.h中的定义如下:#ifndef _TM_DEFINEDstruct 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; };#define _TM_DEFINED#endif然后可以介绍有关的函数了。time.h提供了两种不同的函数将日历时间(一个长整型数)转换成我们平时看到的把年月日时分秒分开的时间格式: struct tm *gmtime(const time_t *timer); struct tm *localtime(const time_t *timer);其中gmtime()函数是将日历时间转换为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数是将日历时间转换为本地时间(在中国地区获得的本地时间会比世界标准时间晚8个小时)。例子程序:#include<stdio.h>#include<time.h>void main(){ struct tm *local; time_t t; t=time(NULL); local=localtime(&t); printf("现在北京时间是%d点%A",local->tm_hour); local=gmtime(&t); printf("世界标准时间是%d点%A",local->tm_hour);}运行结果(运行结果与运行的时间有关,我是在早上9点多钟运行这个程序的):现在北京时间是9点世界标准时间是1点请按任意键继续. . .这样子我们就可以完全才输出此刻的年月日时分秒了,当然需要逐个来输出。其实C库函数还提供了一个很有用的以固定的时间格式来输出年月日时分秒的函数。这两个函数原型如下: char *asctime(const struct tm *timeptr); char *ctime(const time_t *timer);asctime()函数是通过tm结构来生成具有固定格式的保存时间信息的字符串,而ctime()是通过日历时间来生成时间字符串。这样下面的例子程序就容易理解了:#include<stdio.h>#include<time.h>void main(){ struct tm *local; time_t t; t=time(NULL); local=localtime(&t); printf(asctime(local)); local=gmtime(&t); printf(asctime(local)); printf(ctime(&t));}运行结果(我是在早上9点多运行这个程序的):Fri Jan 20 09:55:56 2012Fri Jan 20 01:55:56 2012Fri Jan 20 09:55:56 2012请按任意键继续. . .C语言还可以以我们规定的各种形式来规定输出时间的格式。要用到时可以查阅相关的资料,限于篇幅,就介绍到这里。④这里介绍计算持续的时间长度的函数。之前已经介绍了使用clock()函数的例子,它可以精确到毫秒级。其实我们也可以使用difftime()函数,但它只精确到秒。该函数的定义如下: double difftime(time_t time1,time_t time0);例子程序:#include<stdio.h>#include<time.h>#include<stdlib.h>void main(){ time_t start,end; start=time(NULL); for(int i=0;i<1000000000;i++); end=time(NULL); printf("这个循-环用了%f秒%A",difftime(end,start));}运行结果:这个循环用了3.000000秒请按任意键继续. . .⑤最后介绍mktime()函数。原型如下: time_t mktime(struct tm *timer);可以使用函数将用tm结构表示的时间转换为日历时间。其返回值就是转换后的日历时间。这样我们就可以先制定一个分解时间,然后对这个时间进行操作。下面的例子用来计算2012年1月20日是星期几:#include<time.h>#include<stdio.h>#include<stdlib.h>int main(void){ struct tm t; time_t t_of_day; t.tm_year=2012-1900; t.tm_mon=0; t.tm_mday=20; t.tm_hour=0; t.tm_min=12; t.tm_sec=1; t.tm_isdst=1; t_of_day=mktime(&t); printf(ctime(&t_of_day)); return 0;}运行结果:Fri Jan 20 00:12:01 2012请按任意键继续. . .

⑨ C语言中的常用的几种系统时间结构体类型

在C语言涉及中经常需要定时触发事件,涉及到获取系统时间,其结构体类型有多种。Unix/Linux系统下有以下几种时间结构:
1、time_t 类型:长整型,一般用来表示从1970-01-01 00:00:00时以来的秒数,精确度:秒;由函数time()获取;
该类型定义在头文件 /usr/include/sys/time.h 中:
#define _TIME_T
typedef long time_t;
#endif
函数定义:time_t time(time_t* lpt);
如:time_t time = time(NULL);
2、struct timeb 结构:它有两个主要成员,一个是秒,另一个是毫秒;精确度:毫秒(10E-3秒);
由函数ftime()获取struct timeb结构的时间;其定义如下:
struct timeb
{
time_t time;
unsigned short millitm;
short timezone;
short dstflag;
};
#include <sys/timeb.h>
int ftime(struct timeb* tp);
调用成功返回0;调用失败返回-1;
3、struct timeval 结构,它有两个成员;一个是秒,另一个表示微秒,精确度:微秒(10E-6);
由函数gettime0fday()获取;
struct timeval结构定义为:
struct timeval
{
long tv_sec;
long tv_usec;
}
读取struct timeval结构数据的函数说明:
#include <sys/time.h>
int gettimeofday(struct timeval* tv,struct timezone* tz);
该函数会提取系统当前时间,并把时间分为秒和微秒两部分填充到结构struct timeval中;同时把当地的时区信
息填充到结构struct timezone中;
返回值:成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存
取权限。
struct timezone结构的定义为:
struct timezone
{
int tz_minuteswest;
int tz_dsttime;
}
上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下
DST_NONE
DST_USA
DST_AUST
DST_WET
DST_MET
DST_EET
DST_CAN
DST_GB
DST_RUM
DST_TUR
DST_AUSTALT
4、struct timespec 结构:它是POSIX.4标准定义的一个时间结构,精确度:纳秒(10E-9秒);
由函数gethrestime()或gethrestime_lasttick()获取当前系统struct timespec结构的时间;其定义如下:
struct timespec
{
time_t tv_sec;
long tv_nsec;
};
typedef struct timespec timespec_t;
该结构定义在头头文件 /usr/include/sys/time_impl.h 中;
extern void gethrestime(timespec_t*);
extern void gethrestime_lasttick(timespec_t*);
5、clock_t 类型:由函数clock()获取;
#include <time.h>
clock_t clock(void);
该函数以微秒的方式返回CPU的时间;
类型 clock_t 定义在头文件/usr/include/sys/types.h中:
#ifndef _CLOCK_T
#define _CLOCK_T
typedef long clock_t;
#endif
6、struct tm 结构:由函数gmtime()解析time_t得到
struct tm*gmtime(const time_t*timep);
函数说明:gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后
将结果由结构tm返回。
结构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;
};
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min 代表目前分数,范围0-59
int tm_hour 从午夜算起的时数,范围为0-23
int tm_mday 目前月份的日数,范围01-31
int tm_mon 代表目前月份,从一月算起,范围从0-11
int tm_year 从1900 年算起至今的年数
int tm_wday 一星期的日数,从星期一算起,范围为0-6
int tm_yday 从今年1月1日算起至今的天数,范围为0-365
int tm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
返回值:返回结构tm代表目前UTC 时间
7、Unix对时间单位的定义:
#define SEC 1 // 秒
#define MILLISEC 1000 // 毫秒
#define MICROSEC 1000000 // 微秒
#define NANOSEC 1000000000 // 纳秒
8、时间格式化函数:
size_t strftime(char *str,size_t max,char *fmt,struct tm *tp); strftime有点像sprintf,其格式由fmt来指定。
%a : 本第几天名称,缩写
%A : 本第几天名称,全称
%b : 月份名称,缩写
%B : 月份名称,全称
%c : 与ctime/asctime格式相同
%d : 本月第几日名称,由零算起
%H : 当天第几个小时,24小时制,由零算起
%I : 当天第几个小时,12小时制,由零算起
%j : 当年第几天,由零算起
%m : 当年第几月,由零算起
%M : 该小时的第几分,由零算起
%p : AM或PM
%S : 该分钟的第几秒,由零算起
%U : 当年第几,由第一个日开始计算
%W : 当年第几,由第一个一开始计算
%w : 当第几日,由零算起
%x : 当地日期
%X : 当地时间
%y : 两位数的年份
%Y : 四位数的年份
%Z : 时区名称的缩写
%% : %符号

char * strptime(char *s,char *fmt,struct tm *tp); 如同scanf一样,解译字串成为tm格式
%h : 与%b及%B同
%c : 读取%x及%X格式
%C : 读取%C格式
%e : 与%d同
%D : 读取%m/%d/%y格式
%k : 与%H同
%l : 与%I同
%r : 读取"%I:%M:%S %p"格式
%R : 读取"%H:%M"格式
%T : 读取"%H:%M:%S"格式
%y : 读取两位数年份
%Y : 读取四位数年份
希望可以帮到你,谢谢!

⑩ C语言中如何把时间变量赋值到一个专门存放时间的数组里面

C语言中有专门储存时间的变量结构体 struct tm,在time.h头文件中。如果要把时间转换成字符数组,使用asctime函数即可。

1、asctime函数:
原型:char* asctime (const struct tm * timeptr);
功能:把timeptr指向的tm结构体中储存的时间转换为字符串;
头文件:time.h;
返回值:一个固定格式的字符串。字符串格式为:Www Mmm dd hh:mm:ss yyyy。其中Www为星期,Mmm为月份,dd为日,hh为时,mm为分,ss为秒,yyyy为年份。
2、例程:

#include<time.h>
#include<stdio.h>
intmain(){
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);//使用localtime函数把秒数时间rawtime转换为本地时间以tm结构体保存,并把tm结构体地址储存到timeinfo当中
printf("当前日期为:%s",asctime(timeinfo));//使用asctime函数把tm结构体中储存的时间转换为字符串,并输出
return0;
}