您的位置:首页 > 其它

time_t、struct tm,ctime各数据类型、函数详解,转换以及跟时间字符串的转换

2011-05-16 11:04 1851 查看

time函数与time_t类型

获取当前时间:

time_t time(time_t *t);

<time.h>

#ifndef __TIME_T

#define __TIME_T /* 避免重复定义 time_t */

typedef long time_t; /* 时间值time_t 为长整型的别名*/

#endif

localtime函数与struct tm类型

struct tm *localtime(const time_t *timep);

struct tm {

int tm_sec; /* seconds */

int tm_min; /* minutes */

int tm_hour; /* hours */

int tm_mday; /* day of the month */

int tm_mon; /* month */

int tm_year; /* year */

int tm_wday; /* day of the week */

int tm_yday; /* day in the year */

int tm_isdst; /* daylight saving time */

};

The members of the tm structure are:

tm_sec

The number of seconds after the minute, normally in the range 0 to 59, but can be up to 60 to allow for leap seconds.

tm_min

The number of minutes after the hour, in the range 0 to 59.

tm_hour

The number of hours past midnight, in the range 0 to 23.

tm_mday

The day of the month, in the range 1 to 31.

tm_mon

The number of months since January, in the range 0 to 11.

tm_year

The number of years since 1900.

tm_wday

The number of days since Sunday, in the range 0 to 6.

tm_yday

The number of days since January 1, in the range 0 to 365.

tm_isdst

A flag that indicates whether daylight saving time is in effect at the time described. The value is positive if daylight saving time is

in effect, zero if it is not, and negative if the information is not available.

ctime函数

char *ctime(const time_t *timep);

ctime(t)函数调用和asctime(localtime(t))函数调用结果一样,返回结果格式类似:"Wed Jun 30 21:49:08 1993\n"

asctime函数

char *asctime(const struct tm *tm);

跟ctime函数返回同样格式的字符串。

time_t 转 struct tm

struct tm *localtime( const time_t *timer );

struct tm 转 time_t

time_t mktime( struct tm *timeptr );

gmtime函数与localtime函数区别

struct tm *gmtime(const time_t *timep);

都是从time_t转到struct tm,区别是:

gmtime转出来的是0时区的标准时间。

localtime是将时区考虑在内了,转出的当前时区的时间。但是注意,有些嵌入式设备上被裁减过的系统,时区没有被设置好,导致二者转出来的时间都是0时区的。

#include <time.h>
#include <stdio.h>

int main(int argc, char **argv)
{
time_t tmpcal_ptr = {0};
struct tm *tmp_ptr = NULL;

tmpcal_ptr = time(NULL);
printf("tmpcal_ptr=%d\n", tmpcal_ptr);

tmp_ptr = gmtime(&tmpcal_ptr);
printf("after gmtime, the time is:\n%d:%d:%d", tmp_ptr->tm_hour,
tmp_ptr->tm_min, tmp_ptr->tm_sec);

tmp_ptr = localtime(&tmpcal_ptr);
printf("after localtime, the time is:\n%d:%d:%d", tmp_ptr->tm_hour,
tmp_ptr->tm_min, tmp_ptr->tm_sec);

return 0;
}


结果:



//指定time_t类型的时间,格式化为YYYYMMDDHH24MISS型的字符串

void FormatTime(time_t time1, char *szTime)

{

struct tm tm1;

#ifdef WIN32

tm1 = *localtime( &time1 );

#else

localtime_r( &time1, &tm1 );

#endif

sprintf( szTime, "%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d",

tm1.tm_year+1900, tm1.tm_mon + 1, tm1.tm_mday,

tm1.tm_hour, tm1.tm_min, tm1.tm_sec );

}

//指定YYYYMMDDHH24MISS型的时间,格式化为time_t型的时间

static time_t string2time_t( const char *psz_time, const char *psz_format )
{
struct tm tm_tmp;
time_t time;

if ( !psz_time || !psz_format )
return 0;

sscanf( psz_time, psz_format,
&tm_tmp.tm_year,
&tm_tmp.tm_mon,
&tm_tmp.tm_mday,
&tm_tmp.tm_hour,
&tm_tmp.tm_min,
&tm_tmp.tm_sec );

tm_tmp.tm_year -= 1900;
tm_tmp.tm_mon --;
tm_tmp.tm_isdst=-1;

time = mktime( &tm_tmp );
return time;

}
调用:
const char *psz_format = "%4d-%2d-%2d %2d:%2d:%2d";
time_t time = string2time_t( "2014-04-13 11:48:57", psz_format );


使用注意:

这些时间函数中,转化为字符串的函数,由于使用了静态存储区,下一次使用对前一次使用结果有影响,所以使用后要自己保存字符串。不然就用相应的xxx_r函数。

/article/1534625.html

/article/6983376.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐