获取系统时间相关

获取系统时间

       头文件: #include <time.h>

       函数名:time_t time(time_t * t)

       描述:函数返回值和参数分别是获取时间的方式,最小单位是秒数。

       返回值:成功:时间值;

                     失败:-1;

      

       将获得的时间转化的各种函数

              char *asctime(const struct tm *tm);   参数为struct tm类型的。

char*asctime_r(const struct tm *tm, char *buf)

char*ctime(const time_t *timep);

char*ctime_r(const time_t *timep, char *buf);         

/*以上四个都是将时间转换成字符串的形式 */

structtm *gmtime(const time_t *timep);

structtm *gmtime_r(const time_t *timep, struct tm *result);

structtm *localtime(const time_t *timep);

structtm *localtime_r(const time_t *timep, struct tm *result);

time_tmktime(struct tm *tm);

      

转换时间数据结构模型:

structtm {

            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 */

};

inttm_sec 代表目前秒数,正常范围为0-59,但允许至61秒

inttm_min 代表目前分数,范围0-59

inttm_hour 从午夜算起的时数,范围为0-23

inttm_mday 目前月份的日数,范围01-31

inttm_mon 代表目前月份,从一月算起,范围从0-11

inttm_year 从1900 年算起至今的年数

inttm_wday 一星期的日数,从星期一算起,范围为0-6

inttm_yday 从今年1月1日算起至今的天数,范围为0-365

inttm_isdst 日光节约时间的旗标

此函数返回的时间日期未经时区转换,而是UTC时间。
注意:在直接使用localtime函数转化过的时间时,需要参照上面的参数说明,补全相应的数据。

使用格式化输出:size_t strftime(char *s, size_tmax, const char *format,  const struct tm*timeptr);

strftime函数对timeptr指向的tm结构所代表的时间和日期进行格式编排,其结果放在字符串s中。该字符串的长度被设置为(最少)maxsize个字符。格式字符串format用来对写入字符串的字符进行控制,它包含着将被传送到字符串里去的普通字符以及编排时间和日期格式的转换控制符。

相应的格式控制:

%a星期几的简写

%A星期几的全称

%b月分的简写

%B月份的全称

%c标准的日期的时间串

%C年份的后两位数字

%d十进制表示的每月的第几天

%D月/天/年

%e在两字符域中,十进制表示的每月的第几天

%F年-月-日

%g年份的后两位数字,使用基于周的年

%G年分,使用基于周的年

%h简写的月份名

%H24小时制的小时

%I12小时制的小时

%j十进制表示的每年的第几天

%m十进制表示的月份

%M十时制表示的分钟数

%n新行符

%p本地的AM或PM的等价显示

%r12小时的时间

%R显示小时和分钟:hh:mm

%S十进制的秒数

 

相对应反过来的操作对应的函数为:

char*strptime(const char *restrict buf, const char *restrict format, struct tm*restrict tm);

buf指向一个字符串格式的时间,函数将这个时间用format表示的格式解析,存放到tm中去

例子:

strptime("6Dec 2001 12:33:45", "%d %b %Y %H:%M:%S", &tm);

返回值:

解析正确返回最后解析字符的下一个字符的地址,失败返回NULL

 

 

获得更加精确时间

       time函数获得的时间只能精确到秒,要想获得微妙级的时间得使用gettimeofday函数

       头文件:#include <sys/time.h>      

       函数名:int gettimeofday(struct timeval * tv,struct timezone* tz)

       描述:这个函数主要用来或的时间和时区

       参数:tv:数据类型

structtimeval {

time_t      tv_sec;     /* seconds */

suseconds_ttv_usec;    /* microseconds */

};

struct timezone{                    /*一般不做设置*/

int tz_minuteswest;     /* minutes west of Greenwich */

int tz_dsttime;         /* type of DST correction */

};

任何一个 参数为NULL,将不会获得相应的数据。

       返回值:成功:0

                       失败:-1


一个简单的获取系统时间的程序

<pre name="code" class="cpp">#include <time.h>

int main(void)
{
        time_t  tt; 
        struct  tm* timep;


        time(&tt);
        timep = localtime(&tt);


        if(timep == NULL){
                return -1; 
        }   
    
        printf("Time:%d-%d-%d %d:%d:%d\n", \
                1900+timep->tm_year, 1+timep->tm_mon, timep->tm_mday,\
                 timep->tm_hour, timep->tm_min, timep->tm_sec);
}
 


详细整理 :http://blog.chinaunix.net/uid-20322341-id-1705359.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值