C语言有没有读取系统时间的函数

如题所述

time.h头文件中倒是有
以下是百度来的,详细可查看http://zhidao.baidu.com/question/52856667.html?an=0&si=1

4.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秒。下面是个例子:
#include "time.h"
#include "stdio.h"
int main(void)
{
struct tm *local;
time_t t;
t=time(NUL);
local=localtime(&t);
printf("Local hour is: %d\n",local->tm_hour);
local=gmtime(&t);
printf("UTC hour is: %d\n",local->tm_hour);
return 0;
}

运行结果是:

Local hour is: 15
UTC hour is: 7

4.3 固定的时间格式

我们可以通过asctime()函数和ctime()函数将时间以固定的格式显示出来,两者的返回值都是char*型的字符串。返回的时间格式为:

星期几 月份 日期 时:分:秒 年\n\0
例如:Wed Jan 02 02:03:55 1980\n\0

其中\n是一个换行符,\0是一个空字符,表示字符串结束。下面是两个函数的原型:
char * asctime(const struct tm * timeptr);
char * ctime(const time_t *timer);
其中asctime()函数是通过tm结构来生成具有固定格式的保存时间信息的字符串,而ctime()是通过日历时间来生成时间字符串。这样的话,asctime()函数只是把tm结构对象中的各个域填到时间字符串的相应位置就行了,而ctime()函数需要先参照本地的时间设置,把日历时间转化为本地时间,然后再生成格式化后的字符串。在下面,如果t是一个非空的time_t变量的话,那么:
printf(ctime(&t));
等价于:
struct tm *ptr;
ptr=localtime(&t);
printf(asctime(ptr));
那么,下面这个程序的两条printf语句输出的结果就是不同的了(除非你将本地时区设为世界标准时间所在的时区):

#include "time.h"
#include "stdio.h"
int main(void)
{
struct tm *ptr;
time_t lt;
lt =time(NUL);
ptr=gmtime(<);
printf(asctime(ptr));
printf(ctime(<));
return 0;
}

运行结果:

Sat Jul 30 08:43:03 2005
Sat Jul 30 16:43:03 2005
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-09-01

C语言中读取系统时间的函数为time(),其函数原型为:

#include <time.h>

time_t  time( time_t * ) ;

time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。可以调用ctime()函数进行时间转换输出:

char * ctime(const time_t *timer);

将日历时间转换成本地时间,按年月日格式,进行输出,如:

Wed Sep 23 08:43:03 2015


C语言还提供了将秒数转换成相应的时间结构的函数:

struct tm * gmtime(const time_t *timer); //将日历时间转化为世界标准时间(即格林尼治时间)

struct tm * localtime(const time_t * timer);   //将日历时间转化为本地时间

将通过time()函数返回的值,转换成时间结构struct tm :

struct tm { 
    int tm_sec; /* 秒 – 取值区间为[0,59] */ 
    int tm_min; /* 分 - 取值区间为[0,59] */ 
    int tm_hour; /* 时 - 取值区间为[0,23] */ 
    int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */ 
    int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */ 
    int tm_year; /* 年份,其值等于实际年份减去1900 */ 
    int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */ 
    int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */ 
    int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/ 
};

编程者可以根据程序功能的情况,灵活的进行日期的读取与输出了。

第2个回答  2011-06-30
#include <stdio.h>
#include <time.h>

void main ()
{
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "\007The current date/time is: %s", asctime (timeinfo) );

exit(0);
}

=================
#include <time.h> -- 必须的时间函数头文件
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时间格式:
星期 月 日 时:分:秒 年
=========================================
你要的格式可这样输出:
printf ( "%4d-%02d-%02d %02d:%02d:%02d\n",1900+timeinfo->tm_year, 1+timeinfo->tm_mon,
timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);

就是直接打印tm,tm_year 从1900年计算,所以要加1900,
月tm_mon,从0计算,所以要加1
其它你一目了然啦。
第3个回答  2011-07-02
byte[] bt = new byte[1];

using (FileStream fs = new FileStream("文件绝对路径", FileMode.Open, FileAccess.Read))
{
fs.Seek(18, SeekOrigin.Begin);
fs.Read(bt, 0, 1);
fs.Close();
}

读入了bt
第4个回答  2011-07-02
byte[] bt = new byte[1];

using (FileStream fs = new FileStream("文件绝对路径", FileMode.Open, FileAccess.Read))
{
fs.Seek(18, SeekOrigin.Begin);
fs.Read(bt, 0, 1);
fs.Close();
}

读入了bt

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网