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

C语言中的常用的几种系统时间结构体类型,第1张

在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_ttv_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

typedeflong 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 SEC1// 秒

#define MILLISEC1000 // 毫秒

#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语言时间函数:

1、获得日历时间函数:

可以通过time()函数来获得日历时间(Calendar Time),其原型为:time_t time(time_t * timer)

如果已经声明了参数timer,可以从参数timer返回现在的日历时间,同时也可以通过返回值返回现在的日历时间,即从一个时间点(例如:1970年1月1日0时0分0秒)到现在此时的秒数。如果参数为空(NUL),函数将只通过返回值返回现在的日历时间,比汪搭如下面这个例子用来塌余显示当前的日历时间:

2、获得日期和时间函数:

这里说的日期和时间就是平时所说的年、月、日、时、分、秒等信息。从第2节我们已经知道这些信息都保存在一个名为tm的结构体中,那么如何将一个日历时间保存为一个tm结构的对象呢?

其中可以使用的函数是gmtime()和localtime(),这两个函数的原型为:

struct tm * gmtime(const time_t *timer)

struct tm * localtime(const time_t * timer)

其中gmtime()函数是将日历时间转化为世界标准时间(即格林尼困衫拿治时间),并返回一个tm结构体来保存这个时间,而localtime()函数是将日历时间转化为本地时间。比如现在用gmtime()函数获得的世界标准时间是2005年7月30日7点18分20秒,那么用localtime()函数在中国地区获得的本地时间会比世界标准时间晚8个小时,即2005年7月30日15点18分20秒。

/*

This file is part of the GNU C Library.

The GNU C Library is free softwareyou can redistribute it and/or

modify it under the terms of the GNU Library General Public License as

published by the Free Software Foundationeither version 2 of the

License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTYwithout even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

Library General Public License for more details.

You should have received a copy of the GNU Library General Public

License along with the GNU C Librarysee the file COPYING.LIB. If not,

write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,

Boston, MA 02111-1307, USA. */

/*

* ISO C Standard: 4.12 DATE and TIME <time.h>

*/

#ifndef _TIME_H

#if (! defined __need_time_t &&!defined __need_clock_t &&\

! defined __need_timespec)

# define _TIME_H1

# include <features.h>

__BEGIN_DECLS

#endif

#ifdef _TIME_H

/* Get size_t and NULL from <stddef.h>. */

# define __need_size_t

# define __need_NULL

# include <stddef.h>

/* This defines CLOCKS_PER_SEC, which is the number of processor clock

ticks per second. */

# include <bits/time.h>

/* This is the obsolete POSIX.1-1988 name for the same constant. */

# ifdef __USE_POSIX

# ifndef CLK_TCK

# define CLK_TCK CLOCKS_PER_SEC

# endif

# endif

#endif /* <time.h>included. */

#if !defined __clock_t_defined &&(defined _TIME_H || defined __need_clock_t)

# define __clock_t_defined 1

# include <bits/types.h>

/* Returned by `clock'. */

typedef __clock_t clock_t

#endif /* clock_t not defined and <time.h>or need clock_t. */

#undef __need_clock_t

#if !defined __time_t_defined &&(defined _TIME_H || defined __need_time_t)

# define __time_t_defined 1

# include <bits/types.h>

/* Returned by `time'. */

typedef __time_t time_t

#endif /* time_t not defined and <time.h>or need time_t. */

#undef __need_time_t

#if !defined __timespec_defined &&\

((defined _TIME_H &&defined __USE_POSIX199309) || defined __need_timespec)

# define __timespec_defined 1

/* POSIX.4 structure for a time value. This is like a `struct timeval' but

has nanoseconds instead of microseconds. */

struct timespec

{

long int tv_sec /* Seconds. */

long int tv_nsec /* Nanoseconds. */

}

#endif /* timespec not defined and <time.h>or need timespec. */

#undef __need_timespec

#ifdef _TIME_H

/* Used by other time functions. */

struct tm

{

int tm_sec /* Seconds. [0-60] (1 leap second) */

int tm_min /* Minutes. [0-59] */

int tm_hour /* Hours. [0-23] */

int tm_mday /* Day. [1-31] */

int tm_mon /* Month. [0-11] */

int tm_year /* Year - 1900. */

int tm_wday /* Day of week. [0-6] */

int tm_yday /* Days in year.[0-365] */

int tm_isdst/* DST. [-1/0/1]*/

# ifdef __USE_BSD

long int tm_gmtoff /* Seconds east of UTC. */

__const char *tm_zone /* Timezone abbreviation. */

# else

long int __tm_gmtoff/* Seconds east of UTC. */

__const char *__tm_zone /* Timezone abbreviation. */

# endif

}

/* Time used by the program so far (user time + system time).

The result / CLOCKS_PER_SECOND is program time in seconds. */

extern clock_t clock __P ((void))

/* Return the current time and put it in *TIMER if TIMER is not NULL. */

extern time_t time __P ((time_t *__timer))

/* Return the difference between TIME1 and TIME0. */

extern double difftime __P ((time_t __time1, time_t __time0))

__attribute__ ((__const__))

/* Return the `time_t' representation of TP and normalize TP. */

extern time_t mktime __P ((struct tm *__tp))

/* Format TP into S according to FORMAT.

Write no more than MAXSIZE characters and return the number

of characters written, or 0 if it would exceed MAXSIZE. */

extern size_t strftime __P ((char *__restrict __s, size_t __maxsize,

__const char *__restrict __format,

__const struct tm *__restrict __tp))

# ifdef __USE_XOPEN

/* Parse S according to FORMAT and store binary time information in TP.

The return value is a pointer to the first unparsed character in S. */

extern char *strptime __P ((__const char *__s, __const char *__fmt,

struct tm *__tp))

# endif

/* Return the `struct tm' representation of *TIMER

in Universal Coordinated Time (aka Greenwich Mean Time). */

extern struct tm *gmtime __P ((__const time_t *__timer))

/* Return the `struct tm' representation

of *TIMER in the local timezone. */

extern struct tm *localtime __P ((__const time_t *__timer))

# if defined __USE_POSIX || defined __USE_MISC

/* Return the `struct tm' representation of *TIMER in UTC,

using *TP to store the result. */

extern struct tm *__gmtime_r __P ((__const time_t *__restrict __timer,

struct tm *__restrict __tp))

extern struct tm *gmtime_r __P ((__const time_t *__restrict __timer,

struct tm *__restrict __tp))

/* Return the `struct tm' representation of *TIMER in local time,

using *TP to store the result. */

extern struct tm *localtime_r __P ((__const time_t *__restrict __timer,

struct tm *__restrict __tp))

# endif /* POSIX or misc */

/* Return a string of the form "Day Mon dd hh:mm:ss yyyy\n"

that is the representation of TP in this format. */

extern char *asctime __P ((__const struct tm *__tp))

/* Equivalent to `asctime (localtime (timer))'. */

extern char *ctime __P ((__const time_t *__timer))

# if defined __USE_POSIX || defined __USE_MISC

/* Reentrant versions of the above functions. */

/* Return in BUF a string of the form "Day Mon dd hh:mm:ss yyyy\n"

that is the representation of TP in this format. */

extern char *asctime_r __P ((__const struct tm *__restrict __tp,

char *__restrict __buf))

/* Equivalent to `asctime_r (localtime_r (timer, *TMP*), buf)'. */

extern char *ctime_r __P ((__const time_t *__restrict __timer,

char *__restrict __buf))

# endif /* POSIX or misc */

/* Defined in localtime.c. */

extern char *__tzname[2] /* Current timezone names. */

extern int __daylight /* If daylight-saving time is ever in use. */

extern long int __timezone/* Seconds west of UTC. */

# ifdef __USE_POSIX

/* Same as above. */

extern char *tzname[2]

/* Set time conversion information from the TZ environment variable.

If TZ is not defined, a locale-dependent default is used. */

extern void tzset __P ((void))

# endif

# if defined __USE_SVID || defined __USE_XOPEN

extern int daylight

extern long int timezone

# endif

# ifdef __USE_SVID

/* Set the system time to *WHEN.

This call is restricted to the superuser. */

extern int stime __P ((__const time_t *__when))

# endif

/* Nonzero if YEAR is a leap year (every 4 years,

except every 100th isn't, and every 400th is). */

# define __isleap(year) \

((year) % 4 == 0 &&((year) % 100 != 0 || (year) % 400 == 0))

# ifdef __USE_MISC

/* Miscellaneous functions many Unices inherited from the public domain

localtime package. These are included only for compatibility. */

/* Like `mktime', but for TP represents Universal Time, not local time. */

extern time_t timegm __P ((struct tm *__tp))

/* Another name for `mktime'. */

extern time_t timelocal __P ((struct tm *__tp))

/* Return the number of days in YEAR. */

extern int dysize __P ((int __year))

# endif

# ifdef __USE_POSIX199309

/* Pause execution for a number of nanoseconds. */

extern int nanosleep __P ((__const struct timespec *__requested_time,

struct timespec *__remaining))

# endif

# ifdef __USE_XOPEN_EXTENDED

/* Set to one of the following values to indicate an error.

1 the DATEMSK environment variable is null or undefined,

2 the template file cannot be opened for reading,

3 failed to get file status information,

4 the template file is not a regular file,

5 an error is encountered while reading the template file,

6 memory allication failed (not enough memory available),

7 there is no line in the template that matches the input,

8 invalid input specification Example: February 31 or a time is

specified that can not be represented in a time_t (representing

the time in seconds since 00:00:00 UTC, January 1, 1970) */

extern int getdate_err

/* Parse the given string as a date specification and return a value

representing the value. The templates from the file identified by

the environment variable DATEMSK are used. In case of an error

`getdate_err' is set. */

extern struct tm *getdate __P ((__const char *__string))

# endif

# ifdef __USE_GNU

/* Since `getdate' is not reentrant because of the use of `getdate_err'

and the static buffer to return the result in, we provide a thread-safe

variant. The functionality is the same. The result is returned in

the buffer pointed to by RESBUFP and in case of an error the return

value is != 0 with the same values as given above for `getdate_err'. */

extern int getdate_r __P ((__const char *__restrict __string,

struct tm *__restrict __resbufp))

# endif

__END_DECLS

#endif /* <time.h>included. */

#endif /* <time.h>not already included. */


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

原文地址:https://54852.com/tougao/12130160.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存