现在的位置: 首页 > 综合 > 正文

c语言的系统时间

2013年09月09日 ⁄ 综合 ⁄ 共 1143字 ⁄ 字号 评论关闭

1.变量的定义:

struct tm
{
 int tm_sec; /* (0 - 61) */
 int tm_min; /* (0 - 59) */
 int tm_hour; /* (0 - 23) */
 int tm_mday; /* (1 - 31) */
 int tm_mon; /* (0 - 11) */
 int tm_year; /* past 1900 */
 int tm_wday; /* (0 - 6) */
 int tm_yday; /* (0 - 365) */
 int tm_isdst; /* daylight savings flag */
};

2.一个实例:读取系统时间,显示:

/*
c语言中与系统时间相关的函数的示例

*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>

void main( void )
{
    /*时间*/
    time_t timer ;

    /*指向struct tm的指针*/
    struct tm *ptrtime;

    /*调用time()函数获取当前时间*/
    timer = time( NULL ) ;

    /*调用localtime()函数将获得的系统时间转化为指向struct tm的指针指向的结构体*/
    ptrtime = localtime( &timer ) ;

    /*用asctime()将结构体转化为字符串输出*/
    printf("Now is: %s",asctime( ptrtime ) ) ;
    getch();
}

3.相关头文件

#include<time.h>

4.其他函数

asctime()
相关函数
time,ctime,gmtime,localtime
表头文件
#include<time.h>
定义函数
char * asctime(const struct tm * timeptr);
函数说明
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:“Wed Jun 30 21:49:08 1993/n”
返回值
若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。
附加说明
返回一字符串表示目前当地的时间日期。
gettimeofday(取得目前的时间)
相关函数
time,ctime,ftime,settimeofday
表头文件
#include <sys/time.h>
#include <unistd.h>
定义函数
int gettimeofday ( struct timeval * tv , struct timezone * tz )
函数说明
gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。

抱歉!评论已关闭.